Enhance replaceText logic for CKEditor
This commit is contained in:
parent
dde1010026
commit
374dd0045a
@ -536,24 +536,36 @@ class BesEditor {
|
|||||||
// This function should be able to handle both cases or find a way that works for both.
|
// This function should be able to handle both cases or find a way that works for both.
|
||||||
static replaceText(el, rect, match, replacement, editor) {
|
static replaceText(el, rect, match, replacement, editor) {
|
||||||
// const tags = this.getTagsAndText(el)
|
// const tags = this.getTagsAndText(el)
|
||||||
|
if (!editor.CKEditorInstance) {
|
||||||
const text = el.textContent
|
const text = el.textContent
|
||||||
const newText =
|
const newText =
|
||||||
text.substring(0, match.offset) +
|
text.substring(0, match.offset) +
|
||||||
replacement +
|
replacement +
|
||||||
text.substring(match.offset + match.length)
|
text.substring(match.offset + match.length)
|
||||||
el.textContent = newText
|
el.textContent = newText
|
||||||
if (editor.CKEditorInstance) {
|
} else {
|
||||||
const { CKEditorInstance } = editor
|
const { CKEditorInstance } = editor
|
||||||
CKEditorInstance.model.change(writer => {
|
CKEditorInstance.model.change(writer => {
|
||||||
// Find the corresponding element in the model.
|
|
||||||
const viewElement =
|
const viewElement =
|
||||||
CKEditorInstance.editing.view.domConverter.mapDomToView(el)
|
CKEditorInstance.editing.view.domConverter.mapDomToView(el)
|
||||||
const modelElement =
|
const modelElement =
|
||||||
CKEditorInstance.editing.mapper.toModelElement(viewElement)
|
CKEditorInstance.editing.mapper.toModelElement(viewElement)
|
||||||
|
|
||||||
if (modelElement) {
|
if (modelElement) {
|
||||||
writer.remove(writer.createRangeIn(modelElement))
|
const elementRange = writer.createRangeIn(modelElement)
|
||||||
writer.insertText(newText, modelElement, 'end')
|
// TODO: This logic should work once the HTML tags are removed from match.offset and match.length if is possible.
|
||||||
|
if (
|
||||||
|
elementRange.start.offset <= match.offset &&
|
||||||
|
elementRange.end.offset >= match.offset + match.length
|
||||||
|
) {
|
||||||
|
const start = writer.createPositionAt(modelElement, match.offset)
|
||||||
|
const end = writer.createPositionAt(
|
||||||
|
modelElement,
|
||||||
|
match.offset + match.length
|
||||||
|
)
|
||||||
|
const range = writer.createRange(start, end)
|
||||||
|
writer.remove(range)
|
||||||
|
writer.insertText(replacement, start)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user