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

Add option indent list #1465

Merged
merged 7 commits into from
Mar 29, 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
7 changes: 7 additions & 0 deletions .changeset/aafraid-stingrays-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@udecode/plate-indent-list": minor
---

- indent-list plugin:
- new option: `getSiblingIndentListOptions` which is used by normalizers to get list siblings (e.g. for `listStart`).
- normalizer handling a few more cases
7 changes: 7 additions & 0 deletions .changeset/tricky-fishes-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@udecode/plate-core": minor
---

- `withoutNormalizing`: `Editor.withoutNormalizing` which returns true if normalized
- `createPlateEditor`: add `normalizeInitialValue` option
- `createPlateTestEditor`
6 changes: 4 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
module.exports = {
collectCoverageFrom: [
'packages/*/src/**/*',
'packages/**/src/**/*.{ts,tsx}',
'!**/*.styles.ts*',
'!**/index.ts*',
'!**/*test*/**',
'!**/*fixture*/**',
'!**/*template*/**',
'!**/*stories*',
'!**/*.development.*',
],
rootDir: '.',
globals: {
'ts-jest': {
tsconfig: 'tsconfig.test.json',
Expand All @@ -18,7 +20,7 @@ module.exports = {
moduleNameMapper: {
'\\.(css|less|sass|scss)$': '<rootDir>/scripts/styleMock.js',
// '^@udecode/plate-ui-dnd$': '<rootDir>/packages/dnd/src',
'^@udecode/plate-common$': '<rootDir>/packages/common/src',
// '^@udecode/plate-common$': '<rootDir>/packages/common/src',
// '^@udecode/plate-basic-elements$':
// '<rootDir>/packages/nodes/basic-elements/src',
// '^@udecode/plate-alignment$': '<rootDir>/packages/nodes/alignment/src',
Expand Down
24 changes: 24 additions & 0 deletions packages/core/src/common/__tests__/createPlateTestEditor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { buildTestHarness } from 'slate-test-utils';
import { PlateTest } from '../../components/PlateTest';
import {
createPlateEditor,
CreatePlateEditorOptions,
} from '../../utils/createPlateEditor';

/**
* `buildTestHarness` where:
* - `Component`: `PlateTest`
* - `editor`: `createPlateEditor`
*/
export const createPlateTestEditor = async (
options: CreatePlateEditorOptions,
buildTestHarnessOptions?: Omit<
Parameters<ReturnType<typeof buildTestHarness>>[0],
'editor'
>
) => {
return buildTestHarness(PlateTest)({
editor: createPlateEditor(options),
...buildTestHarnessOptions,
});
};
1 change: 1 addition & 0 deletions packages/core/src/common/transforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export * from './toggleNodeType';
export * from './toggleWrapNodes';
export * from './unhangRange';
export * from './unwrapNodes';
export * from './withoutNormalizing';
export * from './wrapNodes';
19 changes: 19 additions & 0 deletions packages/core/src/common/transforms/withoutNormalizing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Editor } from 'slate';
import { TEditor } from '../../types/slate/TEditor';

/**
* {@link Editor.withoutNormalizing}
* @return true if normalized.
*/
export const withoutNormalizing = (
editor: TEditor,
fn: () => boolean | void
) => {
let normalized = false;

Editor.withoutNormalizing(editor, () => {
normalized = !!fn();
});

return normalized;
};
2 changes: 2 additions & 0 deletions packages/core/src/components/PlateTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Plate, PlateProps } from './Plate';
export const PlateTest = ({
variant = 'wordProcessor',
editableProps,
normalizeInitialValue,
...props
}: {
variant?: 'comment' | 'wordProcessor';
Expand All @@ -18,6 +19,7 @@ export const PlateTest = ({
editor,
plugins,
id,
normalizeInitialValue,
});
}

Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/utils/createPlateEditor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createEditor } from 'slate';
import { createEditor, Editor } from 'slate';
import { withPlate, WithPlateOptions } from '../plugins/withPlate';
import { OverrideByKey } from '../types/OverrideByKey';
import { PlateEditor } from '../types/PlateEditor';
Expand All @@ -12,6 +12,7 @@ export interface CreatePlateEditorOptions<T = {}>
plugins?: PlatePlugin<T>[];
components?: Record<string, PlatePluginComponent>;
overrideByKey?: OverrideByKey<T>;
normalizeInitialValue?: boolean;
}

/**
Expand All @@ -25,15 +26,22 @@ export const createPlateEditor = <T = {}>({
plugins = [],
components,
overrideByKey,
normalizeInitialValue,
...withPlateOptions
}: CreatePlateEditorOptions<T> = {}): PlateEditor<T> => {
plugins = createPlugins(plugins, {
components,
overrideByKey,
});

return withPlate(editor, {
editor = withPlate(editor, {
plugins,
...withPlateOptions,
});

if (normalizeInitialValue) {
Editor.normalize(editor, { force: true });
}

return editor;
};
7 changes: 6 additions & 1 deletion packages/nodes/indent-list/src/createIndentListPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { createPluginFactory } from '@udecode/plate-core';
import { GetSiblingIndentListOptions } from './queries/getSiblingIndentList';
import { injectIndentListComponent } from './injectIndentListComponent';
import { withIndentList } from './withIndentList';

export const KEY_LIST_STYLE_TYPE = 'listStyleType';
export const KEY_LIST_START = 'listStart';

export const createIndentListPlugin = createPluginFactory({
export interface IndentListPlugin {
getSiblingIndentListOptions?: GetSiblingIndentListOptions;
}

export const createIndentListPlugin = createPluginFactory<IndentListPlugin>({
key: KEY_LIST_STYLE_TYPE,
inject: {
belowComponent: injectIndentListComponent,
Expand Down
2 changes: 2 additions & 0 deletions packages/nodes/indent-list/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

export * from './createIndentListPlugin';
export * from './injectIndentListComponent';
export * from './normalizeIndentList';
export * from './types';
export * from './withIndentList';
export * from './normalizers/index';
export * from './queries/index';
export * from './transforms/index';
29 changes: 29 additions & 0 deletions packages/nodes/indent-list/src/normalizeIndentList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { TEditor, withoutNormalizing } from '@udecode/plate-core';
import { NodeEntry } from 'slate';
import { normalizeIndentListNotIndented } from './normalizers/normalizeIndentListNotIndented';
import { normalizeIndentListStart } from './normalizers/normalizeIndentListStart';
import { IndentListPlugin } from './createIndentListPlugin';

export const normalizeIndentList = (
editor: TEditor,
{ getSiblingIndentListOptions }: IndentListPlugin = {}
) => {
const { normalizeNode } = editor;

return ([node, path]: NodeEntry) => {
const normalized = withoutNormalizing(editor, () => {
if (normalizeIndentListNotIndented(editor, [node, path])) return true;
if (
normalizeIndentListStart(
editor,
[node, path],
getSiblingIndentListOptions
)
)
return true;
});
if (normalized) return;

return normalizeNode([node, path]);
};
};
7 changes: 7 additions & 0 deletions packages/nodes/indent-list/src/normalizers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @file Automatically generated by barrelsby.
*/

export * from './normalizeFirstIndentListStart';
export * from './normalizeIndentListNotIndented';
export * from './normalizeIndentListStart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/** @jsx jsx */

import { createPlateEditor } from '@udecode/plate-core';
import { createIndentPlugin } from '@udecode/plate-indent';
import { jsx } from '@udecode/plate-test-utils';
import { Editor } from 'slate';
import { createParagraphPlugin } from '../../../paragraph/src/createParagraphPlugin';
import { createIndentListPlugin } from '../createIndentListPlugin';

jsx;

const input = ((
<editor>
<hp>1</hp>
<hp indent={1} listStyleType="disc" listStart={1}>
2
</hp>
</editor>
) as any) as Editor;

const output = ((
<editor>
<hp>1</hp>
<hp indent={1} listStyleType="disc">
2
</hp>
</editor>
) as any) as Editor;

it('should be', async () => {
const editor = createPlateEditor({
editor: input,
plugins: [
createParagraphPlugin(),
createIndentPlugin(),
createIndentListPlugin(),
],
normalizeInitialValue: true,
});

expect(editor.children).toEqual(output.children);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { isDefined, TEditor, unsetNodes } from '@udecode/plate-core';
import { NodeEntry } from 'slate';
import { KEY_LIST_START } from '../createIndentListPlugin';

/**
* If there is no previous list item and node list start is defined, unset list start (1).
*/
export const normalizeFirstIndentListStart = (
editor: TEditor,
[node, path]: NodeEntry
) => {
if (isDefined(node[KEY_LIST_START])) {
unsetNodes(editor, KEY_LIST_START, { at: path });
return true;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/** @jsx jsx */

import { createPlateEditor, TEditor } from '@udecode/plate-core';
import { createIndentPlugin } from '@udecode/plate-indent';
import { jsx } from '@udecode/plate-test-utils';
import { createIndentListPlugin } from '../createIndentListPlugin';

jsx;

describe('normalizeIndentList', () => {
describe('when listStyleType without indent', () => {
it('should remove listStyleType and listStart props', async () => {
const input = ((
<editor>
<hp listStyleType="disc" listStart={1}>
1
</hp>
</editor>
) as any) as TEditor;

const output = ((
<editor>
<hp>1</hp>
</editor>
) as any) as TEditor;

const editor = createPlateEditor({
editor: input,
plugins: [createIndentListPlugin(), createIndentPlugin()],
normalizeInitialValue: true,
});

expect(editor.children).toEqual(output.children);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { TEditor, unsetNodes } from '@udecode/plate-core';
import { KEY_INDENT } from '@udecode/plate-indent';
import { NodeEntry } from 'slate';
import { KEY_LIST_START, KEY_LIST_STYLE_TYPE } from '../createIndentListPlugin';

/**
* Unset KEY_LIST_STYLE_TYPE, KEY_LIST_START if KEY_INDENT is not defined.
*/
export const normalizeIndentListNotIndented = (
editor: TEditor,
[node, path]: NodeEntry
) => {
if (
!node[KEY_INDENT] &&
(node[KEY_LIST_STYLE_TYPE] || node[KEY_LIST_START])
) {
unsetNodes(editor, [KEY_LIST_STYLE_TYPE, KEY_LIST_START], { at: path });
return true;
}
};
Loading