Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tableEditing function more extendable #107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
59 changes: 29 additions & 30 deletions src/input.js
Expand Up @@ -114,50 +114,25 @@ 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
}
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)
Expand All @@ -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)
Expand Down