@pallasoft/ps-input-components 2.2.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/ps-portable-text-editor.d.ts +1 -1
- package/dist/components/ps-portable-text-editor.js +1 -2
- package/dist/components/ps-portable-text-link-dialog.d.ts +2 -1
- package/dist/components/ps-portable-text-link-dialog.js +2 -2
- package/dist/components/ps-portable-text-toolbar.js +2 -7
- package/dist/constants/ps-portable-text-editor-config.d.ts +8 -0
- package/dist/constants/ps-portable-text-editor-config.js +8 -0
- package/dist/esm/components/ps-portable-text-editor.js +1 -2
- package/dist/esm/components/ps-portable-text-link-dialog.js +2 -2
- package/dist/esm/components/ps-portable-text-toolbar.js +2 -7
- package/dist/esm/constants/ps-portable-text-editor-config.js +8 -0
- package/dist/esm/portable-text-core/components/ps-portable-text-core-editor-stub.js +2 -0
- package/dist/esm/portable-text-core/components/ps-portable-text-core-editor.js +64 -0
- package/dist/esm/portable-text-core/components/ps-portable-text-core.js +5 -26
- package/dist/esm/portable-text-core/constants/ps-portable-text-core-config.js +29 -9
- package/dist/esm/portable-text-core/contexts/ps-portable-text-core-context.js +9 -0
- package/dist/esm/portable-text-core/hooks/use-ps-portable-text-link-form.js +11 -5
- package/dist/esm/portable-text-core/hooks/use-ps-portable-text-toolbar.js +17 -49
- package/dist/esm/portable-text-core/index.js +4 -0
- package/dist/esm/portable-text-core/interfaces/ps-portable-text-active-formats.js +1 -0
- package/dist/esm/portable-text-core/interfaces/ps-portable-text-block-draft.js +1 -0
- package/dist/esm/portable-text-core/interfaces/ps-portable-text-core-class-names.js +1 -0
- package/dist/esm/portable-text-core/interfaces/ps-portable-text-core-context-data.js +1 -0
- package/dist/esm/portable-text-core/interfaces/ps-portable-text-core-props.js +1 -0
- package/dist/esm/portable-text-core/interfaces/ps-portable-text-extension-options.js +1 -0
- package/dist/esm/portable-text-core/interfaces/ps-portable-text-span-draft.js +1 -0
- package/dist/esm/portable-text-core/interfaces/ps-portable-text-toolbar-state.js +1 -0
- package/dist/esm/portable-text-core/services/ps-portable-text-command-service.js +73 -0
- package/dist/esm/portable-text-core/services/ps-portable-text-converter-service.js +196 -0
- package/dist/esm/portable-text-core/services/ps-portable-text-editor-service.js +4 -0
- package/dist/esm/portable-text-core/services/ps-portable-text-extension-service.js +89 -0
- package/dist/esm/portable-text-engine/index.js +1 -0
- package/dist/esm/portable-text-engine/worker.js +1 -0
- package/dist/portable-text/index.d.ts +1 -1
- package/dist/portable-text-core/components/ps-portable-text-core-editor-stub.d.ts +2 -0
- package/dist/portable-text-core/components/ps-portable-text-core-editor-stub.js +6 -0
- package/dist/portable-text-core/components/ps-portable-text-core-editor.d.ts +2 -0
- package/dist/portable-text-core/components/ps-portable-text-core-editor.js +68 -0
- package/dist/portable-text-core/components/ps-portable-text-core.d.ts +2 -17
- package/dist/portable-text-core/components/ps-portable-text-core.js +3 -24
- package/dist/portable-text-core/constants/ps-portable-text-core-config.d.ts +36 -19
- package/dist/portable-text-core/constants/ps-portable-text-core-config.js +29 -9
- package/dist/portable-text-core/contexts/ps-portable-text-core-context.d.ts +2 -0
- package/dist/portable-text-core/contexts/ps-portable-text-core-context.js +12 -0
- package/dist/portable-text-core/hooks/use-ps-portable-text-link-form.d.ts +3 -1
- package/dist/portable-text-core/hooks/use-ps-portable-text-link-form.js +10 -4
- package/dist/portable-text-core/hooks/use-ps-portable-text-toolbar.d.ts +5 -10
- package/dist/portable-text-core/hooks/use-ps-portable-text-toolbar.js +17 -49
- package/dist/portable-text-core/index.d.ts +5 -1
- package/dist/portable-text-core/index.js +4 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-active-formats.d.ts +10 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-active-formats.js +2 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-block-draft.d.ts +7 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-block-draft.js +2 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-core-class-names.d.ts +8 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-core-class-names.js +2 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-core-context-data.d.ts +9 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-core-context-data.js +2 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-core-props.d.ts +16 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-core-props.js +2 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-editor-labels.d.ts +1 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-extension-options.d.ts +6 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-extension-options.js +2 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-span-draft.d.ts +4 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-span-draft.js +2 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-toolbar-state.d.ts +5 -0
- package/dist/portable-text-core/interfaces/ps-portable-text-toolbar-state.js +2 -0
- package/dist/portable-text-core/services/ps-portable-text-command-service.d.ts +14 -0
- package/dist/portable-text-core/services/ps-portable-text-command-service.js +77 -0
- package/dist/portable-text-core/services/ps-portable-text-converter-service.d.ts +18 -0
- package/dist/portable-text-core/services/ps-portable-text-converter-service.js +200 -0
- package/dist/portable-text-core/services/ps-portable-text-editor-service.d.ts +1 -0
- package/dist/portable-text-core/services/ps-portable-text-editor-service.js +4 -0
- package/dist/portable-text-core/services/ps-portable-text-extension-service.d.ts +8 -0
- package/dist/portable-text-core/services/ps-portable-text-extension-service.js +93 -0
- package/dist/portable-text-engine/index.d.ts +1 -0
- package/dist/portable-text-engine/index.js +17 -0
- package/dist/portable-text-engine/worker.d.ts +1 -0
- package/dist/portable-text-engine/worker.js +17 -0
- package/package.json +29 -5
- package/dist/constants/ps-portable-text-editor-renderers.d.ts +0 -6
- package/dist/constants/ps-portable-text-editor-renderers.js +0 -32
- package/dist/esm/constants/ps-portable-text-editor-renderers.js +0 -29
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
export declare const usePsPortableTextToolbar: () => {
|
|
2
|
-
active:
|
|
3
|
-
bold: boolean;
|
|
4
|
-
bulletedList: boolean;
|
|
5
|
-
code: boolean;
|
|
6
|
-
italic: boolean;
|
|
7
|
-
link: boolean;
|
|
8
|
-
numberedList: boolean;
|
|
9
|
-
quote: boolean;
|
|
10
|
-
underline: boolean;
|
|
11
|
-
};
|
|
2
|
+
active: import("..").PsPortableTextActiveFormats;
|
|
12
3
|
addLink: (href: string) => void;
|
|
4
|
+
closeLink: () => void;
|
|
13
5
|
focus: () => void;
|
|
6
|
+
isLinkOpen: boolean;
|
|
7
|
+
linkHref: string;
|
|
8
|
+
openLink: () => void;
|
|
14
9
|
removeLink: () => void;
|
|
15
10
|
toggleDecorator: (decorator: string) => void;
|
|
16
11
|
toggleListItem: (listItem: string) => void;
|
|
@@ -1,58 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.usePsPortableTextToolbar = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const ps_portable_text_core_context_1 = require("../contexts/ps-portable-text-core-context");
|
|
6
|
+
const ps_portable_text_command_service_1 = require("../services/ps-portable-text-command-service");
|
|
7
7
|
const usePsPortableTextToolbar = () => {
|
|
8
|
-
const editor = (0,
|
|
9
|
-
const isBold = (0, editor_1.useEditorSelector)(editor, (0, selectors_1.isActiveDecorator)(ps_portable_text_core_config_1.PsPortableTextCoreConfig.decorators.bold));
|
|
10
|
-
const isItalic = (0, editor_1.useEditorSelector)(editor, (0, selectors_1.isActiveDecorator)(ps_portable_text_core_config_1.PsPortableTextCoreConfig.decorators.italic));
|
|
11
|
-
const isUnderline = (0, editor_1.useEditorSelector)(editor, (0, selectors_1.isActiveDecorator)(ps_portable_text_core_config_1.PsPortableTextCoreConfig.decorators.underline));
|
|
12
|
-
const isCode = (0, editor_1.useEditorSelector)(editor, (0, selectors_1.isActiveDecorator)(ps_portable_text_core_config_1.PsPortableTextCoreConfig.decorators.code));
|
|
13
|
-
const isBulletedList = (0, editor_1.useEditorSelector)(editor, (0, selectors_1.isActiveListItem)(ps_portable_text_core_config_1.PsPortableTextCoreConfig.listItems.bullet));
|
|
14
|
-
const isNumberedList = (0, editor_1.useEditorSelector)(editor, (0, selectors_1.isActiveListItem)(ps_portable_text_core_config_1.PsPortableTextCoreConfig.listItems.number));
|
|
15
|
-
const isQuote = (0, editor_1.useEditorSelector)(editor, (0, selectors_1.isActiveStyle)(ps_portable_text_core_config_1.PsPortableTextCoreConfig.styles.blockquote));
|
|
16
|
-
const isLink = (0, editor_1.useEditorSelector)(editor, (0, selectors_1.isActiveAnnotation)(ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkAnnotation));
|
|
17
|
-
const focus = () => editor.send({ type: 'focus' });
|
|
8
|
+
const { closeLink, editor, isLinkOpen, openLink, state } = (0, react_1.useContext)(ps_portable_text_core_context_1.PsPortableTextCoreContext);
|
|
18
9
|
return {
|
|
19
|
-
active:
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
link: isLink,
|
|
25
|
-
numberedList: isNumberedList,
|
|
26
|
-
quote: isQuote,
|
|
27
|
-
underline: isUnderline,
|
|
28
|
-
},
|
|
29
|
-
addLink: (href) => {
|
|
30
|
-
editor.send({
|
|
31
|
-
type: 'annotation.add',
|
|
32
|
-
annotation: { name: ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkAnnotation, value: { href } },
|
|
33
|
-
});
|
|
34
|
-
focus();
|
|
35
|
-
},
|
|
36
|
-
focus,
|
|
37
|
-
removeLink: () => {
|
|
38
|
-
editor.send({
|
|
39
|
-
type: 'annotation.remove',
|
|
40
|
-
annotation: { name: ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkAnnotation },
|
|
41
|
-
});
|
|
42
|
-
focus();
|
|
43
|
-
},
|
|
44
|
-
toggleDecorator: (decorator) => {
|
|
45
|
-
editor.send({ type: 'decorator.toggle', decorator });
|
|
46
|
-
focus();
|
|
47
|
-
},
|
|
48
|
-
toggleListItem: (listItem) => {
|
|
49
|
-
editor.send({ type: 'list item.toggle', listItem });
|
|
50
|
-
focus();
|
|
51
|
-
},
|
|
52
|
-
toggleQuote: () => {
|
|
53
|
-
editor.send({ type: 'style.toggle', style: ps_portable_text_core_config_1.PsPortableTextCoreConfig.styles.blockquote });
|
|
54
|
-
focus();
|
|
10
|
+
active: state.active,
|
|
11
|
+
addLink: (href) => ps_portable_text_command_service_1.PsPortableTextCommandService.setLink(editor, href),
|
|
12
|
+
closeLink: () => {
|
|
13
|
+
closeLink();
|
|
14
|
+
ps_portable_text_command_service_1.PsPortableTextCommandService.focus(editor);
|
|
55
15
|
},
|
|
16
|
+
focus: () => ps_portable_text_command_service_1.PsPortableTextCommandService.focus(editor),
|
|
17
|
+
isLinkOpen,
|
|
18
|
+
linkHref: state.linkHref,
|
|
19
|
+
openLink,
|
|
20
|
+
removeLink: () => ps_portable_text_command_service_1.PsPortableTextCommandService.removeLink(editor),
|
|
21
|
+
toggleDecorator: (decorator) => ps_portable_text_command_service_1.PsPortableTextCommandService.toggleDecorator(editor, decorator),
|
|
22
|
+
toggleListItem: (listItem) => ps_portable_text_command_service_1.PsPortableTextCommandService.toggleListItem(editor, listItem),
|
|
23
|
+
toggleQuote: () => ps_portable_text_command_service_1.PsPortableTextCommandService.toggleQuote(editor),
|
|
56
24
|
};
|
|
57
25
|
};
|
|
58
26
|
exports.usePsPortableTextToolbar = usePsPortableTextToolbar;
|
|
@@ -2,6 +2,10 @@ export * from './components/ps-portable-text-core';
|
|
|
2
2
|
export * from './constants/ps-portable-text-core-config';
|
|
3
3
|
export * from './hooks/use-ps-portable-text-link-form';
|
|
4
4
|
export * from './hooks/use-ps-portable-text-toolbar';
|
|
5
|
+
export * from './interfaces/ps-portable-text-active-formats';
|
|
6
|
+
export * from './interfaces/ps-portable-text-core-class-names';
|
|
7
|
+
export * from './interfaces/ps-portable-text-core-props';
|
|
5
8
|
export * from './interfaces/ps-portable-text-editor-labels';
|
|
9
|
+
export * from './services/ps-portable-text-converter-service';
|
|
6
10
|
export * from './services/ps-portable-text-editor-service';
|
|
7
|
-
export type {
|
|
11
|
+
export type { PortableTextBlock } from '@portabletext/schema';
|
|
@@ -18,5 +18,9 @@ __exportStar(require("./components/ps-portable-text-core"), exports);
|
|
|
18
18
|
__exportStar(require("./constants/ps-portable-text-core-config"), exports);
|
|
19
19
|
__exportStar(require("./hooks/use-ps-portable-text-link-form"), exports);
|
|
20
20
|
__exportStar(require("./hooks/use-ps-portable-text-toolbar"), exports);
|
|
21
|
+
__exportStar(require("./interfaces/ps-portable-text-active-formats"), exports);
|
|
22
|
+
__exportStar(require("./interfaces/ps-portable-text-core-class-names"), exports);
|
|
23
|
+
__exportStar(require("./interfaces/ps-portable-text-core-props"), exports);
|
|
21
24
|
__exportStar(require("./interfaces/ps-portable-text-editor-labels"), exports);
|
|
25
|
+
__exportStar(require("./services/ps-portable-text-converter-service"), exports);
|
|
22
26
|
__exportStar(require("./services/ps-portable-text-editor-service"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Editor } from '@tiptap/core';
|
|
2
|
+
import type { PsPortableTextToolbarState } from './ps-portable-text-toolbar-state';
|
|
3
|
+
export interface PsPortableTextCoreContextData {
|
|
4
|
+
closeLink: () => void;
|
|
5
|
+
editor: Editor | null;
|
|
6
|
+
isLinkOpen: boolean;
|
|
7
|
+
openLink: () => void;
|
|
8
|
+
state: PsPortableTextToolbarState;
|
|
9
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { PortableTextBlock } from '@portabletext/schema';
|
|
3
|
+
import type { PsPortableTextCoreClassNames } from './ps-portable-text-core-class-names';
|
|
4
|
+
export interface PsPortableTextCoreProps {
|
|
5
|
+
className?: string;
|
|
6
|
+
classNames: PsPortableTextCoreClassNames;
|
|
7
|
+
disabled?: boolean;
|
|
8
|
+
editableClassName?: string;
|
|
9
|
+
fallback?: ReactNode;
|
|
10
|
+
label?: string;
|
|
11
|
+
minRows?: number;
|
|
12
|
+
onChange: (value: PortableTextBlock[]) => void;
|
|
13
|
+
placeholder?: string;
|
|
14
|
+
toolbar?: ReactNode;
|
|
15
|
+
value: PortableTextBlock[];
|
|
16
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Editor } from '@tiptap/core';
|
|
2
|
+
import type { PsPortableTextActiveFormats } from '../interfaces/ps-portable-text-active-formats';
|
|
3
|
+
import type { PsPortableTextToolbarState } from '../interfaces/ps-portable-text-toolbar-state';
|
|
4
|
+
export declare class PsPortableTextCommandService {
|
|
5
|
+
static state(editor: Editor | null): PsPortableTextToolbarState;
|
|
6
|
+
static active(editor: Editor | null): PsPortableTextActiveFormats;
|
|
7
|
+
static linkHref(editor: Editor | null): string;
|
|
8
|
+
static toggleDecorator(editor: Editor | null, decorator: string): void;
|
|
9
|
+
static toggleListItem(editor: Editor | null, listItem: string): void;
|
|
10
|
+
static toggleQuote(editor: Editor | null): void;
|
|
11
|
+
static setLink(editor: Editor | null, href: string): void;
|
|
12
|
+
static removeLink(editor: Editor | null): void;
|
|
13
|
+
static focus(editor: Editor | null): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PsPortableTextCommandService = void 0;
|
|
4
|
+
const ps_portable_text_core_config_1 = require("../constants/ps-portable-text-core-config");
|
|
5
|
+
const ps_portable_text_editor_service_1 = require("./ps-portable-text-editor-service");
|
|
6
|
+
class PsPortableTextCommandService {
|
|
7
|
+
static state(editor) {
|
|
8
|
+
return { active: this.active(editor), linkHref: this.linkHref(editor) };
|
|
9
|
+
}
|
|
10
|
+
static active(editor) {
|
|
11
|
+
const isActive = (name) => (editor === null ? false : editor.isActive(name));
|
|
12
|
+
const marks = ps_portable_text_core_config_1.PsPortableTextCoreConfig.decoratorMarks;
|
|
13
|
+
const decorators = ps_portable_text_core_config_1.PsPortableTextCoreConfig.decorators;
|
|
14
|
+
return {
|
|
15
|
+
bold: isActive(marks[decorators.bold]),
|
|
16
|
+
bulletedList: isActive(ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.bulletList),
|
|
17
|
+
code: isActive(marks[decorators.code]),
|
|
18
|
+
italic: isActive(marks[decorators.italic]),
|
|
19
|
+
link: isActive(ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkMark),
|
|
20
|
+
numberedList: isActive(ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.orderedList),
|
|
21
|
+
quote: isActive(ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.blockquote),
|
|
22
|
+
underline: isActive(marks[decorators.underline]),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
static linkHref(editor) {
|
|
26
|
+
const href = editor === null ? undefined : editor.getAttributes(ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkMark).href;
|
|
27
|
+
return typeof href === 'string' ? href : '';
|
|
28
|
+
}
|
|
29
|
+
static toggleDecorator(editor, decorator) {
|
|
30
|
+
const mark = ps_portable_text_editor_service_1.PsPortableTextEditorService.lookup(ps_portable_text_core_config_1.PsPortableTextCoreConfig.decoratorMarks, decorator);
|
|
31
|
+
if (editor !== null && mark !== null)
|
|
32
|
+
editor.chain().focus().toggleMark(mark).run();
|
|
33
|
+
}
|
|
34
|
+
static toggleListItem(editor, listItem) {
|
|
35
|
+
const list = ps_portable_text_editor_service_1.PsPortableTextEditorService.lookup(ps_portable_text_core_config_1.PsPortableTextCoreConfig.listNodes, listItem);
|
|
36
|
+
if (editor !== null && list !== null) {
|
|
37
|
+
editor.chain().focus().toggleList(list, ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.listItem).run();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
static toggleQuote(editor) {
|
|
41
|
+
if (editor !== null)
|
|
42
|
+
editor.chain().focus().toggleBlockquote().run();
|
|
43
|
+
}
|
|
44
|
+
static setLink(editor, href) {
|
|
45
|
+
if (editor === null)
|
|
46
|
+
return;
|
|
47
|
+
if (editor.state.selection.empty && !editor.isActive(ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkMark)) {
|
|
48
|
+
editor
|
|
49
|
+
.chain()
|
|
50
|
+
.focus()
|
|
51
|
+
.insertContent({
|
|
52
|
+
type: ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.text,
|
|
53
|
+
text: href.replace(ps_portable_text_core_config_1.PsPortableTextCoreConfig.mailtoPrefix, ''),
|
|
54
|
+
marks: [{ type: ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkMark, attrs: { href } }],
|
|
55
|
+
})
|
|
56
|
+
.unsetMark(ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkMark)
|
|
57
|
+
.run();
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
editor
|
|
61
|
+
.chain()
|
|
62
|
+
.focus()
|
|
63
|
+
.extendMarkRange(ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkMark)
|
|
64
|
+
.setLink({ href })
|
|
65
|
+
.run();
|
|
66
|
+
}
|
|
67
|
+
static removeLink(editor) {
|
|
68
|
+
if (editor !== null) {
|
|
69
|
+
editor.chain().focus().extendMarkRange(ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkMark).unsetLink().run();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
static focus(editor) {
|
|
73
|
+
if (editor !== null)
|
|
74
|
+
editor.commands.focus();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.PsPortableTextCommandService = PsPortableTextCommandService;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { PortableTextBlock } from '@portabletext/schema';
|
|
2
|
+
import type { JSONContent } from '@tiptap/core';
|
|
3
|
+
export declare class PsPortableTextConverterService {
|
|
4
|
+
static toDocument(blocks: PortableTextBlock[]): JSONContent;
|
|
5
|
+
static toBlocks(document: JSONContent): PortableTextBlock[];
|
|
6
|
+
private static drafts;
|
|
7
|
+
private static block;
|
|
8
|
+
private static spans;
|
|
9
|
+
private static spanMarks;
|
|
10
|
+
private static paragraph;
|
|
11
|
+
private static listNode;
|
|
12
|
+
private static listItem;
|
|
13
|
+
private static level;
|
|
14
|
+
private static href;
|
|
15
|
+
private static marks;
|
|
16
|
+
private static children;
|
|
17
|
+
private static isSame;
|
|
18
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PsPortableTextConverterService = void 0;
|
|
4
|
+
const ps_models_1 = require("@pallasoft/ps-models");
|
|
5
|
+
const ps_portable_text_core_config_1 = require("../constants/ps-portable-text-core-config");
|
|
6
|
+
const ps_portable_text_editor_service_1 = require("./ps-portable-text-editor-service");
|
|
7
|
+
class PsPortableTextConverterService {
|
|
8
|
+
static toDocument(blocks) {
|
|
9
|
+
const content = [];
|
|
10
|
+
let lists = [];
|
|
11
|
+
let quote = null;
|
|
12
|
+
(Array.isArray(blocks) ? blocks : [])
|
|
13
|
+
.filter((block) => block._type === ps_models_1.Common.PortableTextConfig.blockType)
|
|
14
|
+
.forEach((block) => {
|
|
15
|
+
const paragraph = this.paragraph(block);
|
|
16
|
+
const listNode = this.listNode(block.listItem);
|
|
17
|
+
if (listNode === null) {
|
|
18
|
+
lists = [];
|
|
19
|
+
if (block.style !== ps_portable_text_core_config_1.PsPortableTextCoreConfig.styles.blockquote) {
|
|
20
|
+
quote = null;
|
|
21
|
+
content.push(paragraph);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (quote === null) {
|
|
25
|
+
quote = { type: ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.blockquote, content: [] };
|
|
26
|
+
content.push(quote);
|
|
27
|
+
}
|
|
28
|
+
this.children(quote).push(paragraph);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
quote = null;
|
|
32
|
+
const level = Math.min(this.level(block.level), lists.length + 1);
|
|
33
|
+
const open = lists.slice(0, level);
|
|
34
|
+
lists =
|
|
35
|
+
open.length === level && open[level - 1].type !== listNode ? open.slice(0, -1) : open;
|
|
36
|
+
if (lists.length < level) {
|
|
37
|
+
const list = { type: listNode, content: [] };
|
|
38
|
+
const parent = lists[lists.length - 1];
|
|
39
|
+
if (parent === undefined)
|
|
40
|
+
content.push(list);
|
|
41
|
+
else
|
|
42
|
+
this.children(this.children(parent)[this.children(parent).length - 1]).push(list);
|
|
43
|
+
lists = [...lists, list];
|
|
44
|
+
}
|
|
45
|
+
this.children(lists[level - 1]).push({
|
|
46
|
+
type: ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.listItem,
|
|
47
|
+
content: [paragraph],
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
return {
|
|
51
|
+
type: ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.document,
|
|
52
|
+
content: content.length === 0 ? [{ type: ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.paragraph }] : content,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
static toBlocks(document) {
|
|
56
|
+
const drafts = this.children(document).flatMap((node) => this.drafts(node, 1));
|
|
57
|
+
const isBlank = drafts.length === 1 &&
|
|
58
|
+
drafts[0].listItem === undefined &&
|
|
59
|
+
drafts[0].style === ps_portable_text_core_config_1.PsPortableTextCoreConfig.styles.normal &&
|
|
60
|
+
this.children(drafts[0].paragraph).length === 0;
|
|
61
|
+
return isBlank
|
|
62
|
+
? []
|
|
63
|
+
: drafts.map((draft, index) => this.block(draft, `${ps_portable_text_core_config_1.PsPortableTextCoreConfig.keyPrefix.block}${index}`));
|
|
64
|
+
}
|
|
65
|
+
static drafts(node, level) {
|
|
66
|
+
if (node.type === ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.paragraph) {
|
|
67
|
+
return [{ paragraph: node, style: ps_portable_text_core_config_1.PsPortableTextCoreConfig.styles.normal }];
|
|
68
|
+
}
|
|
69
|
+
if (node.type === ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.blockquote) {
|
|
70
|
+
return this.children(node)
|
|
71
|
+
.filter((child) => child.type === ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.paragraph)
|
|
72
|
+
.map((paragraph) => ({ paragraph, style: ps_portable_text_core_config_1.PsPortableTextCoreConfig.styles.blockquote }));
|
|
73
|
+
}
|
|
74
|
+
const listItem = this.listItem(node.type);
|
|
75
|
+
if (listItem === null)
|
|
76
|
+
return [];
|
|
77
|
+
return this.children(node).flatMap((item) => this.children(item).flatMap((child) => child.type === ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.paragraph
|
|
78
|
+
? [
|
|
79
|
+
{
|
|
80
|
+
level: Math.min(level, ps_models_1.Common.PortableTextConfig.maxListLevel),
|
|
81
|
+
listItem,
|
|
82
|
+
paragraph: child,
|
|
83
|
+
style: ps_portable_text_core_config_1.PsPortableTextCoreConfig.styles.normal,
|
|
84
|
+
},
|
|
85
|
+
]
|
|
86
|
+
: this.drafts(child, level + 1)));
|
|
87
|
+
}
|
|
88
|
+
static block(draft, key) {
|
|
89
|
+
const hrefs = Array.from(new Set(this.children(draft.paragraph).flatMap((node) => this.marks(node)
|
|
90
|
+
.filter((mark) => mark.type === ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkMark)
|
|
91
|
+
.map((mark) => this.href(mark))
|
|
92
|
+
.filter((href) => href !== null))));
|
|
93
|
+
const markDefs = hrefs.map((href, index) => ({
|
|
94
|
+
_key: `${key}${ps_portable_text_core_config_1.PsPortableTextCoreConfig.keyPrefix.link}${index}`,
|
|
95
|
+
_type: ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkAnnotation,
|
|
96
|
+
href,
|
|
97
|
+
}));
|
|
98
|
+
const spans = this.spans(draft.paragraph, markDefs);
|
|
99
|
+
const children = (spans.length === 0 ? [{ marks: [], text: '' }] : spans).map((span, index) => ({
|
|
100
|
+
_key: `${key}${ps_portable_text_core_config_1.PsPortableTextCoreConfig.keyPrefix.span}${index}`,
|
|
101
|
+
_type: ps_models_1.Common.PortableTextConfig.spanType,
|
|
102
|
+
marks: span.marks,
|
|
103
|
+
text: span.text,
|
|
104
|
+
}));
|
|
105
|
+
const base = {
|
|
106
|
+
_key: key,
|
|
107
|
+
_type: ps_models_1.Common.PortableTextConfig.blockType,
|
|
108
|
+
children,
|
|
109
|
+
markDefs,
|
|
110
|
+
style: draft.style,
|
|
111
|
+
};
|
|
112
|
+
return draft.listItem === undefined
|
|
113
|
+
? base
|
|
114
|
+
: { ...base, level: draft.level, listItem: draft.listItem };
|
|
115
|
+
}
|
|
116
|
+
static spans(paragraph, markDefs) {
|
|
117
|
+
return this.children(paragraph).reduce((spans, node) => {
|
|
118
|
+
const previous = spans[spans.length - 1];
|
|
119
|
+
const isBreak = node.type === ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.hardBreak;
|
|
120
|
+
if (!isBreak && (node.type !== ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.text || !node.text)) {
|
|
121
|
+
return spans;
|
|
122
|
+
}
|
|
123
|
+
const marks = this.spanMarks(node, markDefs);
|
|
124
|
+
const text = isBreak ? ps_portable_text_core_config_1.PsPortableTextCoreConfig.lineBreak : String(node.text);
|
|
125
|
+
return previous !== undefined && this.isSame(previous.marks, marks)
|
|
126
|
+
? [...spans.slice(0, -1), { marks, text: previous.text + text }]
|
|
127
|
+
: [...spans, { marks, text }];
|
|
128
|
+
}, []);
|
|
129
|
+
}
|
|
130
|
+
static spanMarks(node, markDefs) {
|
|
131
|
+
const types = this.marks(node).map((mark) => mark.type);
|
|
132
|
+
const decorators = ps_models_1.Common.PortableTextConfig.decorators.filter((decorator) => types.includes(ps_portable_text_core_config_1.PsPortableTextCoreConfig.decoratorMarks[decorator]));
|
|
133
|
+
const links = this.marks(node)
|
|
134
|
+
.filter((mark) => mark.type === ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkMark)
|
|
135
|
+
.map((mark) => markDefs.find((definition) => definition.href === this.href(mark)))
|
|
136
|
+
.filter((definition) => definition !== undefined)
|
|
137
|
+
.map((definition) => definition._key);
|
|
138
|
+
return [...decorators, ...links];
|
|
139
|
+
}
|
|
140
|
+
static paragraph(block) {
|
|
141
|
+
const links = new Map((Array.isArray(block.markDefs) ? block.markDefs : [])
|
|
142
|
+
.filter((definition) => definition._type === ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkAnnotation)
|
|
143
|
+
.map((definition) => [
|
|
144
|
+
definition._key,
|
|
145
|
+
typeof definition.href === 'string'
|
|
146
|
+
? ps_portable_text_editor_service_1.PsPortableTextEditorService.normalizeLink(definition.href)
|
|
147
|
+
: null,
|
|
148
|
+
]));
|
|
149
|
+
const content = (Array.isArray(block.children) ? block.children : [])
|
|
150
|
+
.filter((child) => child._type === ps_models_1.Common.PortableTextConfig.spanType && typeof child.text === 'string')
|
|
151
|
+
.flatMap((child) => {
|
|
152
|
+
const marks = (Array.isArray(child.marks) ? child.marks : [])
|
|
153
|
+
.filter((mark) => typeof mark === 'string')
|
|
154
|
+
.flatMap((mark) => {
|
|
155
|
+
const decorator = ps_portable_text_editor_service_1.PsPortableTextEditorService.lookup(ps_portable_text_core_config_1.PsPortableTextCoreConfig.decoratorMarks, mark);
|
|
156
|
+
const href = links.get(mark);
|
|
157
|
+
if (decorator !== null)
|
|
158
|
+
return [{ type: decorator }];
|
|
159
|
+
return typeof href === 'string'
|
|
160
|
+
? [{ type: ps_portable_text_core_config_1.PsPortableTextCoreConfig.linkMark, attrs: { href } }]
|
|
161
|
+
: [];
|
|
162
|
+
});
|
|
163
|
+
return String(child.text)
|
|
164
|
+
.split(ps_portable_text_core_config_1.PsPortableTextCoreConfig.lineBreak)
|
|
165
|
+
.flatMap((text, index) => [
|
|
166
|
+
...(index === 0 ? [] : [{ type: ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.hardBreak, marks }]),
|
|
167
|
+
...(text === '' ? [] : [{ type: ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.text, text, marks }]),
|
|
168
|
+
]);
|
|
169
|
+
});
|
|
170
|
+
return content.length === 0
|
|
171
|
+
? { type: ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.paragraph }
|
|
172
|
+
: { type: ps_portable_text_core_config_1.PsPortableTextCoreConfig.nodes.paragraph, content };
|
|
173
|
+
}
|
|
174
|
+
static listNode(listItem) {
|
|
175
|
+
return ps_portable_text_editor_service_1.PsPortableTextEditorService.lookup(ps_portable_text_core_config_1.PsPortableTextCoreConfig.listNodes, listItem);
|
|
176
|
+
}
|
|
177
|
+
static listItem(type) {
|
|
178
|
+
const match = Object.entries(ps_portable_text_core_config_1.PsPortableTextCoreConfig.listNodes).find(([, node]) => node === type);
|
|
179
|
+
return match === undefined ? null : match[0];
|
|
180
|
+
}
|
|
181
|
+
static level(level) {
|
|
182
|
+
return typeof level === 'number' && Number.isInteger(level)
|
|
183
|
+
? Math.max(1, Math.min(level, ps_models_1.Common.PortableTextConfig.maxListLevel))
|
|
184
|
+
: 1;
|
|
185
|
+
}
|
|
186
|
+
static href(mark) {
|
|
187
|
+
const href = mark.attrs === undefined ? undefined : mark.attrs.href;
|
|
188
|
+
return typeof href === 'string' ? ps_portable_text_editor_service_1.PsPortableTextEditorService.normalizeLink(href) : null;
|
|
189
|
+
}
|
|
190
|
+
static marks(node) {
|
|
191
|
+
return Array.isArray(node.marks) ? node.marks : [];
|
|
192
|
+
}
|
|
193
|
+
static children(node) {
|
|
194
|
+
return Array.isArray(node.content) ? node.content : [];
|
|
195
|
+
}
|
|
196
|
+
static isSame(first, second) {
|
|
197
|
+
return first.length === second.length && first.every((mark, index) => mark === second[index]);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
exports.PsPortableTextConverterService = PsPortableTextConverterService;
|
|
@@ -2,4 +2,5 @@ export declare class PsPortableTextEditorService {
|
|
|
2
2
|
static normalizeLink(raw: string): string | null;
|
|
3
3
|
static minHeight(minRows: number): string;
|
|
4
4
|
static isSame(first: unknown, second: unknown): boolean;
|
|
5
|
+
static lookup(record: Record<string, string>, key: unknown): string | null;
|
|
5
6
|
}
|
|
@@ -30,5 +30,9 @@ class PsPortableTextEditorService {
|
|
|
30
30
|
static isSame(first, second) {
|
|
31
31
|
return JSON.stringify(first) === JSON.stringify(second);
|
|
32
32
|
}
|
|
33
|
+
static lookup(record, key) {
|
|
34
|
+
const match = Object.entries(record).find(([name]) => name === key);
|
|
35
|
+
return match === undefined ? null : match[1];
|
|
36
|
+
}
|
|
33
37
|
}
|
|
34
38
|
exports.PsPortableTextEditorService = PsPortableTextEditorService;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type AnyExtension, type Editor } from '@tiptap/core';
|
|
2
|
+
import type { PsPortableTextExtensionOptions } from '../interfaces/ps-portable-text-extension-options';
|
|
3
|
+
export declare class PsPortableTextExtensionService {
|
|
4
|
+
static extensions({ classNames, onLinkShortcut, placeholder, }: PsPortableTextExtensionOptions): AnyExtension[];
|
|
5
|
+
static listDepth(editor: Editor): number;
|
|
6
|
+
private static nest;
|
|
7
|
+
private static unnest;
|
|
8
|
+
}
|