From 850ab4b13f3402464b7ff9a89261bc278031f961 Mon Sep 17 00:00:00 2001 From: bob Date: Fri, 4 Oct 2019 12:49:42 -0400 Subject: fixes for embedded text boxes. --- src/client/util/RichTextSchema.tsx | 60 ++++++++++++++++++----------- src/client/views/nodes/FormattedTextBox.tsx | 20 ++++++---- 2 files changed, 51 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/client/util/RichTextSchema.tsx b/src/client/util/RichTextSchema.tsx index 12cf40b32..e6b456313 100644 --- a/src/client/util/RichTextSchema.tsx +++ b/src/client/util/RichTextSchema.tsx @@ -3,7 +3,7 @@ import { redo, undo } from "prosemirror-history"; import { keymap } from "prosemirror-keymap"; import { DOMOutputSpecArray, Fragment, MarkSpec, Node, NodeSpec, Schema, Slice } from "prosemirror-model"; import { bulletList, listItem, orderedList } from 'prosemirror-schema-list'; -import { EditorState, TextSelection } from "prosemirror-state"; +import { EditorState, TextSelection, NodeSelection } from "prosemirror-state"; import { StepMap } from "prosemirror-transform"; import { EditorView } from "prosemirror-view"; import { Doc } from "../../new_fields/Doc"; @@ -635,6 +635,7 @@ export class ImageResizeView { this._outer = document.createElement("span"); this._outer.style.position = "relative"; this._outer.style.width = node.attrs.width; + this._outer.style.height = node.attrs.height; this._outer.style.display = "inline-block"; this._outer.style.overflow = "hidden"; (this._outer.style as any).float = node.attrs.float; @@ -650,8 +651,15 @@ export class ImageResizeView { this._handle.style.bottom = "-10px"; this._handle.style.right = "-10px"; let self = this; + this._img.onclick = function (e: any) { + e.stopPropagation(); + e.preventDefault(); + if (view.state.selection.node && view.state.selection.node.type !== view.state.schema.nodes.image) + view.dispatch( + view.state.tr.setSelection(new NodeSelection(view.state.doc.resolve(view.state.selection.from - 2)))); + } this._img.onpointerdown = function (e: any) { - if (!view.isOverlay || e.ctrlKey) { + if (e.ctrlKey) { e.preventDefault(); e.stopPropagation(); DocServer.GetRefField(node.attrs.docid).then(async linkDoc => @@ -663,32 +671,31 @@ export class ImageResizeView { this._handle.onpointerdown = function (e: any) { e.preventDefault(); e.stopPropagation(); + let wid = Number(getComputedStyle(self._img).width!.replace(/px/, "")); + let hgt = Number(getComputedStyle(self._img).height!.replace(/px/, "")); const startX = e.pageX; const startWidth = parseFloat(node.attrs.width); const onpointermove = (e: any) => { const currentX = e.pageX; const diffInPx = currentX - startX; self._outer.style.width = `${startWidth + diffInPx}`; - //Array.from(FormattedTextBox.InputBoxOverlay!.CurrentDiv.getElementsByTagName("img")).map((img: any) => img.opacity = "0.1"); - FormattedTextBox.InputBoxOverlay!.CurrentDiv.style.opacity = "0"; + self._outer.style.height = `${(startWidth + diffInPx) * hgt / wid}`; }; const onpointerup = () => { document.removeEventListener("pointermove", onpointermove); document.removeEventListener("pointerup", onpointerup); - view.dispatch( - view.state.tr.setSelection(view.state.selection).setNodeMarkup(getPos(), null, - { ...node.attrs, width: self._outer.style.width }) - ); - FormattedTextBox.InputBoxOverlay!.CurrentDiv.style.opacity = "1"; + let pos = view.state.selection.from; + view.dispatch(view.state.tr.setNodeMarkup(getPos(), null, { ...node.attrs, width: self._outer.style.width, height: self._outer.style.height })); + view.dispatch(view.state.tr.setSelection(new NodeSelection(view.state.doc.resolve(pos)))); }; document.addEventListener("pointermove", onpointermove); document.addEventListener("pointerup", onpointerup); }; - this._outer.appendChild(this._handle); this._outer.appendChild(this._img); + this._outer.appendChild(this._handle); (this as any).dom = this._outer; } @@ -761,37 +768,46 @@ export class DashDocView { } }); let self = this; - this._dashSpan.onpointerdown = function (e: any) { - }; + this._dashSpan.onclick = function (e: any) { + FormattedTextBox.firstTarget && FormattedTextBox.firstTarget(); + } + this._dashSpan.onkeydown = function (e: any) { + e.stopPropagation(); + } + this._dashSpan.onkeypress = function (e: any) { + e.stopPropagation(); + } + this._dashSpan.onkeyup = function (e: any) { + e.stopPropagation(); + } this._handle.onpointerdown = function (e: any) { e.preventDefault(); e.stopPropagation(); const startX = e.pageX; + const startY = e.pageY; const startWidth = parseFloat(node.attrs.width); + const startHeight = parseFloat(node.attrs.height); const onpointermove = (e: any) => { - const currentX = e.pageX; - const diffInPx = currentX - startX; + const diffInPx = e.pageX - startX; + const diffInPy = e.pageY - startY; self._outer.style.width = `${startWidth + diffInPx}`; - //Array.from(FormattedTextBox.InputBoxOverlay!.CurrentDiv.getElementsByTagName("img")).map((img: any) => img.opacity = "0.1"); - FormattedTextBox.InputBoxOverlay!.CurrentDiv.style.opacity = "0"; + self._outer.style.height = `${startHeight + diffInPy}`; }; const onpointerup = () => { document.removeEventListener("pointermove", onpointermove); document.removeEventListener("pointerup", onpointerup); - view.dispatch( - view.state.tr.setSelection(view.state.selection).setNodeMarkup(getPos(), null, - { ...node.attrs, width: self._outer.style.width }) - ); - FormattedTextBox.InputBoxOverlay!.CurrentDiv.style.opacity = "1"; + let pos = view.state.selection.from; + view.dispatch(view.state.tr.setNodeMarkup(getPos(), null, { ...node.attrs, width: self._outer.style.width, height: self._outer.style.height })); + view.dispatch(view.state.tr.setSelection(new NodeSelection(view.state.doc.resolve(pos)))); }; document.addEventListener("pointermove", onpointermove); document.addEventListener("pointerup", onpointerup); }; - this._outer.appendChild(this._handle); this._outer.appendChild(this._dashSpan); + this._outer.appendChild(this._handle); (this as any).dom = this._outer; } diff --git a/src/client/views/nodes/FormattedTextBox.tsx b/src/client/views/nodes/FormattedTextBox.tsx index 13eb78f48..05904e1e7 100644 --- a/src/client/views/nodes/FormattedTextBox.tsx +++ b/src/client/views/nodes/FormattedTextBox.tsx @@ -774,9 +774,10 @@ export class FormattedTextBox extends DocComponent<(FieldViewProps & FormattedTe this._searchReactionDisposer && this._searchReactionDisposer(); this._editorView && this._editorView.destroy(); } - - + public static firstTarget: () => void; onPointerDown = (e: React.PointerEvent): void => { + if ((e.nativeEvent as any).formattedHandled) return; + (e.nativeEvent as any).formattedHandled = true; let pos = this._editorView!.posAtCoords({ left: e.clientX, top: e.clientY }); pos && (this._nodeClicked = this._editorView!.state.doc.nodeAt(pos.pos)); if (this.props.onClick && e.button === 0) { @@ -785,10 +786,19 @@ export class FormattedTextBox extends DocComponent<(FieldViewProps & FormattedTe if (e.button === 0 && this.props.isSelected() && !e.altKey && !e.ctrlKey && !e.metaKey) { e.stopPropagation(); } - let ctrlKey = e.ctrlKey; if (e.button === 2 || (e.button === 0 && e.ctrlKey)) { e.preventDefault(); } + FormattedTextBox.firstTarget = () => { // this is here to support nested text boxes. when that happens, the click event will propagate through prosemirror to the outer editor. In RichTextSchema, the outer editor calls this function to revert the focus/selection + if (pos && pos.pos > 0) { + let node = this._editorView!.state.doc.nodeAt(pos.pos); + if (!node || (node.type !== this._editorView!.state.schema.nodes.dashDoc && node.type !== this._editorView!.state.schema.nodes.image && + pos.pos !== this._editorView!.state.selection.from)) { + this._editorView!.dispatch(this._editorView!.state.tr.setSelection(new TextSelection(this._editorView!.state.doc.resolve(pos!.pos)))); + this._editorView!.focus(); + } + } + } } onPointerUp = (e: React.PointerEvent): void => { @@ -873,10 +883,6 @@ export class FormattedTextBox extends DocComponent<(FieldViewProps & FormattedTe } } } - let pos = this._editorView!.posAtCoords({ left: e.clientX, top: e.clientY }); - if (pos && pos.pos > 0) { - this._editorView!.dispatch(this._editorView!.state.tr.setSelection(new TextSelection(this._editorView!.state.doc.resolve(pos.pos)))); - } this._editorView!.focus(); if (this._linkClicked) { this._linkClicked = ""; -- cgit v1.2.3-70-g09d2