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: add api for converting position to match editor mode #2622

Merged
merged 2 commits into from
Jul 26, 2022
Merged
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
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