-
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 99a6615
Showing
3 changed files
with
151 additions
and
77 deletions.
There are no files selected for viewing
10 changes: 8 additions & 2 deletions
10
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,31 @@ | ||
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; | ||
|
||
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
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