Skip to content

Commit

Permalink
feat(Configurator): Support set keepMarks in props to decide whethe…
Browse files Browse the repository at this point in the history
…r to keep marks after split in

close #145
  • Loading branch information
lastnigtic committed Jun 13, 2022
1 parent 6685028 commit b5a0c19
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/adapter/src/basic/basic-ctrl/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
interface IBasicCtrlConfig {
placeholder?: string;
keepLastLine?: boolean;
keepMarks?: boolean;
dropCursor?: TDropCursorConfig;
keepWhiteSpace?: boolean | 'full';
}
Expand Down
57 changes: 37 additions & 20 deletions packages/adapter/src/basic/keymap/keymap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { TKeymapHandler } from '../..';
import { SylApi } from '../../api';
import { filterData, groupData, toArray, Types } from '../../libs';
import { createCtrlPlugin } from '../ctrl-plugin';
import { splitBlock } from '.';
import {
clearAtHead,
deleteSelection,
Expand Down Expand Up @@ -48,25 +49,41 @@ const backspace = chainCommands(
deleteZeroChar,
);

const enter = chainCommands(newlineInCode, createParagraphNear, liftEmptyBlock, splitBlockKeepMarks);

const basicKeymapPlugin = new Plugin({
key: BASIC_KEYMAP_KEY,
props: {
handleKeyDown: keydownHandler({
Enter: enter,
'Mod-z': undo,
'Mod-y': redo,
'Shift-Mod-z': redo,
Backspace: backspace,
'Alt-ArrowUp': joinUp,
'Alt-ArrowDown': joinDown,
'Mod-Bracketleft': lift,
Escape: selectParentNode,
'Shift-Enter': insertBreak,
}),
},
});
const createBasicKeymapPlugin = (config: { keepMarks: boolean }) => {
let keepMarks = config.keepMarks;
return new Plugin({
key: BASIC_KEYMAP_KEY,
state: {
init() {
return null;
},
apply(tr, val) {
const data = tr.getMeta(BASIC_KEYMAP_KEY);
if (data?.keepMarks !== undefined) {
keepMarks = Boolean(data.keepMarks);
}
if (data) return data;
return val;
},
},
props: {
handleKeyDown: keydownHandler({
Enter: chainCommands(newlineInCode, createParagraphNear, liftEmptyBlock, (state, dispatch) =>
keepMarks ? splitBlockKeepMarks(state, dispatch) : splitBlock(state, dispatch),
),
'Mod-z': undo,
'Mod-y': redo,
'Shift-Mod-z': redo,
Backspace: backspace,
'Alt-ArrowUp': joinUp,
'Alt-ArrowDown': joinDown,
'Mod-Bracketleft': lift,
Escape: selectParentNode,
'Shift-Enter': insertBreak,
}),
},
});
};

const defaultKeymapPlugin = new Plugin({
key: DEFAULT_KEYMAP_KEY,
Expand Down Expand Up @@ -126,8 +143,8 @@ const createCustomKeymapPlugins = (adapter: SylApi, customKeyMaps?: Array<TSylKe

export {
BASIC_KEYMAP_KEY,
basicKeymapPlugin,
composeKeymap,
createBasicKeymapPlugin,
createCustomKeymapPlugins,
CUSTOM_KEYMAP_KEY,
DEFAULT_KEYMAP_KEY,
Expand Down
16 changes: 13 additions & 3 deletions packages/adapter/src/configurator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import { BasicCtrlPlugin, BSControlKey, IBasicCtrlConfig } from './basic/basic-c
import { ICtrlPlugin } from './basic/ctrl-plugin';
import { createCustomCtrlPlugin, CUSTOM_CTRL_ACCEPT, ICustomCtrlConfig } from './basic/custom-ctrl';
import { DecorationPlugin } from './basic/decoration';
import { basicKeymapPlugin, createCustomKeymapPlugins, defaultKeymapPlugin, TSylKeymap } from './basic/keymap';
import {
BASIC_KEYMAP_KEY,
createBasicKeymapPlugin,
createCustomKeymapPlugins,
defaultKeymapPlugin,
TSylKeymap,
} from './basic/keymap';
import { createLifeCyclePlugin } from './basic/lifecycle/lifecycle-plugin';
import { ruleBuilder } from './basic/text-shortcut/rule-builder';
import { SHORTCUT_KEY } from './basic/text-shortcut/shortcut-plugin';
Expand Down Expand Up @@ -133,6 +139,7 @@ class SylConfigurator {
// configuration that pass to BasicCtrlPlugin
public basicConfiguration: Required<IBasicCtrlConfig> = {
keepLastLine: true,
keepMarks: true,
dropCursor: {},
placeholder: '',
keepWhiteSpace: false,
Expand Down Expand Up @@ -269,7 +276,7 @@ class SylConfigurator {
this.customCtrlPlugin!,
// decrease the priority of the `keymap`, because `handleKeyDown` can handle more things
this.customKeyMapPlugin!,
basicKeymapPlugin,
createBasicKeymapPlugin(this.basicConfiguration),
defaultKeymapPlugin,
DecorationPlugin(),
BasicCtrlPlugin(this.basicConfiguration, !this.baseConfiguration.disable),
Expand Down Expand Up @@ -352,9 +359,12 @@ class SylConfigurator {
});

private setBasicCtrlConfiguration = (config: IConfiguration) =>
setConfiguration(this.basicConfiguration, config, key => {
setConfiguration(this.basicConfiguration, config, (key, val) => {
if (key === 'placeholder' && this.view) {
this.view.updateState(this.view.state);
} else if (key === 'keepMarks' && this.view) {
const { state, dispatch } = this.view;
dispatch(state.tr.setMeta(BASIC_KEYMAP_KEY, { keepMarks: val }));
}
});

Expand Down
20 changes: 20 additions & 0 deletions test/case/adapter/configurator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,26 @@ describe('Controller config test', () => {
expect(isPass).toBe(true);
});

test('support update keepMarks config', async () => {
await page.evaluate(() => {
editor.setHTML('<p><strong>a</strong></p>');
editor.setSelection({ index: 2 });
});

await page.keyboard.press('Enter');
let isMarkActive = await page.evaluate(() => editor.isActive('bold'));
expect(isMarkActive).toBe(true);

await page.evaluate(() => {
editor.configurator.update({ keepMarks: false });
editor.setHTML('<p><strong>a</strong></p>');
editor.setSelection({ index: 2 });
});
await page.keyboard.press('Enter');
isMarkActive = await page.evaluate(() => editor.isActive('bold'));
expect(isMarkActive).toBe(false);
});

test('support update other plugin props', async () => {
const isPass = await page.evaluate(() => {
editor.configurator.setCustomConfiguration({
Expand Down

0 comments on commit b5a0c19

Please sign in to comment.