Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added a focus parameter to the setMarkdown and setHTML functions #2030

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions apps/editor/src/editorCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ class ToastUIEditorCore {

this.setHeight(this.options.height);

this.setMarkdown(this.options.initialValue, false);
this.setMarkdown(this.options.initialValue, false, this.options.autofocus);

if (this.options.placeholder) {
this.setPlaceholder(this.options.placeholder);
}

if (!this.options.initialValue) {
this.setHTML(this.initialHTML, false);
this.setHTML(this.initialHTML, false, this.options.autofocus);
}

this.commandManager = new CommandManager(
Expand Down Expand Up @@ -433,8 +433,8 @@ class ToastUIEditorCore {
* @param {string} markdown - markdown syntax text.
* @param {boolean} [cursorToEnd=true] - move cursor to contents end
*/
setMarkdown(markdown = '', cursorToEnd = true) {
this.mdEditor.setMarkdown(markdown, cursorToEnd);
setMarkdown(markdown = '', cursorToEnd = true, focus = true) {
this.mdEditor.setMarkdown(markdown, cursorToEnd, focus);

if (this.isWysiwygMode()) {
const mdNode = this.toastMark.getRootNode();
Expand All @@ -449,15 +449,15 @@ class ToastUIEditorCore {
* @param {string} html - html syntax text
* @param {boolean} [cursorToEnd=true] - move cursor to contents end
*/
setHTML(html = '', cursorToEnd = true) {
setHTML(html = '', cursorToEnd = true, focus = true) {
const container = document.createElement('div');

// the `br` tag should be replaced with empty block to separate between blocks
container.innerHTML = replaceBRWithEmptyBlock(html);
const wwNode = DOMParser.fromSchema(this.wwEditor.schema).parse(container);

if (this.isMarkdownMode()) {
this.mdEditor.setMarkdown(this.convertor.toMarkdownText(wwNode), cursorToEnd);
this.mdEditor.setMarkdown(this.convertor.toMarkdownText(wwNode), cursorToEnd, focus);
} else {
this.wwEditor.setModel(wwNode, cursorToEnd);
}
Expand Down
4 changes: 2 additions & 2 deletions apps/editor/src/markdown/mdEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export default class MdEditor extends EditorBase {
return getEditorToMdPos(this.view.state.tr.doc, from, to);
}

setMarkdown(markdown: string, cursorToEnd = true) {
setMarkdown(markdown: string, cursorToEnd = true, focus = true) {
const lineTexts = markdown.split(reLineEnding);
const { tr, doc, schema } = this.view.state;
const nodes = lineTexts.map((lineText) =>
Expand All @@ -332,7 +332,7 @@ export default class MdEditor extends EditorBase {
this.view.dispatch(tr.replaceWith(0, doc.content.size, nodes));

if (cursorToEnd) {
this.moveCursorToEnd(true);
this.moveCursorToEnd(focus);
}
}

Expand Down
4 changes: 2 additions & 2 deletions apps/editor/types/editor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ export class EditorCore {

moveCursorToStart(focus?: boolean): void;

setMarkdown(markdown: string, cursorToEnd?: boolean): void;
setMarkdown(markdown: string, cursorToEnd?: boolean, focus?: boolean): void;

setHTML(html: string, cursorToEnd?: boolean): void;
setHTML(html: string, cursorToEnd?: boolean, focus?: boolean): void;

getMarkdown(): string;

Expand Down