src/lib/org/checklistPlugin.js (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import { ViewPlugin } from '@codemirror/view';
import { toggleChecklistLine, isChecklistLine } from './editorUtils.js';
export function checklistPlugin(getReadOnly) {
return ViewPlugin.fromClass(
class {
constructor(view) {
this.view = view;
this.getReadOnly = getReadOnly;
this.onMouseDown = this.onMouseDown.bind(this);
view.dom.addEventListener('mousedown', this.onMouseDown);
}
onMouseDown(event) {
if (this.getReadOnly()) return;
const pos = this.view.posAtCoords({ x: event.clientX, y: event.clientY });
if (pos == null) return;
const line = this.view.state.doc.lineAt(pos);
if (!isChecklistLine(line.text)) return;
const bracketStart = line.text.indexOf('[');
const bracketEnd = line.text.indexOf(']', bracketStart);
if (bracketStart < 0 || bracketEnd < 0) return;
const clickStart = pos - line.from;
if (clickStart < bracketStart || clickStart > bracketEnd + 1) return;
event.preventDefault();
const nextLine = toggleChecklistLine(line.text);
this.view.dispatch({
changes: { from: line.from, to: line.to, insert: nextLine },
});
}
destroy() {
this.view.dom.removeEventListener('mousedown', this.onMouseDown);
}
},
);
}
|