Skip to content

Commit

Permalink
feat: add insertHTML command
Browse files Browse the repository at this point in the history
  • Loading branch information
sibiraj-s committed Feb 17, 2021
1 parent c18d1ed commit 9d41ff0
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/lib/EditorCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
chainCommands, createParagraphNear, liftEmptyBlock,
newlineInCode, splitBlock
} from 'prosemirror-commands';
import { DOMParser } from 'prosemirror-model';

import MarkCommand from './commands/Mark';
import ListCommand from './commands/ListItem';
Expand Down Expand Up @@ -214,6 +215,20 @@ class EditorCommands {
command.toggle()(this.state, this.dispatch);
return this;
}

insertHTML(html: string): this {
const { selection, schema, tr } = this.state;
const { from, to } = selection;

const element = document.createElement('div');
element.innerHTML = html.trim();
const slice = DOMParser.fromSchema(schema).parseSlice(element);

const transaction = tr.replaceRange(from, to, slice);
this.applyTrx(transaction);

return this;
}
}

export default EditorCommands;

0 comments on commit 9d41ff0

Please sign in to comment.