-
Notifications
You must be signed in to change notification settings - Fork 45
Fix: reduce size of text-editor (split into plugin files) #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { Plugin, PluginKey } from 'prosemirror-state'; | ||
| import { EditorView } from 'prosemirror-view'; | ||
| import { buildContentFromDocument, buildDocumentFromContent } from './functions'; | ||
| import { diffEditor } from './diff'; | ||
| import { documentSchema } from './config'; | ||
|
|
||
| export const diffPluginKey = new PluginKey('diff'); | ||
|
|
||
| export function diffPlugin(documentId: string): Plugin { | ||
| let previewOriginalContentRef: string | null = null; | ||
| let previewActiveRef: boolean = false; | ||
| let lastPreviewContentRef: string | null = null; | ||
|
|
||
| return new Plugin({ | ||
| key: diffPluginKey, | ||
| view(editorView: EditorView) { | ||
| const handlePreviewUpdate = (event: CustomEvent) => { | ||
| if (!event.detail) return; | ||
| const { documentId: previewDocId, newContent } = event.detail; | ||
| if (previewDocId !== documentId) return; | ||
|
|
||
| if (lastPreviewContentRef === newContent) return; | ||
|
|
||
| if (!previewActiveRef) { | ||
| previewOriginalContentRef = buildContentFromDocument(editorView.state.doc); | ||
| } | ||
|
|
||
| const oldContent = previewOriginalContentRef ?? buildContentFromDocument(editorView.state.doc); | ||
| if (newContent === oldContent) return; | ||
|
|
||
| const oldDocNode = buildDocumentFromContent(oldContent); | ||
| const newDocNode = buildDocumentFromContent(newContent); | ||
|
|
||
| const diffedDoc = diffEditor(documentSchema, oldDocNode.toJSON(), newDocNode.toJSON()); | ||
|
|
||
| const tr = editorView.state.tr | ||
| .replaceWith(0, editorView.state.doc.content.size, diffedDoc.content) | ||
| .setMeta('external', true) | ||
| .setMeta('addToHistory', false); | ||
|
|
||
| requestAnimationFrame(() => editorView.dispatch(tr)); | ||
|
|
||
| previewActiveRef = true; | ||
| lastPreviewContentRef = newContent; | ||
| }; | ||
|
|
||
| const handleCancelPreview = (event: CustomEvent) => { | ||
| if (!event.detail) return; | ||
| const { documentId: cancelDocId } = event.detail; | ||
| if (cancelDocId !== documentId) return; | ||
| if (!previewActiveRef || previewOriginalContentRef === null) return; | ||
|
|
||
| const originalDocNode = buildDocumentFromContent(previewOriginalContentRef); | ||
| const tr = editorView.state.tr.replaceWith(0, editorView.state.doc.content.size, originalDocNode.content); | ||
| editorView.dispatch(tr); | ||
|
|
||
| previewActiveRef = false; | ||
| previewOriginalContentRef = null; | ||
| lastPreviewContentRef = null; | ||
| }; | ||
|
|
||
| const handleApply = (event: CustomEvent) => { | ||
| if (!event.detail) return; | ||
| const { documentId: applyDocId } = event.detail; | ||
| if (applyDocId !== documentId) return; | ||
|
|
||
| const animationDuration = 500; | ||
|
|
||
| const finalizeApply = async () => { | ||
| const { state } = editorView; | ||
| let tr = state.tr; | ||
| const diffMarkType = state.schema.marks.diffMark; | ||
| const { DiffType } = await import('./diff'); | ||
|
|
||
| const rangesToDelete: { from: number; to: number }[] = []; | ||
| state.doc.descendants((node, pos) => { | ||
| if (!node.isText) return; | ||
|
|
||
| const deletedMark = node.marks.find( | ||
| (mark) => mark.type === diffMarkType && mark.attrs.type === DiffType.Deleted | ||
| ); | ||
| if (deletedMark) { | ||
| rangesToDelete.push({ from: pos, to: pos + node.nodeSize }); | ||
| } | ||
| }); | ||
|
|
||
| for (let i = rangesToDelete.length - 1; i >= 0; i--) { | ||
| const { from, to } = rangesToDelete[i]; | ||
| tr.delete(from, to); | ||
| } | ||
|
|
||
| tr.removeMark(0, tr.doc.content.size, diffMarkType); | ||
| tr.setMeta('addToHistory', false); | ||
| editorView.dispatch(tr); | ||
| editorView.dom.classList.remove('applying-changes'); | ||
|
|
||
| previewActiveRef = false; | ||
| previewOriginalContentRef = null; | ||
| lastPreviewContentRef = null; | ||
| }; | ||
|
|
||
| editorView.dom.classList.add('applying-changes'); | ||
| setTimeout(finalizeApply, animationDuration); | ||
| }; | ||
|
|
||
| window.addEventListener('preview-document-update', handlePreviewUpdate as EventListener); | ||
| window.addEventListener('cancel-document-update', handleCancelPreview as EventListener); | ||
| window.addEventListener('apply-document-update', handleApply as EventListener); | ||
|
|
||
| return { | ||
| destroy() { | ||
| window.removeEventListener('preview-document-update', handlePreviewUpdate as EventListener); | ||
| window.removeEventListener('cancel-document-update', handleCancelPreview as EventListener); | ||
| window.removeEventListener('apply-document-update', handleApply as EventListener); | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { exampleSetup } from 'prosemirror-example-setup'; | ||
| import { inputRules } from 'prosemirror-inputrules'; | ||
| import { Plugin } from 'prosemirror-state'; | ||
|
|
||
| import { documentSchema, headingRule } from './config'; | ||
| import { creationStreamingPlugin } from './creation-streaming-plugin'; | ||
| import { placeholderPlugin } from './placeholder-plugin'; | ||
| import { inlineSuggestionPlugin } from './inline-suggestion-plugin'; | ||
| import { selectionContextPlugin } from './suggestion-plugin'; | ||
| import { synonymsPlugin } from './synonym-plugin'; | ||
| import { diffPlugin } from './diff-plugin'; | ||
| import { formatPlugin } from './format-plugin'; | ||
| import { savePlugin } from './save-plugin'; | ||
|
|
||
| export interface EditorPluginOptions { | ||
| documentId: string; | ||
| initialLastSaved: Date | null; | ||
| placeholder?: string; | ||
| performSave: (content: string) => Promise<any>; | ||
| requestInlineSuggestion: (state: any) => void; | ||
| setActiveFormats: (formats: any) => void; | ||
| } | ||
|
|
||
| export function createEditorPlugins(opts: EditorPluginOptions): Plugin[] { | ||
| return [ | ||
| creationStreamingPlugin(opts.documentId), | ||
| placeholderPlugin(opts.placeholder ?? (opts.documentId === 'init' ? 'Start typing' : 'Start typing...')), | ||
| ...exampleSetup({ schema: documentSchema, menuBar: false }), | ||
| inputRules({ | ||
| rules: [1, 2, 3, 4, 5, 6].map((level) => headingRule(level)), | ||
| }), | ||
| inlineSuggestionPlugin({ requestSuggestion: opts.requestInlineSuggestion }), | ||
| selectionContextPlugin(opts.documentId), | ||
| synonymsPlugin(), | ||
| diffPlugin(opts.documentId), | ||
| formatPlugin(opts.setActiveFormats), | ||
| savePlugin({ | ||
| saveFunction: opts.performSave, | ||
| initialLastSaved: opts.initialLastSaved, | ||
| debounceMs: 200, | ||
| documentId: opts.documentId, | ||
| }), | ||
| ]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { Plugin, PluginKey, EditorState } from 'prosemirror-state'; | ||
| import { EditorView } from 'prosemirror-view'; | ||
| import { documentSchema } from './config'; | ||
|
|
||
| const { nodes, marks } = documentSchema; | ||
|
|
||
| export interface FormatState { | ||
| h1: boolean; | ||
| h2: boolean; | ||
| p: boolean; | ||
| bulletList: boolean; | ||
| orderedList: boolean; | ||
| bold: boolean; | ||
| italic: boolean; | ||
| } | ||
|
|
||
| export const formatPluginKey = new PluginKey<FormatState>('format'); | ||
|
|
||
| function isMarkActive(state: EditorState, type: any): boolean { | ||
| const { from, $from, to, empty } = state.selection; | ||
| if (empty) { | ||
| return !!type.isInSet(state.storedMarks || $from.marks()); | ||
| } else { | ||
| return state.doc.rangeHasMark(from, to, type); | ||
| } | ||
| } | ||
|
|
||
| function isBlockActive(state: EditorState, type: any, attrs: Record<string, any> = {}): boolean { | ||
| const { $from } = state.selection; | ||
| const node = $from.node($from.depth); | ||
| return node?.hasMarkup(type, attrs); | ||
| } | ||
|
|
||
| function isListActive(state: EditorState, type: any): boolean { | ||
| const { $from } = state.selection; | ||
| for (let d = $from.depth; d > 0; d--) { | ||
| if ($from.node(d).type === type) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| function getActiveFormats(state: EditorState): FormatState { | ||
| return { | ||
| h1: isBlockActive(state, nodes.heading, { level: 1 }), | ||
| h2: isBlockActive(state, nodes.heading, { level: 2 }), | ||
| p: isBlockActive(state, nodes.paragraph), | ||
| bulletList: isListActive(state, nodes.bullet_list), | ||
| orderedList: isListActive(state, nodes.ordered_list), | ||
| bold: isMarkActive(state, marks.strong), | ||
| italic: isMarkActive(state, marks.em), | ||
| }; | ||
| } | ||
|
|
||
| export function formatPlugin(onFormatChange: (formats: FormatState) => void): Plugin<FormatState> { | ||
| return new Plugin<FormatState>({ | ||
| key: formatPluginKey, | ||
| state: { | ||
| init(_, state): FormatState { | ||
| return getActiveFormats(state); | ||
| }, | ||
| apply(tr, pluginState, oldState, newState): FormatState { | ||
| if (tr.selectionSet || tr.docChanged) { | ||
| return getActiveFormats(newState); | ||
| } | ||
| return pluginState; | ||
| }, | ||
| }, | ||
| view(editorView: EditorView) { | ||
| const initialState = formatPluginKey.getState(editorView.state); | ||
| if (initialState) { | ||
| onFormatChange(initialState); | ||
| } | ||
|
|
||
| return { | ||
| update(view: EditorView, prevState: EditorState) { | ||
| const newState = formatPluginKey.getState(view.state); | ||
| const oldState = formatPluginKey.getState(prevState); | ||
|
|
||
| if (newState && oldState && newState !== oldState) { | ||
| onFormatChange(newState); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Replace 'any' types with more specific types.
Using
anyreduces type safety. Consider defining proper types for these parameters.Would you like me to help define the proper types based on your plugin implementations?
📝 Committable suggestion
🤖 Prompt for AI Agents