Skip to content

Commit

Permalink
Bugfix/5650 inserting text uses model data not current view (#5651)
Browse files Browse the repository at this point in the history
* #5650 - Fix inserting component using text from the view rather than the model

* #5650 - Updated code to be better based on PR feedback

* #5650 - Made the content replacement controlled by property on the options object

---------

Co-authored-by: Wayne Mather <[email protected]>
  • Loading branch information
Wayne-Mather and Wayne Mather authored Feb 19, 2024
1 parent c2c6cdf commit 9ee52e6
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/dom_components/view/ComponentTextView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export default class ComponentTextView extends ComponentView<ComponentText> {
}
}

insertComponent(content: ComponentDefinition, opts = {}) {
insertComponent(content: ComponentDefinition, opts: any = {}) {
const { model, el } = this;
const doc = el.ownerDocument;
const selection = doc.getSelection();
Expand All @@ -182,13 +182,16 @@ export default class ComponentTextView extends ComponentView<ComponentText> {
const offset = range.startOffset;
const textModel = getComponentModel(textNode);
const newCmps: (ComponentDefinition | Component)[] = [];

const data = textNode.textContent || '';
if (textModel && textModel.is?.('textnode')) {
const cmps = textModel.collection;
cmps.forEach(cmp => {
if (cmp === textModel) {
const type = 'textnode';
const cnt = cmp.content;
let cnt = cmp.content;
if ('useDomContent' in opts && opts.useDomContent) {
cnt = data || cmp.content;
}
newCmps.push({ type, content: cnt.slice(0, offset) });
newCmps.push(content);
newCmps.push({ type, content: cnt.slice(offset) });
Expand Down

0 comments on commit 9ee52e6

Please sign in to comment.