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 = '';
$: command = prompt.split(' ')?.at(-1) ?? '';
$: command = prompt?.split('\n').pop()?.split(' ')?.pop() ?? '';
</script>
{#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 { keymap } from 'prosemirror-keymap';
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 { dev } from '$app/environment';
@ -69,7 +69,10 @@
// Method to convert markdown content to ProseMirror-compatible document
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
@ -399,6 +402,7 @@
// Reinitialize the editor if the value is externally changed (i.e. when `value` is updated)
$: if (view && value !== serializeEditorContent(view.state.doc)) {
const newDoc = markdownToProseMirrorDoc(value || '');
const newState = EditorState.create({
doc: newDoc,
schema,
@ -407,15 +411,17 @@
});
view.updateState(newState);
// After updating the state, try to find and select the next template
setTimeout(() => {
const templateFound = selectNextTemplate(view.state, view.dispatch);
if (!templateFound) {
// If no template found, set cursor at the end
const endPos = view.state.doc.content.size;
view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, endPos)));
}
}, 0);
if (value !== '') {
// After updating the state, try to find and select the next template
setTimeout(() => {
const templateFound = selectNextTemplate(view.state, view.dispatch);
if (!templateFound) {
// If no template found, set cursor at the end
const endPos = view.state.doc.content.size;
view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, endPos)));
}
}, 0);
}
}
// 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"
></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="{$showSidebar

View File

@ -273,20 +273,35 @@ export const findWordIndices = (text) => {
};
export const removeLastWordFromString = (inputString, wordString) => {
// Split the string into an array of words
const words = inputString.split(' ');
console.log('inputString', inputString);
// 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) === '\\#')) {
words.pop();
words.pop(); // Remove last word if condition is satisfied
}
// Join the remaining words back into a string
let resultString = words.join(' ');
if (resultString !== '') {
resultString += ' ';
// Join the remaining words back into a string and handle space correctly
let updatedLastLine = words.join(' ');
// 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;
};