enh: rich text input space behaviour

This commit is contained in:
Timothy J. Baek 2024-10-18 22:16:18 -07:00
parent 2fc8fa1869
commit 353dc83542

View File

@ -104,6 +104,48 @@
// Initialize Editor State and View
function handleSpace(state, dispatch) {
let { from, to, empty } = state.selection;
console.log('Space key pressed', from, to, empty);
if (dispatch) {
let tr = state.tr.insertText(' ', state.selection.from, state.selection.to);
// // After inserting space, check for any active marks at `from + 1`
const storedMarks = state.storedMarks || state.selection.$from.marks();
const hasBold = storedMarks.some((mark) => mark.type === state.schema.marks.strong);
const hasItalic = storedMarks.some((mark) => mark.type === state.schema.marks.em);
console.log('Stored marks:', storedMarks, hasBold, hasItalic);
// Step 2: Remove marks only for the space character inserted
if (hasBold) {
tr = tr.removeMark(from, from + 1, state.schema.marks.strong);
}
if (hasItalic) {
tr = tr.removeMark(from, from + 1, state.schema.marks.em);
}
// Final step: Dispatch the transaction
dispatch(tr);
}
return false;
}
function toggleMark(markType) {
return (state, dispatch) => {
const { from, to } = state.selection;
if (state.doc.rangeHasMark(from, to, markType)) {
if (dispatch) dispatch(state.tr.removeMark(from, to, markType));
return true;
} else {
if (dispatch) dispatch(state.tr.addMark(from, to, markType.create()));
return true;
}
};
}
function isInList(state) {
const { $from } = state.selection;
return (
@ -122,7 +164,6 @@
onMount(() => {
const initialDoc = markdownToProseMirrorDoc(value || ''); // Convert the initial content
// const initialDoc =
state = EditorState.create({
doc: initialDoc,
@ -143,6 +184,7 @@
...baseKeymap,
'Mod-z': undo,
'Mod-y': redo,
Space: handleSpace,
Enter: chainCommands(
(state, dispatch, view) => {
if (isEmptyListItem(state)) {
@ -174,7 +216,9 @@
return liftListItem(schema.nodes.list_item)(state, dispatch);
}
return true; // Prevent Shift-Tab from moving the focus
}
},
'Mod-b': toggleMark(schema.marks.strong),
'Mod-i': toggleMark(schema.marks.em)
})
]
});