diff --git a/src/index.js b/src/index.js index 80d94539..9ef0e7a3 100644 --- a/src/index.js +++ b/src/index.js @@ -33,7 +33,7 @@ export function tableEditing({ allowTableNodeSelection = false } = {}) { state: { init() { return null }, apply(tr, cur) { - let set = tr.getMeta(tableEditingKey) + let set = tr.getMeta(this.spec.key) if (set != null) return set == -1 ? null : set if (cur == null || !tr.docChanged) return cur let {deleted, pos} = tr.mapping.mapResult(cur) @@ -49,7 +49,7 @@ export function tableEditing({ allowTableNodeSelection = false } = {}) { }, createSelectionBetween(view) { - if (tableEditingKey.getState(view.state) != null) return view.state.selection + if (this.spec.key.getState(view.state) != null) return view.state.selection }, handleTripleClick, diff --git a/src/input.js b/src/input.js index 1b183a5f..05befe47 100644 --- a/src/input.js +++ b/src/input.js @@ -114,28 +114,11 @@ export function handlePaste(view, _, slice) { export function handleMouseDown(view, startEvent) { if (startEvent.ctrlKey || startEvent.metaKey) return - let startDOMCell = domInCell(view, startEvent.target), $anchor - if (startEvent.shiftKey && (view.state.selection instanceof CellSelection)) { - // Adding to an existing cell selection - setCellSelection(view.state.selection.$anchorCell, startEvent) - startEvent.preventDefault() - } else if (startEvent.shiftKey && startDOMCell && - ($anchor = cellAround(view.state.selection.$anchor)) != null && - cellUnderMouse(view, startEvent).pos != $anchor.pos) { - // Adding to a selection that starts in another cell (causing a - // cell selection to be created). - setCellSelection($anchor, startEvent) - startEvent.preventDefault() - } else if (!startDOMCell) { - // Not in a cell, let the default behavior happen. - return - } - // Create and dispatch a cell selection between the given anchor and // the position under the mouse. - function setCellSelection($anchor, event) { + const setCellSelection = ($anchor, event) => { let $head = cellUnderMouse(view, event) - let starting = key.getState(view.state) == null + let starting = this.spec.key.getState(view.state) == null if (!$head || !inSameTable($anchor, $head)) { if (starting) $head = $anchor else return @@ -143,21 +126,13 @@ export function handleMouseDown(view, startEvent) { let selection = new CellSelection($anchor, $head) if (starting || !view.state.selection.eq(selection)) { let tr = view.state.tr.setSelection(selection) - if (starting) tr.setMeta(key, $anchor.pos) + if (starting) tr.setMeta(this.spec.key, $anchor.pos) view.dispatch(tr) } } - // Stop listening to mouse motion events. - function stop() { - view.root.removeEventListener("mouseup", stop) - view.root.removeEventListener("dragstart", stop) - view.root.removeEventListener("mousemove", move) - if (key.getState(view.state) != null) view.dispatch(view.state.tr.setMeta(key, -1)) - } - - function move(event) { - let anchor = key.getState(view.state), $anchor + const move = (event) => { + let anchor = this.spec.key.getState(view.state), $anchor if (anchor != null) { // Continuing an existing cross-cell selection $anchor = view.state.doc.resolve(anchor) @@ -168,6 +143,30 @@ export function handleMouseDown(view, startEvent) { } if ($anchor) setCellSelection($anchor, event) } + + // Stop listening to mouse motion events. + const stop = () => { + view.root.removeEventListener("mouseup", stop) + view.root.removeEventListener("dragstart", stop) + view.root.removeEventListener("mousemove", move) + if (this.spec.key.getState(view.state) != null) view.dispatch(view.state.tr.setMeta(this.spec.key, -1)) + } + let startDOMCell = domInCell(view, startEvent.target), $anchor + if (startEvent.shiftKey && (view.state.selection instanceof CellSelection)) { + // Adding to an existing cell selection + setCellSelection(view.state.selection.$anchorCell, startEvent) + startEvent.preventDefault() + } else if (startEvent.shiftKey && startDOMCell && + ($anchor = cellAround(view.state.selection.$anchor)) != null && + cellUnderMouse(view, startEvent).pos != $anchor.pos) { + // Adding to a selection that starts in another cell (causing a + // cell selection to be created). + setCellSelection($anchor, startEvent) + startEvent.preventDefault() + } else if (!startDOMCell) { + // Not in a cell, let the default behavior happen. + return + } view.root.addEventListener("mouseup", stop) view.root.addEventListener("dragstart", stop) view.root.addEventListener("mousemove", move)