Skip to content

Commit

Permalink
feat: add api for converting position to match editor mode (#2622)
Browse files Browse the repository at this point in the history
* test: add tests for convert pos api

* feat: add api for converting position to match editor mode
  • Loading branch information
jajugoguma authored Jul 26, 2022
1 parent f2a8bae commit 723c100
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
21 changes: 21 additions & 0 deletions apps/editor/src/__test__/unit/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ describe('editor', () => {
document.body.removeChild(container);
});

describe('convertPosToMatchEditorMode', () => {
const mdPos: [number, number] = [2, 1];
const wwPos = 14;

it('should convert position to match editor mode', () => {
editor.setMarkdown('Hello World\nwelcome to the world');

editor.changeMode('wysiwyg');
expect(editor.convertPosToMatchEditorMode(mdPos)).toEqual([wwPos, wwPos]);

editor.changeMode('markdown');
expect(editor.convertPosToMatchEditorMode(wwPos)).toEqual([mdPos, mdPos]);
});

it('should occurs error when types of parameters is not matched', () => {
expect(() => {
editor.convertPosToMatchEditorMode(mdPos, wwPos);
}).toThrowError();
});
});

it('setPlaceholder()', () => {
editor.setPlaceholder('Please input text');

Expand Down
29 changes: 29 additions & 0 deletions apps/editor/src/editorCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import { sanitizeHTML } from './sanitizer/htmlSanitizer';
import { createHTMLSchemaMap } from './wysiwyg/nodes/html';
import { getHTMLRenderConvertors } from './markdown/htmlRenderConvertors';
import { buildQuery } from './queries/queryManager';
import { getEditorToMdPos, getMdToEditorPos } from './markdown/helper/pos';
import { Pos } from '@t/toastmark';

/**
* ToastUIEditorCore
Expand Down Expand Up @@ -820,6 +822,33 @@ class ToastUIEditorCore {
wwEditor: this.wwEditor.getElement(),
};
}

/**
* Convert position to match editor mode
* @param {number|Array.<number>} start - start position
* @param {number|Array.<number>} end - end position
* @param {string} mode - Editor mode name of want to match converted position to
*/
convertPosToMatchEditorMode(start: EditorPos, end = start, mode = this.mode) {
const { doc } = this.mdEditor.view.state;
const isFromArray = Array.isArray(start);
const isToArray = Array.isArray(end);

let convertedFrom = start;
let convertedTo = end;

if (isFromArray !== isToArray) {
throw new Error('Types of arguments must be same');
}

if (mode === 'markdown' && !isFromArray && !isToArray) {
[convertedFrom, convertedTo] = getEditorToMdPos(doc, start as number, end as number);
} else if (mode === 'wysiwyg' && isFromArray && isToArray) {
[convertedFrom, convertedTo] = getMdToEditorPos(doc, start as Pos, end as Pos);
}

return [convertedFrom, convertedTo];
}
}

// // (Not an official API)
Expand Down

0 comments on commit 723c100

Please sign in to comment.