Skip to content

Commit

Permalink
[#68928] sync checkboxes between preview and editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Trzcin committed Nov 28, 2024
1 parent 472e983 commit ac681f3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/MystEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ResolvedComments from "./components/Resolved";
import { handlePreviewClickToScroll } from "./extensions/syncDualPane";
import { createMystState, MystState, predefinedButtons, defaultButtons } from "./mystState";
import { batch, computed, signal, effect } from "@preact/signals";
import { syncCheckboxes } from "./hooks/markdownCheckboxes";

const EditorParent = styled.div`
font-family: "Lato";
Expand Down Expand Up @@ -183,6 +184,7 @@ const MystEditor = () => {
ref={preview}
mode={options.mode.value}
onClick={(ev) => {
syncCheckboxes(ev, text.lineMap, editorView.value);
if (options.syncScroll.value && options.mode.value == "Both")
handlePreviewClickToScroll(ev, text.lineMap, preview, editorView.value);
}}
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/syncDualPane.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function scrollPreviewElemIntoView({ view, matchingLine, matchingElem, behavior
* @param {{ target: HTMLElement }} ev
* @param {{ current: Map<number, string> }} lineMap
* @param {{ current: HTMLElement }} preview
* @param { current: EditorView } editor
* @param { EditorView } editor
*/
export function handlePreviewClickToScroll(ev, lineMap, preview, editor) {
let id = ev.target.getAttribute("data-line-id");
Expand Down
24 changes: 24 additions & 0 deletions src/hooks/markdownCheckboxes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getLineById } from "./markdownSourceMap";
import { EditorView } from "codemirror";

/**
* @param {{ target: HTMLElement }} ev
* @param {{ current: Map<number, string> }} lineMap
* @param { EditorView } editor
*/
export function syncCheckboxes(ev, lineMap, editor) {
if (ev.target.tagName != "INPUT") return;
const id = ev.target.getAttribute("data-line-id");
const lineNumber = getLineById(lineMap.current, id);
const line = editor.state.doc.line(lineNumber);
const openIdx = line.text.indexOf("[");
const closeIdx = line.text.indexOf("]");

editor.dispatch({
changes: {
from: line.from + openIdx + 1,
to: line.from + closeIdx,
insert: ev.target.checked ? "x" : " ",
},
});
}

0 comments on commit ac681f3

Please sign in to comment.