@limetech/lime-elements 38.13.2-dev.1 → 38.13.3
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/CHANGELOG.md +17 -2
- package/dist/cjs/limel-ai-avatar.cjs.entry.js +1 -1
- package/dist/cjs/limel-ai-avatar.cjs.entry.js.map +1 -1
- package/dist/cjs/limel-markdown.cjs.entry.js +1 -1
- package/dist/cjs/limel-prosemirror-adapter.cjs.entry.js +169 -62
- package/dist/cjs/limel-prosemirror-adapter.cjs.entry.js.map +1 -1
- package/dist/cjs/{markdown-parser-564adb69.js → markdown-parser-563c73db.js} +144 -386
- package/dist/cjs/{markdown-parser-564adb69.js.map → markdown-parser-563c73db.js.map} +1 -1
- package/dist/collection/components/ai-avatar/ai-avatar.css +9 -14
- package/dist/collection/components/markdown/link-markdown-plugin.js +30 -0
- package/dist/collection/components/markdown/link-markdown-plugin.js.map +1 -0
- package/dist/collection/components/markdown/markdown-parser.js +4 -4
- package/dist/collection/components/markdown/markdown-parser.js.map +1 -1
- package/dist/collection/components/text-editor/prosemirror-adapter/menu/menu-commands.js +3 -42
- package/dist/collection/components/text-editor/prosemirror-adapter/menu/menu-commands.js.map +1 -1
- package/dist/collection/components/text-editor/prosemirror-adapter/plugins/link/link-mark-spec.js +33 -0
- package/dist/collection/components/text-editor/prosemirror-adapter/plugins/link/link-mark-spec.js.map +1 -0
- package/dist/collection/components/text-editor/prosemirror-adapter/plugins/{link-plugin.js → link/link-plugin.js} +135 -22
- package/dist/collection/components/text-editor/prosemirror-adapter/plugins/link/link-plugin.js.map +1 -0
- package/dist/collection/components/text-editor/prosemirror-adapter/plugins/link/utils.js +39 -0
- package/dist/collection/components/text-editor/prosemirror-adapter/plugins/link/utils.js.map +1 -0
- package/dist/collection/components/text-editor/prosemirror-adapter/prosemirror-adapter.js +3 -1
- package/dist/collection/components/text-editor/prosemirror-adapter/prosemirror-adapter.js.map +1 -1
- package/dist/esm/limel-ai-avatar.entry.js +1 -1
- package/dist/esm/limel-ai-avatar.entry.js.map +1 -1
- package/dist/esm/limel-markdown.entry.js +1 -1
- package/dist/esm/limel-prosemirror-adapter.entry.js +169 -62
- package/dist/esm/limel-prosemirror-adapter.entry.js.map +1 -1
- package/dist/esm/{markdown-parser-1c1fdedc.js → markdown-parser-41d15994.js} +144 -387
- package/dist/esm/markdown-parser-41d15994.js.map +1 -0
- package/dist/lime-elements/lime-elements.esm.js +1 -1
- package/dist/lime-elements/p-6b5d588e.entry.js +2 -0
- package/dist/lime-elements/p-6b5d588e.entry.js.map +1 -0
- package/dist/lime-elements/{p-ce152b39.entry.js → p-895ae176.entry.js} +2 -2
- package/dist/lime-elements/p-95a71e89.js +8 -0
- package/dist/lime-elements/p-95a71e89.js.map +1 -0
- package/dist/lime-elements/p-f15163c0.entry.js +2 -0
- package/dist/lime-elements/p-f15163c0.entry.js.map +1 -0
- package/dist/types/components/markdown/link-markdown-plugin.d.ts +9 -0
- package/dist/types/components/text-editor/prosemirror-adapter/menu/menu-commands.d.ts +0 -2
- package/dist/types/components/text-editor/prosemirror-adapter/plugins/link/link-mark-spec.d.ts +10 -0
- package/dist/types/components/text-editor/prosemirror-adapter/plugins/link/utils.d.ts +3 -0
- package/package.json +1 -2
- package/dist/collection/components/text-editor/prosemirror-adapter/plugins/link-plugin.js.map +0 -1
- package/dist/esm/markdown-parser-1c1fdedc.js.map +0 -1
- package/dist/lime-elements/p-98478c6d.entry.js +0 -2
- package/dist/lime-elements/p-98478c6d.entry.js.map +0 -1
- package/dist/lime-elements/p-cf87519f.js +0 -8
- package/dist/lime-elements/p-cf87519f.js.map +0 -1
- package/dist/lime-elements/p-d5ac8f59.entry.js +0 -2
- package/dist/lime-elements/p-d5ac8f59.entry.js.map +0 -1
- /package/dist/lime-elements/{p-ce152b39.entry.js.map → p-895ae176.entry.js.map} +0 -0
- /package/dist/types/components/text-editor/prosemirror-adapter/plugins/{link-plugin.d.ts → link/link-plugin.d.ts} +0 -0
|
@@ -20,14 +20,13 @@
|
|
|
20
20
|
|
|
21
21
|
:host(limel-ai-avatar[is-thinking]:not([is-thinking=false])) {
|
|
22
22
|
--ai-avatar-animation-play-state: running;
|
|
23
|
-
--ai-avatar-orbitals-opacity:
|
|
23
|
+
--ai-avatar-orbitals-opacity: 0.6;
|
|
24
24
|
--ai-avatar-orbitals-animation-play-state: running;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
.core,
|
|
28
28
|
.orbitals {
|
|
29
29
|
position: absolute;
|
|
30
|
-
z-index: 10;
|
|
31
30
|
inset: 0;
|
|
32
31
|
margin: auto;
|
|
33
32
|
display: flex;
|
|
@@ -40,23 +39,19 @@
|
|
|
40
39
|
.core {
|
|
41
40
|
opacity: 0.8;
|
|
42
41
|
width: 70%;
|
|
43
|
-
animation: breathe 3s ease infinite;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
box-shadow: var(--shadow-depth-8);
|
|
47
|
-
backdrop-filter: blur(1rem);
|
|
42
|
+
animation: breathe 3s ease infinite var(--ai-avatar-animation-play-state, paused);
|
|
43
|
+
background-color: rgb(var(--color-glaucous-darker), 0.6);
|
|
44
|
+
mix-blend-mode: plus-lighter;
|
|
48
45
|
}
|
|
49
46
|
|
|
50
47
|
.orbitals {
|
|
51
|
-
mix-blend-mode:
|
|
48
|
+
mix-blend-mode: plus-lighter;
|
|
52
49
|
width: clamp(0.375rem, 20%, 3.5rem);
|
|
53
|
-
animation: rotate 5s linear infinite;
|
|
54
|
-
animation-play-state: var(--ai-avatar-animation-play-state, paused);
|
|
50
|
+
animation: rotate 5s linear infinite var(--ai-avatar-orbitals-animation-play-state, paused);
|
|
55
51
|
transition: opacity 0.2s ease;
|
|
56
52
|
opacity: var(--ai-avatar-orbitals-opacity, 0);
|
|
57
53
|
}
|
|
58
54
|
.orbitals:after, .orbitals:before {
|
|
59
|
-
animation-play-state: var(--ai-avatar-orbitals-animation-play-state, paused);
|
|
60
55
|
content: "";
|
|
61
56
|
display: block;
|
|
62
57
|
position: absolute;
|
|
@@ -68,13 +63,13 @@
|
|
|
68
63
|
background-color: rgb(var(--color-glaucous-lighter));
|
|
69
64
|
}
|
|
70
65
|
.orbitals:before {
|
|
71
|
-
animation: orbit 1s linear infinite;
|
|
66
|
+
animation: orbit 1s linear infinite var(--ai-avatar-orbitals-animation-play-state, paused);
|
|
72
67
|
opacity: 0.6;
|
|
73
68
|
transform-origin: -220% 0;
|
|
74
69
|
margin-right: -70%;
|
|
75
70
|
}
|
|
76
71
|
.orbitals:after {
|
|
77
|
-
animation: orbit 2s linear infinite;
|
|
72
|
+
animation: orbit 2s linear infinite var(--ai-avatar-orbitals-animation-play-state, paused);
|
|
78
73
|
opacity: 0.8;
|
|
79
74
|
scale: 0.7;
|
|
80
75
|
transform-origin: 0% -250%;
|
|
@@ -94,7 +89,7 @@
|
|
|
94
89
|
transform: scale(1);
|
|
95
90
|
}
|
|
96
91
|
50% {
|
|
97
|
-
transform: scale(0.
|
|
92
|
+
transform: scale(0.86);
|
|
98
93
|
}
|
|
99
94
|
}
|
|
100
95
|
@keyframes rotate {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { visit } from 'unist-util-visit';
|
|
2
|
+
import { getLinkAttributes } from '../text-editor/prosemirror-adapter/plugins/link/utils';
|
|
3
|
+
/**
|
|
4
|
+
* Creates a unified.js plugin that transforms link elements
|
|
5
|
+
* to add target, rel, and referrerpolicy attributes.
|
|
6
|
+
*
|
|
7
|
+
* @returns A unified.js plugin function
|
|
8
|
+
*/
|
|
9
|
+
export function createLinksPlugin() {
|
|
10
|
+
return () => {
|
|
11
|
+
return (tree) => {
|
|
12
|
+
visit(tree, 'element', (node) => {
|
|
13
|
+
var _a, _b;
|
|
14
|
+
if (node.tagName === 'a') {
|
|
15
|
+
const href = (_a = node.properties) === null || _a === void 0 ? void 0 : _a.href;
|
|
16
|
+
const title = (_b = node.properties) === null || _b === void 0 ? void 0 : _b.title;
|
|
17
|
+
if (!href) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const attributes = getLinkAttributes(href, title);
|
|
21
|
+
node.properties.target = attributes.target;
|
|
22
|
+
node.properties.rel = attributes.rel;
|
|
23
|
+
node.properties.referrerpolicy = attributes.referrerpolicy;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return tree;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=link-markdown-plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"link-markdown-plugin.js","sourceRoot":"","sources":["../../../src/components/markdown/link-markdown-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAGzC,OAAO,EAAE,iBAAiB,EAAE,MAAM,uDAAuD,CAAC;AAE1F;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB;EAC7B,OAAO,GAAgB,EAAE;IACrB,OAAO,CAAC,IAAU,EAAE,EAAE;MAClB,KAAK,CAAC,IAAI,EAAE,SAAS,EAAE,CAAC,IAAS,EAAE,EAAE;;QACjC,IAAI,IAAI,CAAC,OAAO,KAAK,GAAG,EAAE;UACtB,MAAM,IAAI,GAAG,MAAA,IAAI,CAAC,UAAU,0CAAE,IAAI,CAAC;UACnC,MAAM,KAAK,GAAG,MAAA,IAAI,CAAC,UAAU,0CAAE,KAAK,CAAC;UAErC,IAAI,CAAC,IAAI,EAAE;YACP,OAAO;WACV;UAED,MAAM,UAAU,GAAG,iBAAiB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;UAElD,IAAI,CAAC,UAAU,CAAC,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC;UAC3C,IAAI,CAAC,UAAU,CAAC,GAAG,GAAG,UAAU,CAAC,GAAG,CAAC;UACrC,IAAI,CAAC,UAAU,CAAC,cAAc,GAAG,UAAU,CAAC,cAAc,CAAC;SAC9D;MACL,CAAC,CAAC,CAAC;MAEH,OAAO,IAAI,CAAC;IAChB,CAAC,CAAC;EACN,CAAC,CAAC;AACN,CAAC","sourcesContent":["import { visit } from 'unist-util-visit';\nimport { Node } from 'unist';\nimport { Plugin, Transformer } from 'unified';\nimport { getLinkAttributes } from '../text-editor/prosemirror-adapter/plugins/link/utils';\n\n/**\n * Creates a unified.js plugin that transforms link elements\n * to add target, rel, and referrerpolicy attributes.\n *\n * @returns A unified.js plugin function\n */\nexport function createLinksPlugin(): Plugin {\n return (): Transformer => {\n return (tree: Node) => {\n visit(tree, 'element', (node: any) => {\n if (node.tagName === 'a') {\n const href = node.properties?.href;\n const title = node.properties?.title;\n\n if (!href) {\n return;\n }\n\n const attributes = getLinkAttributes(href, title);\n\n node.properties.target = attributes.target;\n node.properties.rel = attributes.rel;\n node.properties.referrerpolicy = attributes.referrerpolicy;\n }\n });\n\n return tree;\n };\n };\n}\n"]}
|
|
@@ -3,13 +3,13 @@ import remarkParse from 'remark-parse';
|
|
|
3
3
|
import remarkRehype from 'remark-rehype';
|
|
4
4
|
import remarkGfm from 'remark-gfm';
|
|
5
5
|
import rehypeParse from 'rehype-parse';
|
|
6
|
-
import rehypeExternalLinks from 'rehype-external-links';
|
|
7
6
|
import rehypeSanitize, { defaultSchema } from 'rehype-sanitize';
|
|
8
7
|
import rehypeStringify from 'rehype-stringify';
|
|
9
8
|
import rehypeRaw from 'rehype-raw';
|
|
10
9
|
import { visit } from 'unist-util-visit';
|
|
11
10
|
import { sanitizeStyle } from './sanitize-style';
|
|
12
11
|
import { createLazyLoadImagesPlugin } from './image-markdown-plugin';
|
|
12
|
+
import { createLinksPlugin } from './link-markdown-plugin';
|
|
13
13
|
/**
|
|
14
14
|
* Takes a string as input and returns a new string
|
|
15
15
|
* where the text has been converted to HTML.
|
|
@@ -33,8 +33,8 @@ export async function markdownToHTML(text, options) {
|
|
|
33
33
|
.use(remarkParse)
|
|
34
34
|
.use(remarkGfm)
|
|
35
35
|
.use(remarkRehype, { allowDangerousHtml: true })
|
|
36
|
-
.use(rehypeExternalLinks, { target: '_blank' })
|
|
37
36
|
.use(rehypeRaw)
|
|
37
|
+
.use(createLinksPlugin())
|
|
38
38
|
.use(rehypeSanitize, Object.assign({}, getWhiteList((_a = options === null || options === void 0 ? void 0 : options.whitelist) !== null && _a !== void 0 ? _a : [])))
|
|
39
39
|
.use(() => {
|
|
40
40
|
return (tree) => {
|
|
@@ -71,7 +71,7 @@ export async function sanitizeHTML(html, whitelist) {
|
|
|
71
71
|
return file.toString();
|
|
72
72
|
}
|
|
73
73
|
function getWhiteList(allowedComponents) {
|
|
74
|
-
var _a, _b;
|
|
74
|
+
var _a, _b, _c;
|
|
75
75
|
const defaultSchemaClone = [...((_a = defaultSchema.attributes['*']) !== null && _a !== void 0 ? _a : [])];
|
|
76
76
|
const asteriskAttributeWhitelist = defaultSchemaClone.filter((attr) => {
|
|
77
77
|
return attr !== 'height';
|
|
@@ -83,7 +83,7 @@ function getWhiteList(allowedComponents) {
|
|
|
83
83
|
], attributes: Object.assign(Object.assign({}, defaultSchema.attributes), { p: [
|
|
84
84
|
...((_b = defaultSchema.attributes.p) !== null && _b !== void 0 ? _b : []),
|
|
85
85
|
['className', 'MsoNormal'],
|
|
86
|
-
], '*': asteriskAttributeWhitelist }) });
|
|
86
|
+
], a: [...((_c = defaultSchema.attributes.a) !== null && _c !== void 0 ? _c : []), 'referrerpolicy'], '*': asteriskAttributeWhitelist }) });
|
|
87
87
|
for (const component of allowedComponents) {
|
|
88
88
|
whitelist.attributes[component.tagName] = component.attributes;
|
|
89
89
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdown-parser.js","sourceRoot":"","sources":["../../../src/components/markdown/markdown-parser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,WAAW,MAAM,cAAc,CAAC;AACvC,OAAO,YAAY,MAAM,eAAe,CAAC;AACzC,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,WAAW,MAAM,c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,CAAC;EAC7B,CAAC,CAAC,CAAC;EACH,0BAA0B,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;EAEzC,MAAM,SAAS,mCACR,aAAa,KAChB,QAAQ,EAAE;MACN,GAAG,CAAC,aAAa,CAAC,QAAQ,IAAI,EAAE,CAAC;MACjC,GAAG,iBAAiB,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC;KAC7D,EACD,UAAU,kCACH,aAAa,CAAC,UAAU,KAC3B,CAAC,EAAE;QACC,GAAG,CAAC,MAAA,aAAa,CAAC,UAAU,CAAC,CAAC,mCAAI,EAAE,CAAC;QACrC,CAAC,WAAW,EAAE,WAAW,CAAC;OAC7B,EACD,CAAC,EAAE,CAAC,GAAG,CAAC,MAAA,aAAa,CAAC,UAAU,CAAC,CAAC,mCAAI,EAAE,CAAC,EAAE,gBAAgB,CAAC,EAC5D,GAAG,EAAE,0BAA0B,MAEtC,CAAC;EAEF,KAAK,MAAM,SAAS,IAAI,iBAAiB,EAAE;IACvC,SAAS,CAAC,UAAU,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,SAAS,CAAC,UAAU,CAAC;GAClE;EAED,OAAO,SAAS,CAAC;AACrB,CAAC","sourcesContent":["import { unified } from 'unified';\nimport remarkParse from 'remark-parse';\nimport remarkRehype from 'remark-rehype';\nimport remarkGfm from 'remark-gfm';\nimport rehypeParse from 'rehype-parse';\nimport rehypeSanitize, { defaultSchema } from 'rehype-sanitize';\nimport rehypeStringify from 'rehype-stringify';\nimport rehypeRaw from 'rehype-raw';\nimport { visit } from 'unist-util-visit';\nimport { sanitizeStyle } from './sanitize-style';\nimport { Node } from 'unist';\nimport { Schema } from 'rehype-sanitize/lib';\nimport { createLazyLoadImagesPlugin } from './image-markdown-plugin';\nimport { CustomElementDefinition } from '../../global/shared-types/custom-element.types';\nimport { createLinksPlugin } from './link-markdown-plugin';\n\n/**\n * Takes a string as input and returns a new string\n * where the text has been converted to HTML.\n *\n * If the text is formatted with .md markdown, it will\n * be transformed to HTML.\n *\n * If the text already is in HTML it will be sanitized and\n * \"dangerous\" tags such as <script> will be removed.\n *\n * @param text - The string to convert.\n * @param options - Options for the conversions.\n * @returns The resulting HTML.\n */\nexport async function markdownToHTML(\n text: string,\n options?: MarkdownToHTMLOptions,\n): Promise<string> {\n if (options?.forceHardLineBreaks) {\n text = text.replace(/(?<!\\\\)([\\n\\r])/g, ' $1');\n }\n\n const file = await unified()\n .use(remarkParse)\n .use(remarkGfm)\n .use(remarkRehype, { allowDangerousHtml: true })\n .use(rehypeRaw)\n .use(createLinksPlugin())\n .use(rehypeSanitize, {\n ...getWhiteList(options?.whitelist ?? []),\n })\n .use(() => {\n return (tree: Node) => {\n // Run the sanitizeStyle function on all elements, to sanitize\n // the value of the `style` attribute, if there is one.\n visit(tree, 'element', sanitizeStyle);\n };\n })\n .use(createLazyLoadImagesPlugin(options?.lazyLoadImages))\n .use(rehypeStringify)\n .process(text);\n\n return file.toString();\n}\n\n/**\n * Sanitizes a given HTML string by removing dangerous tags and attributes.\n *\n * @param html - The string containing HTML to sanitize.\n * @param whitelist - Optional whitelist of custom components.\n * @returns The sanitized HTML string.\n */\nexport async function sanitizeHTML(\n html: string,\n whitelist?: CustomElementDefinition[],\n): Promise<string> {\n const file = await unified()\n .use(rehypeParse)\n .use(rehypeSanitize, {\n ...getWhiteList(whitelist ?? []),\n })\n .use(() => {\n return (tree: Node) => {\n // Run the sanitizeStyle function on all elements, to sanitize\n // the value of the `style` attribute, if there is one.\n visit(tree, 'element', sanitizeStyle);\n };\n })\n .use(rehypeStringify)\n .process(html);\n\n return file.toString();\n}\n\nfunction getWhiteList(allowedComponents: CustomElementDefinition[]): Schema {\n const defaultSchemaClone = [...(defaultSchema.attributes['*'] ?? [])];\n const asteriskAttributeWhitelist = defaultSchemaClone.filter((attr) => {\n return attr !== 'height';\n });\n asteriskAttributeWhitelist.push('style');\n\n const whitelist: Schema = {\n ...defaultSchema,\n tagNames: [\n ...(defaultSchema.tagNames || []),\n ...allowedComponents.map((component) => component.tagName),\n ],\n attributes: {\n ...defaultSchema.attributes,\n p: [\n ...(defaultSchema.attributes.p ?? []),\n ['className', 'MsoNormal'],\n ], // Allow the class 'MsoNormal' on <p> elements\n a: [...(defaultSchema.attributes.a ?? []), 'referrerpolicy'], // Allow referrerpolicy on <a> elements\n '*': asteriskAttributeWhitelist,\n },\n };\n\n for (const component of allowedComponents) {\n whitelist.attributes[component.tagName] = component.attributes;\n }\n\n return whitelist;\n}\n\n/**\n * Options for markdownToHTML.\n */\nexport interface MarkdownToHTMLOptions {\n /**\n * Set to `true` to convert all soft line breaks to hard line breaks.\n */\n forceHardLineBreaks?: boolean;\n whitelist?: CustomElementDefinition[];\n lazyLoadImages?: boolean;\n}\n"]}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { toggleMark, setBlockType, wrapIn, lift } from 'prosemirror-commands';
|
|
2
2
|
import { findWrapping, liftTarget } from 'prosemirror-transform';
|
|
3
|
-
import { TextSelection
|
|
3
|
+
import { TextSelection } from 'prosemirror-state';
|
|
4
4
|
import { EditorMenuTypes, LevelMapping } from './types';
|
|
5
|
+
import { getLinkAttributes } from '../plugins/link/utils';
|
|
5
6
|
const setActiveMethodForMark = (command, markType) => {
|
|
6
7
|
command.active = (state) => {
|
|
7
8
|
const { from, $from, to, empty } = state.selection;
|
|
@@ -44,24 +45,15 @@ const setActiveMethodForWrap = (command, nodeType) => {
|
|
|
44
45
|
const createInsertLinkCommand = (schema, _, link) => {
|
|
45
46
|
const command = (state, dispatch) => {
|
|
46
47
|
const { from, to } = state.selection;
|
|
48
|
+
const linkMark = schema.marks.link.create(getLinkAttributes(link.href, link.href));
|
|
47
49
|
if (from === to) {
|
|
48
50
|
// If no text is selected, insert new text with link
|
|
49
|
-
const linkMark = schema.marks.link.create({
|
|
50
|
-
href: link.href,
|
|
51
|
-
title: link.href,
|
|
52
|
-
target: isExternalLink(link.href) ? '_blank' : null,
|
|
53
|
-
});
|
|
54
51
|
const linkText = link.text || link.href;
|
|
55
52
|
const newLink = schema.text(linkText, [linkMark]);
|
|
56
53
|
dispatch(state.tr.insert(from, newLink));
|
|
57
54
|
}
|
|
58
55
|
else {
|
|
59
56
|
// If text is selected, replace selected text with link text
|
|
60
|
-
const linkMark = schema.marks.link.create({
|
|
61
|
-
href: link.href,
|
|
62
|
-
title: link.href,
|
|
63
|
-
target: isExternalLink(link.href) ? '_blank' : null,
|
|
64
|
-
});
|
|
65
57
|
const selectedText = state.doc.textBetween(from, to, ' ');
|
|
66
58
|
const newLink = schema.text(link.text || selectedText, [linkMark]);
|
|
67
59
|
dispatch(state.tr.replaceWith(from, to, newLink));
|
|
@@ -135,15 +127,6 @@ const toggleNodeType = (schema, type, attrs = {}, shouldWrap = false) => {
|
|
|
135
127
|
return false;
|
|
136
128
|
};
|
|
137
129
|
};
|
|
138
|
-
export const isValidUrl = (text) => {
|
|
139
|
-
try {
|
|
140
|
-
new URL(text);
|
|
141
|
-
}
|
|
142
|
-
catch (_a) {
|
|
143
|
-
return false;
|
|
144
|
-
}
|
|
145
|
-
return true;
|
|
146
|
-
};
|
|
147
130
|
const createSetNodeTypeCommand = (schema, nodeType, level) => {
|
|
148
131
|
const type = schema.nodes[nodeType];
|
|
149
132
|
if (!type) {
|
|
@@ -216,27 +199,6 @@ const createListCommand = (schema, listType) => {
|
|
|
216
199
|
setActiveMethodForWrap(command, type);
|
|
217
200
|
return command;
|
|
218
201
|
};
|
|
219
|
-
const copyPasteLinkCommand = (state, dispatch) => {
|
|
220
|
-
const { from, to } = state.selection;
|
|
221
|
-
if (from === to) {
|
|
222
|
-
return false;
|
|
223
|
-
}
|
|
224
|
-
const clipboardData = window.clipboardData;
|
|
225
|
-
if (!clipboardData) {
|
|
226
|
-
return false;
|
|
227
|
-
}
|
|
228
|
-
const copyPastedText = clipboardData.getData('text');
|
|
229
|
-
if (!isValidUrl(copyPastedText)) {
|
|
230
|
-
return false;
|
|
231
|
-
}
|
|
232
|
-
const linkMark = state.schema.marks.link.create({
|
|
233
|
-
href: copyPastedText,
|
|
234
|
-
target: isExternalLink(copyPastedText) ? '_blank' : null,
|
|
235
|
-
});
|
|
236
|
-
const selectedText = state.doc.textBetween(from, to, ' ');
|
|
237
|
-
const newLink = state.schema.text(selectedText, [linkMark]);
|
|
238
|
-
dispatch(state.tr.replaceWith(from, to, newLink));
|
|
239
|
-
};
|
|
240
202
|
const commandMapping = {
|
|
241
203
|
strong: createToggleMarkCommand,
|
|
242
204
|
em: createToggleMarkCommand,
|
|
@@ -275,7 +237,6 @@ export class MenuCommandFactory {
|
|
|
275
237
|
'Mod-Shift-X': this.getCommand(EditorMenuTypes.Strikethrough),
|
|
276
238
|
'Mod-`': this.getCommand(EditorMenuTypes.Code),
|
|
277
239
|
'Mod-Shift-C': this.getCommand(EditorMenuTypes.CodeBlock),
|
|
278
|
-
'Mod-v': copyPasteLinkCommand,
|
|
279
240
|
};
|
|
280
241
|
}
|
|
281
242
|
}
|
package/dist/collection/components/text-editor/prosemirror-adapter/menu/menu-commands.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"menu-commands.js","sourceRoot":"","sources":["../../../../../src/components/text-editor/prosemirror-adapter/menu/menu-commands.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAE9E,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACjE,OAAO,EAIH,aAAa,GAChB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,eAAe,EAAkB,YAAY,EAAE,MAAM,SAAS,CAAC;AAiBxE,MAAM,sBAAsB,GAAG,CAC3B,OAA0B,EAC1B,QAAkB,EACpB,EAAE;EACA,OAAO,CAAC,MAAM,GAAG,CAAC,KAAK,EAAE,EAAE;IACvB,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,EA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AC,eAAe,CAAC,IAAI,CAAC;MAC9C,aAAa,EAAE,IAAI,CAAC,UAAU,CAAC,eAAe,CAAC,SAAS,CAAC;MACzD,OAAO,EAAE,oBAAoB;KAChC,CAAC;EACN,CAAC;CACJ","sourcesContent":["import { toggleMark, setBlockType, wrapIn, lift } from 'prosemirror-commands';\nimport { Schema, MarkType, NodeType, Attrs } from 'prosemirror-model';\nimport { findWrapping, liftTarget } from 'prosemirror-transform';\nimport {\n Command,\n EditorState,\n Transaction,\n TextSelection,\n} from 'prosemirror-state';\nimport { EditorMenuTypes, EditorTextLink, LevelMapping } from './types';\n\ntype CommandFunction = (\n schema: Schema,\n mark: EditorMenuTypes,\n link?: EditorTextLink,\n) => CommandWithActive;\n\ninterface CommandMapping {\n [key: string]: CommandFunction;\n}\n\nexport interface CommandWithActive extends Command {\n active?: (state: EditorState) => boolean;\n allowed?: (state: EditorState) => boolean;\n}\n\nconst setActiveMethodForMark = (\n command: CommandWithActive,\n markType: MarkType,\n) => {\n command.active = (state) => {\n const { from, $from, to, empty } = state.selection;\n if (empty) {\n return !!markType.isInSet(state.storedMarks || $from.marks());\n } else {\n return state.doc.rangeHasMark(from, to, markType);\n }\n };\n};\n\nconst setActiveMethodForNode = (\n command: CommandWithActive,\n nodeType: NodeType,\n level?: number,\n) => {\n command.active = (state) => {\n const { $from } = state.selection;\n const node = $from.node($from.depth);\n\n if (node && node.type.name === nodeType.name) {\n if (nodeType.name === LevelMapping.Heading && level) {\n return node.attrs.level === level;\n }\n\n return true;\n }\n\n return false;\n };\n};\n\nconst setActiveMethodForWrap = (\n command: CommandWithActive,\n nodeType: NodeType,\n) => {\n command.active = (state) => {\n const { from, to } = state.selection;\n\n for (let pos = from; pos <= to; pos++) {\n const resolvedPos = state.doc.resolve(pos);\n for (let i = resolvedPos.depth; i > 0; i--) {\n const node = resolvedPos.node(i);\n if (node && node.type.name === nodeType.name) {\n return true;\n }\n }\n }\n\n return false;\n };\n};\n\nconst createInsertLinkCommand: CommandFunction = (\n schema: Schema,\n _: EditorMenuTypes,\n link?: EditorTextLink,\n): CommandWithActive => {\n const command: Command = (state, dispatch) => {\n const { from, to } = state.selection;\n if (from === to) {\n // If no text is selected, insert new text with link\n const linkMark = schema.marks.link.create({\n href: link.href,\n title: link.href,\n target: isExternalLink(link.href) ? '_blank' : null,\n });\n const linkText = link.text || link.href;\n const newLink = schema.text(linkText, [linkMark]);\n dispatch(state.tr.insert(from, newLink));\n } else {\n // If text is selected, replace selected text with link text\n const linkMark = schema.marks.link.create({\n href: link.href,\n title: link.href,\n target: isExternalLink(link.href) ? '_blank' : null,\n });\n const selectedText = state.doc.textBetween(from, to, ' ');\n const newLink = schema.text(link.text || selectedText, [linkMark]);\n dispatch(state.tr.replaceWith(from, to, newLink));\n }\n\n return true;\n };\n\n setActiveMethodForMark(command, schema.marks.link);\n\n return command;\n};\n\nconst createToggleMarkCommand = (\n schema: Schema,\n markName: string,\n link?: EditorTextLink,\n): CommandWithActive => {\n const markType: MarkType | undefined = schema.marks[markName];\n if (!markType) {\n throw new Error(`Mark \"${markName}\" not found in schema`);\n }\n\n const attrs = getAttributes(markName, link);\n\n const command: CommandWithActive = toggleMark(markType, attrs);\n setActiveMethodForMark(command, markType);\n\n return command;\n};\n\nconst getAttributes = (\n markName: string,\n link: EditorTextLink,\n): Attrs | null => {\n if (markName === EditorMenuTypes.Link && link.href) {\n return {\n href: link.href,\n target: isExternalLink(link.href) ? '_blank' : null,\n };\n }\n\n return undefined;\n};\n\nexport const isExternalLink = (url: string): boolean => {\n return !url.startsWith(window.location.origin);\n};\n\n/**\n * Toggles or wraps a node type based on the selection and parameters.\n * - Toggles to paragraph if the selection is of the specified type.\n * - Lifts content out if already wrapped in the specified type.\n * - Wraps or sets the selection to the specified type based on `shouldWrap`.\n * @param schema - ProseMirror schema.\n * @param type - Block type name to toggle.\n * @param attrs - Attributes for the block type.\n * @param shouldWrap - Wrap selection if true, otherwise directly set the block type for the selection.\n * @returns A command based on selection and action needed.\n */\nconst toggleNodeType = (\n schema: Schema,\n type: string,\n attrs: Attrs = {},\n shouldWrap: boolean = false,\n): Command => {\n const nodeType = schema.nodes[type];\n const paragraphType = schema.nodes.paragraph;\n\n return (state, dispatch) => {\n const { $from, $to } = state.selection;\n\n const hasActiveWrap = $from.node($from.depth - 1).type === nodeType;\n\n if (\n state.selection instanceof TextSelection &&\n // Ensure selection is within the same parent block\n // We don't want toggling block types across multiple blocks\n $from.sameParent($from.doc.resolve($to.pos))\n ) {\n if ($from.parent.type === nodeType) {\n if (dispatch) {\n dispatch(\n state.tr.setBlockType(\n $from.pos,\n $to.pos,\n paragraphType,\n ),\n );\n }\n\n return true;\n } else {\n if (hasActiveWrap) {\n return lift(state, dispatch);\n }\n\n if (shouldWrap) {\n return wrapIn(nodeType, attrs)(state, dispatch);\n } else {\n return setBlockType(nodeType, attrs)(state, dispatch);\n }\n }\n }\n\n return false;\n };\n};\n\nexport const isValidUrl = (text: string): boolean => {\n try {\n new URL(text);\n } catch {\n return false;\n }\n\n return true;\n};\n\nconst createSetNodeTypeCommand = (\n schema: Schema,\n nodeType: string,\n level?: number,\n): CommandWithActive => {\n const type: NodeType | undefined = schema.nodes[nodeType];\n if (!type) {\n throw new Error(`Node type \"${nodeType}\" not found in schema`);\n }\n\n let command: CommandWithActive;\n if (nodeType === LevelMapping.Heading && level) {\n command = toggleNodeType(schema, LevelMapping.Heading, {\n level: level,\n });\n } else if (nodeType === EditorMenuTypes.CodeBlock) {\n command = toggleNodeType(schema, EditorMenuTypes.CodeBlock);\n } else {\n command = setBlockType(type);\n }\n\n setActiveMethodForNode(command, type, level);\n\n return command;\n};\n\nconst createWrapInCommand = (\n schema: Schema,\n nodeType: string,\n): CommandWithActive => {\n const type: NodeType | undefined = schema.nodes[nodeType];\n if (!type) {\n throw new Error(`Node type \"${nodeType}\" not found in schema`);\n }\n\n let command: CommandWithActive;\n if (nodeType === EditorMenuTypes.Blockquote) {\n command = toggleNodeType(schema, EditorMenuTypes.Blockquote, {}, true);\n } else {\n command = wrapIn(type);\n }\n\n setActiveMethodForWrap(command, type);\n\n return command;\n};\n\nconst toggleList = (listType) => {\n return (state, dispatch) => {\n const { $from, $to } = state.selection;\n const range = $from.blockRange($to);\n\n if (!range) {\n return false;\n }\n\n const wrapping = range && findWrapping(range, listType);\n\n if (wrapping) {\n // Wrap the selection in a list\n if (dispatch) {\n dispatch(state.tr.wrap(range, wrapping).scrollIntoView());\n }\n\n return true;\n } else {\n // Check if we are in a list item and lift out of the list\n const liftRange = range && liftTarget(range);\n if (liftRange !== null) {\n if (dispatch) {\n dispatch(state.tr.lift(range, liftRange).scrollIntoView());\n }\n\n return true;\n }\n\n return false;\n }\n };\n};\n\nconst createListCommand = (\n schema: Schema,\n listType: string,\n): CommandWithActive => {\n const type: NodeType | undefined = schema.nodes[listType];\n if (!type) {\n throw new Error(`List type \"${listType}\" not found in schema`);\n }\n\n const command: CommandWithActive = toggleList(type);\n setActiveMethodForWrap(command, type);\n\n return command;\n};\n\nconst copyPasteLinkCommand: Command = (\n state: EditorState,\n dispatch: (tr: Transaction) => void,\n) => {\n const { from, to } = state.selection;\n if (from === to) {\n return false;\n }\n\n const clipboardData = (window as any).clipboardData;\n if (!clipboardData) {\n return false;\n }\n\n const copyPastedText = clipboardData.getData('text');\n if (!isValidUrl(copyPastedText)) {\n return false;\n }\n\n const linkMark = state.schema.marks.link.create({\n href: copyPastedText,\n target: isExternalLink(copyPastedText) ? '_blank' : null,\n });\n\n const selectedText = state.doc.textBetween(from, to, ' ');\n const newLink = state.schema.text(selectedText, [linkMark]);\n dispatch(state.tr.replaceWith(from, to, newLink));\n};\n\nconst commandMapping: CommandMapping = {\n strong: createToggleMarkCommand,\n em: createToggleMarkCommand,\n underline: createToggleMarkCommand,\n strikethrough: createToggleMarkCommand,\n code: createToggleMarkCommand,\n link: createInsertLinkCommand,\n headerlevel1: (schema) =>\n createSetNodeTypeCommand(\n schema,\n LevelMapping.Heading,\n LevelMapping.one,\n ),\n headerlevel2: (schema) =>\n createSetNodeTypeCommand(\n schema,\n LevelMapping.Heading,\n LevelMapping.two,\n ),\n headerlevel3: (schema) =>\n createSetNodeTypeCommand(\n schema,\n LevelMapping.Heading,\n LevelMapping.three,\n ),\n blockquote: (schema) =>\n createWrapInCommand(schema, EditorMenuTypes.Blockquote),\n /* eslint-disable camelcase */\n code_block: (schema) =>\n createSetNodeTypeCommand(schema, EditorMenuTypes.CodeBlock),\n ordered_list: (schema) =>\n createListCommand(schema, EditorMenuTypes.OrderedList),\n bullet_list: (schema) =>\n createListCommand(schema, EditorMenuTypes.BulletList),\n /* eslint-enable camelcase */\n};\n\nexport class MenuCommandFactory {\n private schema: Schema;\n\n constructor(schema: Schema) {\n this.schema = schema;\n }\n\n public getCommand(mark: EditorMenuTypes, link?: EditorTextLink) {\n const commandFunc = commandMapping[mark];\n if (!commandFunc) {\n throw new Error(`The Mark \"${mark}\" is not supported`);\n }\n\n return commandFunc(this.schema, mark, link);\n }\n\n buildKeymap() {\n return {\n 'Mod-B': this.getCommand(EditorMenuTypes.Bold),\n 'Mod-I': this.getCommand(EditorMenuTypes.Italic),\n 'Mod-Shift-1': this.getCommand(EditorMenuTypes.HeaderLevel1),\n 'Mod-Shift-2': this.getCommand(EditorMenuTypes.HeaderLevel2),\n 'Mod-Shift-3': this.getCommand(EditorMenuTypes.HeaderLevel3),\n 'Mod-Shift-X': this.getCommand(EditorMenuTypes.Strikethrough),\n 'Mod-`': this.getCommand(EditorMenuTypes.Code),\n 'Mod-Shift-C': this.getCommand(EditorMenuTypes.CodeBlock),\n 'Mod-v': copyPasteLinkCommand,\n };\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"menu-commands.js","sourceRoot":"","sources":["../../../../../src/components/text-editor/prosemirror-adapter/menu/menu-commands.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAE9E,OAAO,EAAE,YAAY,EAAE,UAAU,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AAU,CAAC,eAAe,CAAC,IAAI,CAAC;MAC9C,aAAa,EAAE,IAAI,CAAC,UAAU,CAAC,eAAe,CAAC,SAAS,CAAC;KAC5D,CAAC;EACN,CAAC;CACJ","sourcesContent":["import { toggleMark, setBlockType, wrapIn, lift } from 'prosemirror-commands';\nimport { Schema, MarkType, NodeType, Attrs } from 'prosemirror-model';\nimport { findWrapping, liftTarget } from 'prosemirror-transform';\nimport { Command, EditorState, TextSelection } from 'prosemirror-state';\nimport { EditorMenuTypes, EditorTextLink, LevelMapping } from './types';\nimport { getLinkAttributes } from '../plugins/link/utils';\n\ntype CommandFunction = (\n schema: Schema,\n mark: EditorMenuTypes,\n link?: EditorTextLink,\n) => CommandWithActive;\n\ninterface CommandMapping {\n [key: string]: CommandFunction;\n}\n\nexport interface CommandWithActive extends Command {\n active?: (state: EditorState) => boolean;\n allowed?: (state: EditorState) => boolean;\n}\n\nconst setActiveMethodForMark = (\n command: CommandWithActive,\n markType: MarkType,\n) => {\n command.active = (state) => {\n const { from, $from, to, empty } = state.selection;\n if (empty) {\n return !!markType.isInSet(state.storedMarks || $from.marks());\n } else {\n return state.doc.rangeHasMark(from, to, markType);\n }\n };\n};\n\nconst setActiveMethodForNode = (\n command: CommandWithActive,\n nodeType: NodeType,\n level?: number,\n) => {\n command.active = (state) => {\n const { $from } = state.selection;\n const node = $from.node($from.depth);\n\n if (node && node.type.name === nodeType.name) {\n if (nodeType.name === LevelMapping.Heading && level) {\n return node.attrs.level === level;\n }\n\n return true;\n }\n\n return false;\n };\n};\n\nconst setActiveMethodForWrap = (\n command: CommandWithActive,\n nodeType: NodeType,\n) => {\n command.active = (state) => {\n const { from, to } = state.selection;\n\n for (let pos = from; pos <= to; pos++) {\n const resolvedPos = state.doc.resolve(pos);\n for (let i = resolvedPos.depth; i > 0; i--) {\n const node = resolvedPos.node(i);\n if (node && node.type.name === nodeType.name) {\n return true;\n }\n }\n }\n\n return false;\n };\n};\n\nconst createInsertLinkCommand: CommandFunction = (\n schema: Schema,\n _: EditorMenuTypes,\n link?: EditorTextLink,\n): CommandWithActive => {\n const command: Command = (state, dispatch) => {\n const { from, to } = state.selection;\n const linkMark = schema.marks.link.create(\n getLinkAttributes(link.href, link.href),\n );\n\n if (from === to) {\n // If no text is selected, insert new text with link\n const linkText = link.text || link.href;\n const newLink = schema.text(linkText, [linkMark]);\n dispatch(state.tr.insert(from, newLink));\n } else {\n // If text is selected, replace selected text with link text\n const selectedText = state.doc.textBetween(from, to, ' ');\n const newLink = schema.text(link.text || selectedText, [linkMark]);\n dispatch(state.tr.replaceWith(from, to, newLink));\n }\n\n return true;\n };\n\n setActiveMethodForMark(command, schema.marks.link);\n\n return command;\n};\n\nconst createToggleMarkCommand = (\n schema: Schema,\n markName: string,\n link?: EditorTextLink,\n): CommandWithActive => {\n const markType: MarkType | undefined = schema.marks[markName];\n if (!markType) {\n throw new Error(`Mark \"${markName}\" not found in schema`);\n }\n\n const attrs = getAttributes(markName, link);\n\n const command: CommandWithActive = toggleMark(markType, attrs);\n setActiveMethodForMark(command, markType);\n\n return command;\n};\n\nconst getAttributes = (\n markName: string,\n link: EditorTextLink,\n): Attrs | null => {\n if (markName === EditorMenuTypes.Link && link.href) {\n return {\n href: link.href,\n target: isExternalLink(link.href) ? '_blank' : null,\n };\n }\n\n return undefined;\n};\n\nexport const isExternalLink = (url: string): boolean => {\n return !url.startsWith(window.location.origin);\n};\n\n/**\n * Toggles or wraps a node type based on the selection and parameters.\n * - Toggles to paragraph if the selection is of the specified type.\n * - Lifts content out if already wrapped in the specified type.\n * - Wraps or sets the selection to the specified type based on `shouldWrap`.\n * @param schema - ProseMirror schema.\n * @param type - Block type name to toggle.\n * @param attrs - Attributes for the block type.\n * @param shouldWrap - Wrap selection if true, otherwise directly set the block type for the selection.\n * @returns A command based on selection and action needed.\n */\nconst toggleNodeType = (\n schema: Schema,\n type: string,\n attrs: Attrs = {},\n shouldWrap: boolean = false,\n): Command => {\n const nodeType = schema.nodes[type];\n const paragraphType = schema.nodes.paragraph;\n\n return (state, dispatch) => {\n const { $from, $to } = state.selection;\n\n const hasActiveWrap = $from.node($from.depth - 1).type === nodeType;\n\n if (\n state.selection instanceof TextSelection &&\n // Ensure selection is within the same parent block\n // We don't want toggling block types across multiple blocks\n $from.sameParent($from.doc.resolve($to.pos))\n ) {\n if ($from.parent.type === nodeType) {\n if (dispatch) {\n dispatch(\n state.tr.setBlockType(\n $from.pos,\n $to.pos,\n paragraphType,\n ),\n );\n }\n\n return true;\n } else {\n if (hasActiveWrap) {\n return lift(state, dispatch);\n }\n\n if (shouldWrap) {\n return wrapIn(nodeType, attrs)(state, dispatch);\n } else {\n return setBlockType(nodeType, attrs)(state, dispatch);\n }\n }\n }\n\n return false;\n };\n};\n\nconst createSetNodeTypeCommand = (\n schema: Schema,\n nodeType: string,\n level?: number,\n): CommandWithActive => {\n const type: NodeType | undefined = schema.nodes[nodeType];\n if (!type) {\n throw new Error(`Node type \"${nodeType}\" not found in schema`);\n }\n\n let command: CommandWithActive;\n if (nodeType === LevelMapping.Heading && level) {\n command = toggleNodeType(schema, LevelMapping.Heading, {\n level: level,\n });\n } else if (nodeType === EditorMenuTypes.CodeBlock) {\n command = toggleNodeType(schema, EditorMenuTypes.CodeBlock);\n } else {\n command = setBlockType(type);\n }\n\n setActiveMethodForNode(command, type, level);\n\n return command;\n};\n\nconst createWrapInCommand = (\n schema: Schema,\n nodeType: string,\n): CommandWithActive => {\n const type: NodeType | undefined = schema.nodes[nodeType];\n if (!type) {\n throw new Error(`Node type \"${nodeType}\" not found in schema`);\n }\n\n let command: CommandWithActive;\n if (nodeType === EditorMenuTypes.Blockquote) {\n command = toggleNodeType(schema, EditorMenuTypes.Blockquote, {}, true);\n } else {\n command = wrapIn(type);\n }\n\n setActiveMethodForWrap(command, type);\n\n return command;\n};\n\nconst toggleList = (listType) => {\n return (state, dispatch) => {\n const { $from, $to } = state.selection;\n const range = $from.blockRange($to);\n\n if (!range) {\n return false;\n }\n\n const wrapping = range && findWrapping(range, listType);\n\n if (wrapping) {\n // Wrap the selection in a list\n if (dispatch) {\n dispatch(state.tr.wrap(range, wrapping).scrollIntoView());\n }\n\n return true;\n } else {\n // Check if we are in a list item and lift out of the list\n const liftRange = range && liftTarget(range);\n if (liftRange !== null) {\n if (dispatch) {\n dispatch(state.tr.lift(range, liftRange).scrollIntoView());\n }\n\n return true;\n }\n\n return false;\n }\n };\n};\n\nconst createListCommand = (\n schema: Schema,\n listType: string,\n): CommandWithActive => {\n const type: NodeType | undefined = schema.nodes[listType];\n if (!type) {\n throw new Error(`List type \"${listType}\" not found in schema`);\n }\n\n const command: CommandWithActive = toggleList(type);\n setActiveMethodForWrap(command, type);\n\n return command;\n};\n\nconst commandMapping: CommandMapping = {\n strong: createToggleMarkCommand,\n em: createToggleMarkCommand,\n underline: createToggleMarkCommand,\n strikethrough: createToggleMarkCommand,\n code: createToggleMarkCommand,\n link: createInsertLinkCommand,\n headerlevel1: (schema) =>\n createSetNodeTypeCommand(\n schema,\n LevelMapping.Heading,\n LevelMapping.one,\n ),\n headerlevel2: (schema) =>\n createSetNodeTypeCommand(\n schema,\n LevelMapping.Heading,\n LevelMapping.two,\n ),\n headerlevel3: (schema) =>\n createSetNodeTypeCommand(\n schema,\n LevelMapping.Heading,\n LevelMapping.three,\n ),\n blockquote: (schema) =>\n createWrapInCommand(schema, EditorMenuTypes.Blockquote),\n /* eslint-disable camelcase */\n code_block: (schema) =>\n createSetNodeTypeCommand(schema, EditorMenuTypes.CodeBlock),\n ordered_list: (schema) =>\n createListCommand(schema, EditorMenuTypes.OrderedList),\n bullet_list: (schema) =>\n createListCommand(schema, EditorMenuTypes.BulletList),\n /* eslint-enable camelcase */\n};\n\nexport class MenuCommandFactory {\n private schema: Schema;\n\n constructor(schema: Schema) {\n this.schema = schema;\n }\n\n public getCommand(mark: EditorMenuTypes, link?: EditorTextLink) {\n const commandFunc = commandMapping[mark];\n if (!commandFunc) {\n throw new Error(`The Mark \"${mark}\" is not supported`);\n }\n\n return commandFunc(this.schema, mark, link);\n }\n\n buildKeymap() {\n return {\n 'Mod-B': this.getCommand(EditorMenuTypes.Bold),\n 'Mod-I': this.getCommand(EditorMenuTypes.Italic),\n 'Mod-Shift-1': this.getCommand(EditorMenuTypes.HeaderLevel1),\n 'Mod-Shift-2': this.getCommand(EditorMenuTypes.HeaderLevel2),\n 'Mod-Shift-3': this.getCommand(EditorMenuTypes.HeaderLevel3),\n 'Mod-Shift-X': this.getCommand(EditorMenuTypes.Strikethrough),\n 'Mod-`': this.getCommand(EditorMenuTypes.Code),\n 'Mod-Shift-C': this.getCommand(EditorMenuTypes.CodeBlock),\n };\n }\n}\n"]}
|
package/dist/collection/components/text-editor/prosemirror-adapter/plugins/link/link-mark-spec.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export const linkMarkSpec = {
|
|
2
|
+
attrs: {
|
|
3
|
+
href: { default: '' },
|
|
4
|
+
title: { default: null },
|
|
5
|
+
target: { default: null },
|
|
6
|
+
rel: { default: null },
|
|
7
|
+
referrerpolicy: { default: null },
|
|
8
|
+
},
|
|
9
|
+
inclusive: false,
|
|
10
|
+
parseDOM: [
|
|
11
|
+
{
|
|
12
|
+
tag: 'a[href]',
|
|
13
|
+
getAttrs: (dom) => {
|
|
14
|
+
return {
|
|
15
|
+
href: dom.getAttribute('href') || '',
|
|
16
|
+
title: dom.getAttribute('title'),
|
|
17
|
+
target: dom.getAttribute('target'),
|
|
18
|
+
rel: dom.getAttribute('rel'),
|
|
19
|
+
referrerpolicy: dom.getAttribute('referrerpolicy'),
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
toDOM: (mark) => {
|
|
25
|
+
const target = mark.attrs.target || null;
|
|
26
|
+
const securityAttrs = {
|
|
27
|
+
rel: target === '_blank' ? 'noopener noreferrer' : null,
|
|
28
|
+
referrerpolicy: target === '_blank' ? 'noreferrer' : null,
|
|
29
|
+
};
|
|
30
|
+
return ['a', Object.assign(Object.assign({}, mark.attrs), securityAttrs), 0];
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=link-mark-spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"link-mark-spec.js","sourceRoot":"","sources":["../../../../../../src/components/text-editor/prosemirror-adapter/plugins/link/link-mark-spec.ts"],"names":[],"mappings":"AAUA,MAAM,CAAC,MAAM,YAAY,GAAa;EAClC,KAAK,EAAE;IACH,IAAI,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;IACrB,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;IACxB,MAAM,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;IACzB,GAAG,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;IACtB,cAAc,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;GACpC;EACD,SAAS,EAAE,KAAK;EAChB,QAAQ,EAAE;IACN;MACI,GAAG,EAAE,SAAS;MACd,QAAQ,EAAE,CAAC,GAAgB,EAAiB,EAAE;QAC1C,OAAO;UACH,IAAI,EAAE,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,EAAE;UACpC,KAAK,EAAE,GAAG,CAAC,YAAY,CAAC,OAAO,CAAC;UAChC,MAAM,EAAE,GAAG,CAAC,YAAY,CAAC,QAAQ,CAAC;UAClC,GAAG,EAAE,GAAG,CAAC,YAAY,CAAC,KAAK,CAAC;UAC5B,cAAc,EAAE,GAAG,CAAC,YAAY,CAAC,gBAAgB,CAAC;SACrD,CAAC;MACN,CAAC;KACJ;GACJ;EACD,KAAK,EAAE,CAAC,IAAI,EAAiB,EAAE;IAC3B,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,IAAI,IAAI,CAAC;IAEzC,MAAM,aAAa,GAAG;MAClB,GAAG,EAAE,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,IAAI;MACvD,cAAc,EAAE,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI;KAC5D,CAAC;IAEF,OAAO,CAAC,GAAG,kCAAO,IAAI,CAAC,KAAK,GAAK,aAAa,GAAI,CAAC,CAAC,CAAC;EACzD,CAAC;CACJ,CAAC","sourcesContent":["import { MarkSpec, DOMOutputSpec } from 'prosemirror-model';\n\nexport interface LinkMarkAttrs {\n href: string;\n title: string | null;\n target: string | null;\n rel: string | null;\n referrerpolicy: string | null;\n}\n\nexport const linkMarkSpec: MarkSpec = {\n attrs: {\n href: { default: '' },\n title: { default: null },\n target: { default: null },\n rel: { default: null },\n referrerpolicy: { default: null },\n },\n inclusive: false,\n parseDOM: [\n {\n tag: 'a[href]',\n getAttrs: (dom: HTMLElement): LinkMarkAttrs => {\n return {\n href: dom.getAttribute('href') || '',\n title: dom.getAttribute('title'),\n target: dom.getAttribute('target'),\n rel: dom.getAttribute('rel'),\n referrerpolicy: dom.getAttribute('referrerpolicy'),\n };\n },\n },\n ],\n toDOM: (mark): DOMOutputSpec => {\n const target = mark.attrs.target || null;\n\n const securityAttrs = {\n rel: target === '_blank' ? 'noopener noreferrer' : null,\n referrerpolicy: target === '_blank' ? 'noreferrer' : null,\n };\n\n return ['a', { ...mark.attrs, ...securityAttrs }, 0];\n },\n};\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Plugin, PluginKey, TextSelection } from 'prosemirror-state';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { Fragment } from 'prosemirror-model';
|
|
3
|
+
import { EditorMenuTypes, MouseButtons } from '../../menu/types';
|
|
4
|
+
import { getLinkAttributes } from './utils';
|
|
5
5
|
export const linkPluginKey = new PluginKey('linkPlugin');
|
|
6
6
|
const updateLink = (view, updateLinkCallback) => {
|
|
7
7
|
const { from, to } = view.state.selection;
|
|
@@ -122,32 +122,145 @@ const processClickEvent = (view, event) => {
|
|
|
122
122
|
}, DOUBLE_CLICK_DELAY);
|
|
123
123
|
return true;
|
|
124
124
|
};
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
125
|
+
/**
|
|
126
|
+
* Regular expression for matching URLs, mailto links, and phone links
|
|
127
|
+
*/
|
|
128
|
+
const URL_REGEX = /(https?:\/\/[^\s<>"']+|mailto:[^\s<>"']+|tel:[^\s<>"']+)/g;
|
|
129
|
+
/**
|
|
130
|
+
* Checks if the text contains any URLs, mailto links, or phone links
|
|
131
|
+
*/
|
|
132
|
+
const hasUrls = (text) => {
|
|
133
|
+
// Reset regex before use
|
|
134
|
+
URL_REGEX.lastIndex = 0;
|
|
135
|
+
return URL_REGEX.test(text);
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Creates a text node with the provided content
|
|
139
|
+
*/
|
|
140
|
+
const createTextNode = (schema, content) => {
|
|
141
|
+
return schema.text(content);
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* Creates a link node with the provided URL
|
|
145
|
+
*/
|
|
146
|
+
const createLinkNode = (schema, url) => {
|
|
147
|
+
const linkMark = schema.marks.link.create(getLinkAttributes(url, url));
|
|
148
|
+
return schema.text(url, [linkMark]);
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Finds all link matches in the provided text
|
|
152
|
+
*/
|
|
153
|
+
const findLinkMatches = (text) => {
|
|
154
|
+
const matches = [];
|
|
155
|
+
let match;
|
|
156
|
+
// Reset regex before use
|
|
157
|
+
URL_REGEX.lastIndex = 0;
|
|
158
|
+
while ((match = URL_REGEX.exec(text)) !== null) {
|
|
159
|
+
matches.push({
|
|
160
|
+
url: match[0],
|
|
161
|
+
start: match.index,
|
|
162
|
+
end: match.index + match[0].length,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
return matches;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Creates text nodes with links for any URLs, mailto links, or phone links found in the text
|
|
169
|
+
*/
|
|
170
|
+
const createNodesWithLinks = (text, schema) => {
|
|
171
|
+
const nodes = [];
|
|
172
|
+
const matches = findLinkMatches(text);
|
|
173
|
+
if (matches.length === 0) {
|
|
174
|
+
// No links found, just return the text as a single node
|
|
175
|
+
return [createTextNode(schema, text)];
|
|
176
|
+
}
|
|
177
|
+
let lastIndex = 0;
|
|
178
|
+
// Process each match
|
|
179
|
+
for (const match of matches) {
|
|
180
|
+
// Add text before the current link if any
|
|
181
|
+
if (match.start > lastIndex) {
|
|
182
|
+
nodes.push(createTextNode(schema, text.slice(lastIndex, match.start)));
|
|
183
|
+
}
|
|
184
|
+
// Add the link node
|
|
185
|
+
nodes.push(createLinkNode(schema, match.url));
|
|
186
|
+
lastIndex = match.end;
|
|
187
|
+
}
|
|
188
|
+
// Add any remaining text after the last link
|
|
189
|
+
if (lastIndex < text.length) {
|
|
190
|
+
nodes.push(createTextNode(schema, text.slice(lastIndex)));
|
|
191
|
+
}
|
|
192
|
+
return nodes;
|
|
193
|
+
};
|
|
194
|
+
/**
|
|
195
|
+
* Pastes nodes at the current selection
|
|
196
|
+
* @param view - The editor view
|
|
197
|
+
* @param nodes - Array of nodes to paste
|
|
198
|
+
*/
|
|
199
|
+
const pasteAsLink = (view, nodes) => {
|
|
200
|
+
if (nodes.length === 0) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (isSingleLinkNode(nodes)) {
|
|
204
|
+
insertSingleLink(view, nodes[0]);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
insertNodeFragment(view, nodes);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
/**
|
|
211
|
+
* Checks if the nodes array contains just a single link node
|
|
212
|
+
*/
|
|
213
|
+
const isSingleLinkNode = (nodes) => {
|
|
214
|
+
if (nodes.length !== 1) {
|
|
128
215
|
return false;
|
|
129
216
|
}
|
|
130
|
-
const
|
|
131
|
-
//
|
|
132
|
-
if (
|
|
133
|
-
|
|
134
|
-
return true;
|
|
217
|
+
const node = nodes[0];
|
|
218
|
+
// Must be text with non-empty content
|
|
219
|
+
if (!node.isText || !node.text || node.text.trim() === '') {
|
|
220
|
+
return false;
|
|
135
221
|
}
|
|
136
|
-
|
|
222
|
+
// Must have a link mark (even if there are other marks, we just care about link presence)
|
|
223
|
+
return !!node.marks.find((mark) => mark.type.name === 'link');
|
|
137
224
|
};
|
|
138
|
-
|
|
225
|
+
/**
|
|
226
|
+
* Inserts a single link node, applying it to selected text if present
|
|
227
|
+
*/
|
|
228
|
+
const insertSingleLink = (view, linkNode) => {
|
|
139
229
|
const { state, dispatch } = view;
|
|
140
230
|
const { from, to } = state.selection;
|
|
141
|
-
const linkMark =
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const selectedText = state.doc.textBetween(from, to, ' ') || href;
|
|
147
|
-
const transaction = state.tr
|
|
231
|
+
const linkMark = linkNode.marks.find((mark) => mark.type.name === 'link');
|
|
232
|
+
// Use selected text if there's a selection, otherwise use the URL
|
|
233
|
+
const selectedText = state.doc.textBetween(from, to, ' ') || linkMark.attrs.href;
|
|
234
|
+
// Insert the text and add the link mark
|
|
235
|
+
dispatch(state.tr
|
|
148
236
|
.insertText(selectedText, from, to)
|
|
149
|
-
.addMark(from, from + selectedText.length, linkMark);
|
|
150
|
-
|
|
237
|
+
.addMark(from, from + selectedText.length, linkMark));
|
|
238
|
+
};
|
|
239
|
+
/**
|
|
240
|
+
* Inserts multiple nodes as a fragment at the current selection
|
|
241
|
+
* @param view - The editor view
|
|
242
|
+
* @param nodes - Array of nodes to insert
|
|
243
|
+
*/
|
|
244
|
+
const insertNodeFragment = (view, nodes) => {
|
|
245
|
+
const { state, dispatch } = view;
|
|
246
|
+
const { from, to } = state.selection;
|
|
247
|
+
// Create a fragment from the array of nodes
|
|
248
|
+
const fragment = Fragment.fromArray(nodes);
|
|
249
|
+
// Replace the current selection with the fragment
|
|
250
|
+
dispatch(state.tr.replaceWith(from, to, fragment));
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* Handles pasted content, converting URLs to links
|
|
254
|
+
*/
|
|
255
|
+
const processPasteEvent = (view, event) => {
|
|
256
|
+
var _a;
|
|
257
|
+
const text = (_a = event.clipboardData) === null || _a === void 0 ? void 0 : _a.getData('text/plain');
|
|
258
|
+
if (!text || !hasUrls(text)) {
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
const nodes = createNodesWithLinks(text, view.state.schema);
|
|
262
|
+
pasteAsLink(view, nodes);
|
|
263
|
+
return true;
|
|
151
264
|
};
|
|
152
265
|
export const createLinkPlugin = (updateLinkCallback) => {
|
|
153
266
|
return new Plugin({
|