Skip to content

Commit

Permalink
refactor(editor): use class property for markdownParser
Browse files Browse the repository at this point in the history
Instead of creating a new instance of MarkdownParser each time, assign it as a class property for reuse.
  • Loading branch information
phodal committed Sep 30, 2024
1 parent 66fe1b6 commit 3676a9b
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions web/core/lib/editor/action/AiActionExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BuiltinFunctionExecutor } from '@/editor/action/BuiltinFunctionExecutor
export class AiActionExecutor {
editor: Editor;
endpointUrl: string = '/api/completion';
markdownParser: MarkdownParser;

constructor() {
}
Expand All @@ -16,6 +17,8 @@ export class AiActionExecutor {
console.error('editor is null, will not set it');
return;
}

this.markdownParser = new MarkdownParser(editor, {});
this.editor = editor;
}

Expand Down Expand Up @@ -80,8 +83,7 @@ export class AiActionExecutor {
const pos = actionPosition(action, this.editor.state.selection);
this.editor.chain().focus()?.insertContentAt(pos, buffer).run();

const markdownParser = new MarkdownParser(this.editor, {});
const markdownNode = markdownParser.parse(allText);
const markdownNode = this.markdownParser.parse(allText);

this.editor.chain().focus()?.deleteRange({
from: originalSelection.from,
Expand Down Expand Up @@ -116,7 +118,8 @@ export class AiActionExecutor {

const msg = await response.text();
const posInfo = actionPosition(action, this.editor.state.selection);
this.editor.chain().focus()?.insertContentAt(posInfo, msg).run();
const node = this.markdownParser.parse(msg);
this.editor.chain().focus()?.insertContentAt(posInfo, node).run();

this.editor.setEditable(true);

Expand Down

0 comments on commit 3676a9b

Please sign in to comment.