Procházet zdrojové kódy

enh: rich text input space behaviour

Timothy J. Baek před 6 měsíci
rodič
revize
353dc83542
1 změnil soubory, kde provedl 46 přidání a 2 odebrání
  1. 46 2
      src/lib/components/common/RichTextInput.svelte

+ 46 - 2
src/lib/components/common/RichTextInput.svelte

@@ -104,6 +104,48 @@
 
 
 	// Initialize Editor State and View
 	// 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) {
 	function isInList(state) {
 		const { $from } = state.selection;
 		const { $from } = state.selection;
 		return (
 		return (
@@ -122,7 +164,6 @@
 
 
 	onMount(() => {
 	onMount(() => {
 		const initialDoc = markdownToProseMirrorDoc(value || ''); // Convert the initial content
 		const initialDoc = markdownToProseMirrorDoc(value || ''); // Convert the initial content
-		// const initialDoc =
 
 
 		state = EditorState.create({
 		state = EditorState.create({
 			doc: initialDoc,
 			doc: initialDoc,
@@ -143,6 +184,7 @@
 					...baseKeymap,
 					...baseKeymap,
 					'Mod-z': undo,
 					'Mod-z': undo,
 					'Mod-y': redo,
 					'Mod-y': redo,
+					Space: handleSpace,
 					Enter: chainCommands(
 					Enter: chainCommands(
 						(state, dispatch, view) => {
 						(state, dispatch, view) => {
 							if (isEmptyListItem(state)) {
 							if (isEmptyListItem(state)) {
@@ -174,7 +216,9 @@
 							return liftListItem(schema.nodes.list_item)(state, dispatch);
 							return liftListItem(schema.nodes.list_item)(state, dispatch);
 						}
 						}
 						return true; // Prevent Shift-Tab from moving the focus
 						return true; // Prevent Shift-Tab from moving the focus
-					}
+					},
+					'Mod-b': toggleMark(schema.marks.strong),
+					'Mod-i': toggleMark(schema.marks.em)
 				})
 				})
 			]
 			]
 		});
 		});