Skip to content

Commit

Permalink
feat(global-normalize): add and apply global-normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
pubuzhixing8 committed Jul 14, 2021
1 parent 91bfb9a commit c947c64
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 5 deletions.
3 changes: 2 additions & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ module.exports = {
'decorate',
'pretter',
'types',
'block-card'
'block-card',
'global-normalize'
]
]
}
Expand Down
1 change: 1 addition & 0 deletions custom-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export type CustomText = {
italic?: boolean
code?: boolean
text: string
'code-line': boolean
}

export type EmptyText = {
Expand Down
14 changes: 12 additions & 2 deletions packages/src/components/editable/editable.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { SlateChildrenContext, SlateViewContext } from '../../view/context';
import { ViewType } from '../../types/view';
import { HistoryEditor } from 'slate-history';
import { isDecoratorRangeListEqual } from '../../utils';
import { check, normalize } from '../../utils/global-normalize';

const timeDebug = Debug('slate-time');
// COMPAT: Firefox/Edge Legacy don't support the `beforeinput` event
Expand Down Expand Up @@ -191,9 +192,18 @@ export class SlateEditableComponent implements OnInit, OnChanges, OnDestroy {
this.onTouchedCallback = fn;
}

writeValue(value: Descendant[]) {
writeValue(value: Element[]) {
if (value && value.length) {
this.editor.children = value;
if (check(value)) {
this.editor.children = value;
} else {
this.editor.onError({
code: SlateErrorCode.InvalidValueError,
name: 'initialize invalid data',
data: value
});
this.editor.children = normalize(value);
}
this.cdr.markForCheck();
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/src/plugins/with-angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ export const withAngular = <T extends Editor>(editor: T, clipboardFormatKey = 'x
e.onError = (errorData: SlateError) => {
if (errorData.nativeError) {
console.error(errorData.nativeError);
} else {
console.error(errorData);
}
};

Expand Down
3 changes: 2 additions & 1 deletion packages/src/types/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export enum SlateErrorCode {
OnSyntheticBeforeInputError = 2103,
OnDOMKeydownError = 2104,
GetStartPointError = 2105,
NotFoundPreviousRootNodeError = 3100
NotFoundPreviousRootNodeError = 3100,
InvalidValueError = 4100,
}

export interface SlateError {
Expand Down
114 changes: 114 additions & 0 deletions packages/src/utils/global-normalize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { Element } from 'slate';
import { check, normalize } from './global-normalize';

describe('global-normalize', () => {
const invalidData3: any[] = [
{
type: 'paragraph',
children: [
{ text: '' }
]
},
{
type: 'numbered-list',
children: [
{
type: 'list-item',
children: [
{
type: 'paragraph',
children: [
{
text: ''
}
]
},
{
type: 'paragraph',
children: []
}
]
}
]
}
];

it('should return true', () => {
const validData: Element[] = [
{
type: 'paragraph',
children: [
{ text: 'This is editable ' },
{ text: 'rich', bold: true },
{ text: ' text, ' },
{ text: 'much', bold: true, italic: true },
{ text: ' better than a ' },
{ text: '<textarea>', ['code-line']: true },
{ text: '!' }
]
},
{
type: 'heading-one',
children: [{ text: 'This is h1 ' }]
},
{
type: 'heading-three',
children: [{ text: 'This is h3 ' }]
},
{
type: 'paragraph',
children: [
{
text: `Since it's rich text, you can do things like turn a selection of text `
},
{ text: 'bold', bold: true },
{
text: ', or add a semantically rendered block quote in the middle of the page, like this:'
}
]
},
{
type: 'block-quote',
children: [{ text: 'A wise quote.' }]
},
{
type: 'paragraph',
children: [{ text: 'Try it out for yourself!' }]
},
{
type: 'paragraph',
children: [{ text: '' }]
}
];
const result = check(validData);
expect(result).toBeTrue();
});

it('should return false', () => {
const invalidData1: any[] = [
{ text: '' }
];

const result1 = check(invalidData1);
expect(result1).toBeFalse();

const invalidData2: any[] = [
{
type: 'paragraph',
children: []
}
];

const result2 = check(invalidData2);
expect(result2).toBeFalse();

const result3 = check(invalidData3);
expect(result3).toBeFalse();
});

it('should return valid data', () => {
const result3 = normalize(invalidData3);
expect(result3.length).toEqual(1);
expect(result3[0]).toEqual(invalidData3[0]);
})
});
17 changes: 17 additions & 0 deletions packages/src/utils/global-normalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Descendant, Element, Text } from "slate";

const isValid = (value: Descendant) => (
Element.isElement(value) &&
value.children.length > 0 &&
(value.children as Descendant[]).every((child) => isValid(child))) ||
Text.isText(value);

const check = (document: Element[]) => {
return document.every((value) => Element.isElement(value) && isValid(value));;
}

function normalize(document: Element[]) {
return document.filter((value) => Element.isElement(value) && isValid(value));
}

export { normalize, check, isValid };
3 changes: 2 additions & 1 deletion packages/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export * from './view';
export * from './environment';
export * from './key';
export * from './range-list';
export * from './block-card';
export * from './block-card';
export * from './global-normalize';

0 comments on commit c947c64

Please sign in to comment.