This commit is contained in:
Timothy J. Baek 2024-10-19 16:18:14 -07:00
parent 73b33c3781
commit 953a8285f7
4 changed files with 42 additions and 21 deletions

View File

@ -25,7 +25,7 @@
}; };
let command = ''; let command = '';
$: command = prompt.split(' ')?.at(-1) ?? ''; $: command = prompt?.split('\n').pop()?.split(' ')?.pop() ?? '';
</script> </script>
{#if ['/', '#', '@'].includes(command?.charAt(0)) || '\\#' === command.slice(0, 2)} {#if ['/', '#', '@'].includes(command?.charAt(0)) || '\\#' === command.slice(0, 2)}

View File

@ -17,7 +17,7 @@
import { splitListItem, liftListItem, sinkListItem } from 'prosemirror-schema-list'; // Import from prosemirror-schema-list import { splitListItem, liftListItem, sinkListItem } from 'prosemirror-schema-list'; // Import from prosemirror-schema-list
import { keymap } from 'prosemirror-keymap'; import { keymap } from 'prosemirror-keymap';
import { baseKeymap, chainCommands } from 'prosemirror-commands'; import { baseKeymap, chainCommands } from 'prosemirror-commands';
import { DOMParser, DOMSerializer, Schema } from 'prosemirror-model'; import { DOMParser, DOMSerializer, Schema, Fragment } from 'prosemirror-model';
import { marked } from 'marked'; // Import marked for markdown parsing import { marked } from 'marked'; // Import marked for markdown parsing
import { dev } from '$app/environment'; import { dev } from '$app/environment';
@ -69,7 +69,10 @@
// Method to convert markdown content to ProseMirror-compatible document // Method to convert markdown content to ProseMirror-compatible document
function markdownToProseMirrorDoc(markdown: string) { function markdownToProseMirrorDoc(markdown: string) {
return defaultMarkdownParser.parse(value || ''); console.log('Markdown:', markdown);
// Parse the Markdown content into a ProseMirror document
let doc = defaultMarkdownParser.parse(markdown || '');
return doc;
} }
// Utility function to convert ProseMirror content back to markdown text // Utility function to convert ProseMirror content back to markdown text
@ -399,6 +402,7 @@
// Reinitialize the editor if the value is externally changed (i.e. when `value` is updated) // Reinitialize the editor if the value is externally changed (i.e. when `value` is updated)
$: if (view && value !== serializeEditorContent(view.state.doc)) { $: if (view && value !== serializeEditorContent(view.state.doc)) {
const newDoc = markdownToProseMirrorDoc(value || ''); const newDoc = markdownToProseMirrorDoc(value || '');
const newState = EditorState.create({ const newState = EditorState.create({
doc: newDoc, doc: newDoc,
schema, schema,
@ -407,15 +411,17 @@
}); });
view.updateState(newState); view.updateState(newState);
// After updating the state, try to find and select the next template if (value !== '') {
setTimeout(() => { // After updating the state, try to find and select the next template
const templateFound = selectNextTemplate(view.state, view.dispatch); setTimeout(() => {
if (!templateFound) { const templateFound = selectNextTemplate(view.state, view.dispatch);
// If no template found, set cursor at the end if (!templateFound) {
const endPos = view.state.doc.content.size; // If no template found, set cursor at the end
view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, endPos))); const endPos = view.state.doc.content.size;
} view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, endPos)));
}, 0); }
}, 0);
}
} }
// Destroy ProseMirror instance on unmount // Destroy ProseMirror instance on unmount

View File

@ -47,7 +47,7 @@
class=" bg-gradient-to-b via-50% from-white via-white to-transparent dark:from-gray-900 dark:via-gray-900 dark:to-transparent pointer-events-none absolute inset-0 -bottom-7 z-[-1] blur" class=" bg-gradient-to-b via-50% from-white via-white to-transparent dark:from-gray-900 dark:via-gray-900 dark:to-transparent pointer-events-none absolute inset-0 -bottom-7 z-[-1] blur"
></div> ></div>
<div class=" flex max-w-full w-full mx-auto px-5 pt-0.5 md:px-[1rem] bg-transparen"> <div class=" flex max-w-full w-full mx-auto px-2 pt-0.5 md:px-[1rem] bg-transparent">
<div class="flex items-center w-full max-w-full"> <div class="flex items-center w-full max-w-full">
<div <div
class="{$showSidebar class="{$showSidebar

View File

@ -273,20 +273,35 @@ export const findWordIndices = (text) => {
}; };
export const removeLastWordFromString = (inputString, wordString) => { export const removeLastWordFromString = (inputString, wordString) => {
// Split the string into an array of words console.log('inputString', inputString);
const words = inputString.split(' '); // Split the string by newline characters to handle lines separately
const lines = inputString.split('\n');
console.log(words.at(-1), wordString); // Take the last line to operate only on it
const lastLine = lines.pop();
// Split the last line into an array of words
const words = lastLine.split(' ');
// Conditional to check for the last word removal
if (words.at(-1) === wordString || (wordString === '' && words.at(-1) === '\\#')) { if (words.at(-1) === wordString || (wordString === '' && words.at(-1) === '\\#')) {
words.pop(); words.pop(); // Remove last word if condition is satisfied
} }
// Join the remaining words back into a string // Join the remaining words back into a string and handle space correctly
let resultString = words.join(' '); let updatedLastLine = words.join(' ');
if (resultString !== '') {
resultString += ' '; // Add a trailing space to the updated last line if there are still words
if (updatedLastLine !== '') {
updatedLastLine += ' ';
} }
// Combine the lines together again, placing the updated last line back in
const resultString = [...lines, updatedLastLine].join('\n');
// Return the final string
console.log('resultString', resultString);
return resultString; return resultString;
}; };