keep menuBubble in bounding box of editor

This commit is contained in:
Chrissi2812
2019-04-25 16:01:27 +02:00
parent e441041860
commit 2f0acf2f91
3 changed files with 14 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
<template> <template>
<div class="editor"> <div class="editor">
<editor-menu-bubble :editor="editor"> <editor-menu-bubble :editor="editor" :keepInBounds="keepInBounds">
<div <div
slot-scope="{ commands, isActive, menu }" slot-scope="{ commands, isActive, menu }"
class="menububble" class="menububble"
@@ -69,6 +69,7 @@ export default {
}, },
data() { data() {
return { return {
keepInBounds: true,
editor: new Editor({ editor: new Editor({
extensions: [ extensions: [
new Blockquote(), new Blockquote(),

View File

@@ -7,6 +7,10 @@ export default {
default: null, default: null,
type: Object, type: Object,
}, },
keepInBounds: {
default: true,
type: Boolean,
},
}, },
data() { data() {
@@ -27,6 +31,7 @@ export default {
this.$nextTick(() => { this.$nextTick(() => {
editor.registerPlugin(MenuBubble({ editor.registerPlugin(MenuBubble({
element: this.$el, element: this.$el,
keepInBounds: this.keepInBounds,
onUpdate: menu => { onUpdate: menu => {
// the second check ensures event is fired only once // the second check ensures event is fired only once
if (menu.isActive && this.menu.isActive === false) { if (menu.isActive && this.menu.isActive === false) {

View File

@@ -57,6 +57,7 @@ class Menu {
this.options = { this.options = {
...{ ...{
element: null, element: null,
keepInBounds: true,
onUpdate: () => false, onUpdate: () => false,
}, },
...options, ...options,
@@ -94,14 +95,17 @@ class Menu {
// The box in which the tooltip is positioned, to use as base // The box in which the tooltip is positioned, to use as base
const box = this.options.element.offsetParent.getBoundingClientRect() const box = this.options.element.offsetParent.getBoundingClientRect()
const el = this.options.element.getBoundingClientRect()
// Find a center-ish x position from the selection endpoints (when // Find a center-ish x position from the selection endpoints (when
// crossing lines, end may be more to the left) // crossing lines, end may be more to the left)
const left = Math.max((start.left + end.left) / 2, start.left + 3) const left = ((start.left + end.left) / 2) - box.left
// Keep the menuBubble in the bounding box of the offsetParent i
this.left = Math.round(this.options.keepInBounds
? Math.min(box.width - (el.width / 2), Math.max(left, el.width / 2)) : left)
this.bottom = Math.round(box.bottom - start.top)
this.isActive = true this.isActive = true
this.left = parseInt(left - box.left, 10)
this.bottom = parseInt(box.bottom - start.top, 10)
this.sendUpdate() this.sendUpdate()
} }