feat(engine):#87 增加 setMarkdown 方法

This commit is contained in:
yanmao 2022-02-13 21:05:36 +08:00
parent bc102a139b
commit df3c9d62ab
4 changed files with 59 additions and 0 deletions

View File

@ -250,6 +250,34 @@ class ChangeModel implements ChangeInterface {
);
}
setMarkdown(text: string, callback?: (count: number) => void) {
const textNode = $(document.createTextNode(text));
this.engine.trigger('paste:markdown-before', textNode);
this.engine.trigger('paste:markdown', textNode);
this.engine.trigger('paste:markdown-after', textNode);
const { card, container } = this.engine;
textNode.get<Text>()?.normalize();
this.#nativeEvent.paste(
textNode.text(),
undefined,
callback,
true,
(
fragment: DocumentFragment,
_range?: RangeInterface,
_rangeCallback?: (range: RangeInterface) => void,
_followActiveMark?: boolean,
) => {
container.empty().append(fragment);
card.render(undefined, (count) => {
this.initValue(undefined, false);
this.engine.trigger('paste:after');
if (callback) callback(count);
});
},
);
}
getOriginValue(container: NodeInterface = this.engine.container) {
const { schema, conversion } = this.engine;
return new Parser(

View File

@ -203,6 +203,22 @@ class Engine<T extends EngineOptions = EngineOptions>
return this;
}
setMarkdown(text: string, callback?: (count: number) => void) {
this.change.setMarkdown(text, (count) => {
this.normalize();
this.container.allChildren(true).forEach((child) => {
if (this.node.isInline(child)) {
this.inline.repairCursor(child);
} else if (this.node.isMark(child)) {
this.mark.repairCursor(child);
}
});
if (callback) callback(count);
});
this.nodeId.generateAll(this.container);
return this;
}
setJsonValue(value: Array<any>, callback?: (count: number) => void) {
const dom = $(toDOM(value));
const attributes = dom.get<Element>()?.attributes;

View File

@ -239,6 +239,12 @@ export interface ChangeInterface {
* @param callback
*/
setHtml(html: string, callback?: (count: number) => void): void;
/**
* markdown
* @param text markdown文本
* @param callback
*/
setMarkdown(text: string, callback?: (count: number) => void): void;
/**
*
*/

View File

@ -156,6 +156,15 @@ export interface EngineInterface<T extends EngineOptions = EngineOptions>
* @param callback
*/
setHtml(html: string, callback?: (count: number) => void): EngineInterface;
/**
* markdown
* @param text markdown文本
* @param callback
*/
setMarkdown(
text: string,
callback?: (count: number) => void,
): EngineInterface;
/**
* json格式值
* @param callback