-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(text editor): more explicit checking of triggers and positions
- Loading branch information
1 parent
b823e7d
commit a07e08b
Showing
3 changed files
with
164 additions
and
76 deletions.
There are no files selected for viewing
15 changes: 13 additions & 2 deletions
15
src/components/text-editor/prosemirror-adapter/plugins/trigger/create-html-inserter.ts
This file contains 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 |
---|---|---|
@@ -1,25 +1,36 @@ | ||
import { Node, DOMParser, Fragment } from 'prosemirror-model'; | ||
import { EditorView } from 'prosemirror-view'; | ||
import { ContentTypeConverter } from '../../../utils/content-type-converter'; | ||
import { TriggerCharacter } from 'src/interface'; | ||
import { findTriggerPosition } from './factory'; | ||
|
||
export const createHtmlInserter = ( | ||
view: EditorView, | ||
contentConverter: ContentTypeConverter, | ||
startPos: number, | ||
triggerCharacter: TriggerCharacter, | ||
dispatchTransaction: ( | ||
view: EditorView, | ||
startPos: number, | ||
fragment: Fragment | Node, | ||
) => void, | ||
): ((input: string) => Promise<void>) => { | ||
const schema = view.state.schema; | ||
const state = view.state; | ||
|
||
const foundTrigger = findTriggerPosition(state, triggerCharacter); | ||
const position = foundTrigger?.position; | ||
|
||
if (!position) { | ||
console.error('Could not find the trigger'); | ||
return; | ||
} | ||
|
||
return async (input: string): Promise<void> => { | ||
const container = document.createElement('span'); | ||
container.innerHTML = await contentConverter.parseAsHTML(input, schema); | ||
|
||
const fragment = DOMParser.fromSchema(schema).parse(container).content; | ||
|
||
dispatchTransaction(view, startPos, fragment); | ||
dispatchTransaction(view, position, fragment); | ||
}; | ||
}; |
This file contains 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
Oops, something went wrong.