prosemirror-link-plugin 1.1.5 → 1.3.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/README.md +161 -47
- package/dist/decorationsUpdateInRange.d.ts +5 -17
- package/dist/defaultAliasDecoration.d.ts +1 -6
- package/dist/index.es.js +1 -15
- package/dist/index.js +1 -1
- package/dist/plugin.d.ts +2 -8
- package/dist/prosemirror-utils.d.ts +8 -0
- package/dist/types.d.ts +3 -5
- package/package.json +82 -88
package/README.md
CHANGED
|
@@ -1,47 +1,161 @@
|
|
|
1
|
-
# prosemirror-link-plugin
|
|
2
|
-
|
|
3
|
-
 at [Emergence Engineering](https://emergence-engineering.com/)
|
|
6
|
-
|
|
7
|
-
Try it out at <https://emergence-engineering.com/blog/prosemirror-link-plugin>
|
|
8
|
-
|
|
9
|
-
# Features
|
|
10
|
-
|
|
11
|
-
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
1
|
+
# prosemirror-link-plugin
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
By [Viktor Váczi](https://emergence-engineering.com/cv/viktor) at [Emergence Engineering](https://emergence-engineering.com/)
|
|
6
|
+
|
|
7
|
+
Try it out at <https://emergence-engineering.com/blog/prosemirror-link-plugin>
|
|
8
|
+
|
|
9
|
+
# Features
|
|
10
|
+
|
|
11
|
+
- Add decoration around words in a given list of aliases
|
|
12
|
+
- Update the aliases on the fly and see your decorations update
|
|
13
|
+
- Get notified when the user types or removes an alias from the document
|
|
14
|
+
- Do something with the decorations ( for example links that lead to a page about the alias )
|
|
15
|
+
|
|
16
|
+
# How to use
|
|
17
|
+
|
|
18
|
+
Install with `npm install --save prosemirror-link-plugin`, then:
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { schema } from "prosemirror-schema-basic";
|
|
22
|
+
import { exampleSetup } from "prosemirror-example-setup";
|
|
23
|
+
import { Decoration } from "prosemirror-view";
|
|
24
|
+
import { EditorState } from "prosemirror-state";
|
|
25
|
+
import { EditorView } from "prosemirror-view";
|
|
26
|
+
import {
|
|
27
|
+
autoLinkingPlugin,
|
|
28
|
+
LinksKeyState,
|
|
29
|
+
} from "prosemirror-link-plugin";
|
|
30
|
+
|
|
31
|
+
export interface LinkSpec {
|
|
32
|
+
id: number;
|
|
33
|
+
alias: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const aliasDecoration = (
|
|
37
|
+
start: number,
|
|
38
|
+
end: number,
|
|
39
|
+
alias: string,
|
|
40
|
+
matchPos: number,
|
|
41
|
+
pluginState: LinksKeyState<LinkSpec>,
|
|
42
|
+
) => {
|
|
43
|
+
const spec = pluginState.aliasToSpec[alias];
|
|
44
|
+
return Decoration.inline(
|
|
45
|
+
start,
|
|
46
|
+
end,
|
|
47
|
+
|
|
48
|
+
{
|
|
49
|
+
class: "autoLink",
|
|
50
|
+
onclick: `alert('You clicked on "${alias}"')`,
|
|
51
|
+
},
|
|
52
|
+
{ id: spec?.id, alias },
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
let aliases = { alias: "typing", id: 1 };
|
|
57
|
+
|
|
58
|
+
const initialDoc = {
|
|
59
|
+
content: [
|
|
60
|
+
{
|
|
61
|
+
content: [
|
|
62
|
+
{
|
|
63
|
+
text: "Start typing!",
|
|
64
|
+
type: "text",
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
type: "paragraph",
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
type: "doc",
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const state = EditorState.create<typeof schema>({
|
|
74
|
+
doc: schema.nodeFromJSON(initialDoc),
|
|
75
|
+
plugins: [
|
|
76
|
+
...exampleSetup({
|
|
77
|
+
schema,
|
|
78
|
+
}),
|
|
79
|
+
autoLinkingPlugin(
|
|
80
|
+
aliases,
|
|
81
|
+
aliasDecoration,
|
|
82
|
+
),
|
|
83
|
+
],
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const view: EditorView = new EditorView(document.getElementById("editor"), {
|
|
87
|
+
state,
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## How to update the alias list
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
const meta: LinksMeta<LinkSpec> = {
|
|
95
|
+
type: LinksMetaType.linkUpdate,
|
|
96
|
+
// aliases is the new list of alises
|
|
97
|
+
specs: aliases,
|
|
98
|
+
};
|
|
99
|
+
view.dispatch(pmView.state.tr.setMeta(linksKey, meta));
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
# Configuration
|
|
103
|
+
|
|
104
|
+
### AutoLinkingPlugin
|
|
105
|
+
| name | type | description |
|
|
106
|
+
| --------------------- | ---------------------------------------------------- | -------------------------------------------------------------- |
|
|
107
|
+
| aliasesWithSpec | Array<SpecWithAlias<T>> | Initial list of aliases ( with additional properties ) |
|
|
108
|
+
| createAliasDecoration | CreateAliasDecoration ( explained below ) | Used to create ProseMirror `Decoration` when an alias is found |
|
|
109
|
+
| onLinkAdd | ( addedLinks: Array<SpecWithAlias<T>> ) => void | Called when links are added |
|
|
110
|
+
| onLinkRemove | ( removedLinks: Array<SpecWithAlias<T>> ) => void | Called when links are removed |
|
|
111
|
+
| regexGenerator | regexGenerator: ( aliases: string[] ) => RegExp | Creates the regex used internally to find matches |
|
|
112
|
+
|
|
113
|
+
### createAliasDecoration
|
|
114
|
+
|
|
115
|
+
| name | type | description |
|
|
116
|
+
| --------------------- | ---------------- | --------------------------------- |
|
|
117
|
+
| start | number | Start position of the alias |
|
|
118
|
+
| end | number | End position of the alias |
|
|
119
|
+
| alias | string | Matched alias |
|
|
120
|
+
| matchPos | number | Distance from the non-text parent |
|
|
121
|
+
| pluginState | LinksKeyState<T> | Current state of the plugin |
|
|
122
|
+
| doc | Node | ProseMirror doc Node |
|
|
123
|
+
|
|
124
|
+
### Example CSS
|
|
125
|
+
|
|
126
|
+
```css
|
|
127
|
+
.autoLink {
|
|
128
|
+
display: inline-flex;
|
|
129
|
+
justify-content: center;
|
|
130
|
+
align-items: center;
|
|
131
|
+
background-color: lightblue;
|
|
132
|
+
color: black;
|
|
133
|
+
border: 1px solid blueviolet;
|
|
134
|
+
width: fit-content;
|
|
135
|
+
height: 1.4rem;
|
|
136
|
+
padding: 3px 5px 3px 5px;
|
|
137
|
+
border-radius: 5px;
|
|
138
|
+
margin-left: 3px;
|
|
139
|
+
margin-right: 3px;
|
|
140
|
+
cursor: pointer;
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Development
|
|
145
|
+
|
|
146
|
+
### Running & linking locally
|
|
147
|
+
1. install plugin dependencies: `npm install`
|
|
148
|
+
2. install peer dependencies: `npm run install-peers`
|
|
149
|
+
3. link local lib: `npm run link`
|
|
150
|
+
4. link the package from the project you want to use it: `npm run link prosemirror-link-plugin`
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
### About us
|
|
154
|
+
|
|
155
|
+
Emergence Engineering is dev shop from the EU:
|
|
156
|
+
<https://emergence-engineering.com/>
|
|
157
|
+
|
|
158
|
+
We're looking for work, especially with ProseMirror ;)
|
|
159
|
+
|
|
160
|
+
Feel free to contact me at
|
|
161
|
+
<viktor.vaczi@emergence-engineering.com>
|
|
@@ -1,20 +1,8 @@
|
|
|
1
1
|
import { Decoration } from "prosemirror-view";
|
|
2
|
-
import { Node
|
|
3
|
-
import { LinksKeyState, SpecObject
|
|
4
|
-
declare const decorationsUpdateInRange: <T extends SpecObject
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
[key: string]: any;
|
|
8
|
-
}>) => {
|
|
9
|
-
decorationsToAdd: Decoration<T & {
|
|
10
|
-
alias: string;
|
|
11
|
-
} & {
|
|
12
|
-
[key: string]: any;
|
|
13
|
-
}>[];
|
|
14
|
-
decorationsToRemove: Decoration<T & {
|
|
15
|
-
alias: string;
|
|
16
|
-
} & {
|
|
17
|
-
[key: string]: any;
|
|
18
|
-
}>[];
|
|
2
|
+
import { Node } from "prosemirror-model";
|
|
3
|
+
import { LinksKeyState, SpecObject } from "./types";
|
|
4
|
+
declare const decorationsUpdateInRange: <T extends SpecObject>(from: number, to: number, doc: Node, pluginState: LinksKeyState<T>, createAliasDecoration: (decorationStart: number, decorationEnd: number, alias: string, matchPos: number, linkPluginState: LinksKeyState<T>, docNode: Node) => Decoration) => {
|
|
5
|
+
decorationsToAdd: Decoration[];
|
|
6
|
+
decorationsToRemove: Decoration[];
|
|
19
7
|
};
|
|
20
8
|
export default decorationsUpdateInRange;
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
import { Decoration } from "prosemirror-view";
|
|
2
|
-
import { Node } from "prosemirror-model";
|
|
3
2
|
import { LinksKeyState } from "./types";
|
|
4
|
-
declare const defaultAliasDecoration: (start: number, end: number, alias: string, matchPos: number, pluginState: LinksKeyState
|
|
5
|
-
alias: string;
|
|
6
|
-
} & {
|
|
7
|
-
alias: string;
|
|
8
|
-
} & import("prosemirror-view").InlineDecorationSpec>;
|
|
3
|
+
declare const defaultAliasDecoration: <Spec extends Record<string, any>>(start: number, end: number, alias: string, matchPos: number, pluginState: LinksKeyState<Spec>) => Decoration;
|
|
9
4
|
export default defaultAliasDecoration;
|
package/dist/index.es.js
CHANGED
|
@@ -1,15 +1 @@
|
|
|
1
|
-
import{PluginKey as n,Plugin as e}from"prosemirror-state";import{DecorationSet as r,Decoration as o}from"prosemirror-view";
|
|
2
|
-
/*! *****************************************************************************
|
|
3
|
-
Copyright (c) Microsoft Corporation.
|
|
4
|
-
|
|
5
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
-
purpose with or without fee is hereby granted.
|
|
7
|
-
|
|
8
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
9
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
10
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
11
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
12
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
13
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
14
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
15
|
-
***************************************************************************** */var a,i=function(){return(i=Object.assign||function(n){for(var e,r=1,o=arguments.length;r<o;r++)for(var t in e=arguments[r])Object.prototype.hasOwnProperty.call(e,t)&&(n[t]=e[t]);return n}).apply(this,arguments)};function c(){for(var n=0,e=0,r=arguments.length;e<r;e++)n+=arguments[e].length;var o=Array(n),t=0;for(e=0;e<r;e++)for(var a=arguments[e],i=0,c=a.length;i<c;i++,t++)o[t]=a[i];return o}!function(n){n.linkUpdate="linkUpdate"}(a||(a={}));var d=function(n){var e=n.map((function(n){return"\\b"+n.trim()+"\\b"})).sort((function(n,e){return e.length-n.length})).join("|");return new RegExp(e,"g")},s=function(n,e,r,o,a){var i=n,d=[],s=[],u=[],p=[],l=function(){var n=r.resolve(i),e=n.parent,d=i-n.parentOffset,u=d+e.nodeSize,l=o.decorations.find(d,u);p=c(p,l),t(e).map((function(n){var e,t;return Array.from((null===(t=null===(e=n.node)||void 0===e?void 0:e.text)||void 0===t?void 0:t.matchAll(o.regex))||[]).map((function(e){if(!e)throw new Error("wrong match");var t=e.index||0,i=e[0],u=n.pos+t+d,p=n.pos+t+i.length+d,l=n.pos+t,f=a(u,p,i,l,o,r);s=c(s,[f])}))})),i+=e.nodeSize};do{l()}while(i<e);return p.map((function(n){var e=s.find((function(e){return e.from===n.from&&e.to===n.to&&(r=e.spec,o=n.spec,t=Object.keys(r),a=Object.keys(o),t.length===a.length&&t.reduce((function(n,e){return n&&o[e]===r[e]}),!0));var r,o,t,a}));return e?d.push(e):u.push(n),null})),{decorationsToAdd:s=s.filter((function(n){return!d.includes(n)})),decorationsToRemove:u}},u=new n("links"),p=function(n,o,t,p,l){void 0===l&&(l=d);var f=function(n){return n.map((function(n){return n.spec}))};return new e({key:u,state:{init:function(e,t){var a=l(n.map((function(n){return n.alias}))),c={decorations:new r,regex:a,aliasToSpec:n.reduce((function(n,e){var r;return i(i({},n),((r={})[e.alias]=e,r))}),{})},d=s(0,t.doc.nodeSize,t.doc,c,o),u=c.decorations.add(t.doc,d.decorationsToAdd);return i(i({},c),{decorations:u})},apply:function(n,e){var r=n.getMeta(u),d=e.regex,v=e.aliasToSpec;if((null==r?void 0:r.type)===a.linkUpdate){v=r.specs.reduce((function(n,e){var r;return i(i({},n),((r={})[e.alias]=e,r))}),{}),d=l(r.specs.map((function(n){return n.alias})));var m=s(0,n.doc.nodeSize,n.doc,i(i({},e),{regex:d,aliasToSpec:v}),o),g=m.decorationsToAdd,h=m.decorationsToRemove;return h.length&&p&&p(f(h)),g.length&&t&&t(f(g)),{decorations:e.decorations.remove(h).add(n.doc,g),regex:d,aliasToSpec:v}}if(!n.mapping.maps.length)return e;var T=[],S=e.decorations.map(n.mapping,n.doc,{onRemove:function(n){T.push(n)}}),x=function(n){for(var e=n.doc.nodeSize,r=0,o=0;o<n.mapping.maps.length;o+=1){var t=n.mapping.maps[o].ranges[0],a=t+n.mapping.maps[o].ranges[2];e=Math.min(e,t,a),r=Math.max(r,t,a)}return{start:e,end:r}}(n),w=x.start,y=x.end,k=s(w,y,n.doc,i(i({},e),{decorations:S}),o),A=k.decorationsToRemove,b=k.decorationsToAdd,O=c(T,f(A));return O.length&&p&&p(O),b.length&&t&&t(f(b)),{decorations:S.remove(A).add(n.doc,b),regex:d,aliasToSpec:v}}},props:{decorations:function(n){var e;return r.create(n.doc,(null===(e=u.getState(n))||void 0===e?void 0:e.decorations.find())||[])}}})},l=function(n,e,r,t,a,i){var c=a.aliasToSpec[r];return o.inline(n,e,{class:"autoLink"},c)};export{a as LinksMetaType,p as autoLinkingPlugin,l as defaultAliasDecoration,u as linksKey};
|
|
1
|
+
import{PluginKey as n,Plugin as e}from"prosemirror-state";import{DecorationSet as r,Decoration as o}from"prosemirror-view";var t,a=function(){return a=Object.assign||function(n){for(var e,r=1,o=arguments.length;r<o;r++)for(var t in e=arguments[r])Object.prototype.hasOwnProperty.call(e,t)&&(n[t]=e[t]);return n},a.apply(this,arguments)};function i(n,e,r){if(r||2===arguments.length)for(var o,t=0,a=e.length;t<a;t++)!o&&t in e||(o||(o=Array.prototype.slice.call(e,0,t)),o[t]=e[t]);return n.concat(o||Array.prototype.slice.call(e))}!function(n){n.linkUpdate="linkUpdate"}(t||(t={}));var c=function(n){var e=n.length?n.map((function(n){return"\\b".concat(n.trim(),"\\b")})).sort((function(n,e){return e.length-n.length})).join("|"):"a^";return new RegExp(e,"g")},d=function(n,e,r){if(!n)throw new Error('Invalid "node" parameter');if(!e)throw new Error('Invalid "predicate" parameter');return function(n,e){if(void 0===e&&(e=!0),!n)throw new Error('Invalid "node" parameter');var r=[];return n.descendants((function(n,o){if(r.push({node:n,pos:o}),!e)return!1})),r}(n,r).filter((function(n){return e(n.node)}))},u=function(n,e,r,o,t){var a=n,c=[],u=[],s=[],p=[],l=function(){var n,e=r.resolve(a),c=e.parent,s=a-e.parentOffset,l=s+c.nodeSize,f=o.decorations.find(s,l).filter((function(n){return n.from>=s&&n.to<=l}));p=i(i([],p,!0),f,!0),(void 0===n&&(n=!0),d(c,(function(n){return n.isText}),n)).map((function(n){var e,a;return Array.from((null===(a=null===(e=n.node)||void 0===e?void 0:e.text)||void 0===a?void 0:a.matchAll(o.regex))||[]).map((function(e){if(!e)throw new Error("wrong match");var a=e.index||0,c=e[0],d=n.pos+a+s,p=n.pos+a+c.length+s,l=n.pos+a,f=t(d,p,c,l,o,r);u=i(i([],u,!0),[f],!1)}))})),a+=c.nodeSize};do{l()}while(a<e);return p.map((function(n){var e=u.find((function(e){return e.from===n.from&&e.to===n.to&&(r=e.spec,o=n.spec,t=Object.keys(r),a=Object.keys(o),t.length===a.length&&t.reduce((function(n,e){return n&&o[e]===r[e]}),!0));var r,o,t,a}));return e?c.push(e):s.push(n),null})),{decorationsToAdd:u=u.filter((function(n){return!c.includes(n)})),decorationsToRemove:s}},s=new n("links"),p=function(n,o,d,p,l){void 0===l&&(l=c);var f=function(n){return n.map((function(n){return n.spec}))};return new e({key:s,state:{init:function(e,t){var i=l(n.map((function(n){return n.alias}))),c={decorations:r.empty,regex:i,aliasToSpec:n.reduce((function(n,e){var r;return a(a({},n),((r={})[e.alias]=e,r))}),{})},d=u(0,t.doc.nodeSize,t.doc,c,o),s=c.decorations.add(t.doc,d.decorationsToAdd);return a(a({},c),{decorations:s})},apply:function(n,e){var r=n.getMeta(s),c=e.regex,v=e.aliasToSpec;if((null==r?void 0:r.type)===t.linkUpdate){v=r.specs.reduce((function(n,e){var r;return a(a({},n),((r={})[e.alias]=e,r))}),{}),c=l(r.specs.map((function(n){return n.alias})));var m=u(0,n.doc.nodeSize,n.doc,a(a({},e),{regex:c,aliasToSpec:v}),o),g=m.decorationsToAdd,h=m.decorationsToRemove;return h.length&&p&&p(f(h)),g.length&&d&&d(f(g)),{decorations:e.decorations.remove(h).add(n.doc,g),regex:c,aliasToSpec:v}}if(!n.mapping.maps.length)return e;var w=[],y=e.decorations.map(n.mapping,n.doc,{onRemove:function(n){w.push(n)}}),T=function(n){for(var e=n.doc.nodeSize,r=0,o=0;o<n.mapping.maps.length;o+=1){var t=n.mapping.maps[o].ranges[0],a=t+n.mapping.maps[o].ranges[2];e=Math.min(e,t,a),r=Math.max(r,t,a)}return{start:e,end:r}}(n),x=T.start,S=T.end,k=u(x,S,n.doc,a(a({},e),{decorations:y}),o),A=k.decorationsToRemove,b=k.decorationsToAdd,O=i(i([],w,!0),f(A),!0);return O.length&&p&&p(O),b.length&&d&&d(f(b)),{decorations:y.remove(A).add(n.doc,b),regex:c,aliasToSpec:v}}},props:{decorations:function(n){var e;return r.create(n.doc,(null===(e=s.getState(n))||void 0===e?void 0:e.decorations.find())||[])}}})},l=function(n,e,r,t,a){var i=a.aliasToSpec[r];return o.inline(n,e,{class:"autoLink"},i)};export{t as LinksMetaType,p as autoLinkingPlugin,l as defaultAliasDecoration,s as linksKey};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";var e=require("prosemirror-state"),n=require("prosemirror-view"),r=function(){return r=Object.assign||function(e){for(var n,r=1,t=arguments.length;r<t;r++)for(var o in n=arguments[r])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e},r.apply(this,arguments)};function t(e,n,r){if(r||2===arguments.length)for(var t,o=0,i=n.length;o<i;o++)!t&&o in n||(t||(t=Array.prototype.slice.call(n,0,o)),t[o]=n[o]);return e.concat(t||Array.prototype.slice.call(n))}exports.LinksMetaType=void 0,(exports.LinksMetaType||(exports.LinksMetaType={})).linkUpdate="linkUpdate";var o=function(e){var n=e.length?e.map((function(e){return"\\b".concat(e.trim(),"\\b")})).sort((function(e,n){return n.length-e.length})).join("|"):"a^";return new RegExp(n,"g")},i=function(e,n,r){if(!e)throw new Error('Invalid "node" parameter');if(!n)throw new Error('Invalid "predicate" parameter');return function(e,n){if(void 0===n&&(n=!0),!e)throw new Error('Invalid "node" parameter');var r=[];return e.descendants((function(e,t){if(r.push({node:e,pos:t}),!n)return!1})),r}(e,r).filter((function(e){return n(e.node)}))},a=function(e,n,r,o,a){var c=e,s=[],u=[],d=[],p=[],l=function(){var e,n=r.resolve(c),s=n.parent,d=c-n.parentOffset,l=d+s.nodeSize,f=o.decorations.find(d,l).filter((function(e){return e.from>=d&&e.to<=l}));p=t(t([],p,!0),f,!0),(void 0===e&&(e=!0),i(s,(function(e){return e.isText}),e)).map((function(e){var n,i;return Array.from((null===(i=null===(n=e.node)||void 0===n?void 0:n.text)||void 0===i?void 0:i.matchAll(o.regex))||[]).map((function(n){if(!n)throw new Error("wrong match");var i=n.index||0,c=n[0],s=e.pos+i+d,p=e.pos+i+c.length+d,l=e.pos+i,f=a(s,p,c,l,o,r);u=t(t([],u,!0),[f],!1)}))})),c+=s.nodeSize};do{l()}while(c<n);return p.map((function(e){var n=u.find((function(n){return n.from===e.from&&n.to===e.to&&(r=n.spec,t=e.spec,o=Object.keys(r),i=Object.keys(t),o.length===i.length&&o.reduce((function(e,n){return e&&t[n]===r[n]}),!0));var r,t,o,i}));return n?s.push(n):d.push(e),null})),{decorationsToAdd:u=u.filter((function(e){return!s.includes(e)})),decorationsToRemove:d}},c=new e.PluginKey("links");exports.autoLinkingPlugin=function(i,s,u,d,p){void 0===p&&(p=o);var l=function(e){return e.map((function(e){return e.spec}))};return new e.Plugin({key:c,state:{init:function(e,t){var o=p(i.map((function(e){return e.alias}))),c={decorations:n.DecorationSet.empty,regex:o,aliasToSpec:i.reduce((function(e,n){var t;return r(r({},e),((t={})[n.alias]=n,t))}),{})},u=a(0,t.doc.nodeSize,t.doc,c,s),d=c.decorations.add(t.doc,u.decorationsToAdd);return r(r({},c),{decorations:d})},apply:function(e,n){var o=e.getMeta(c),i=n.regex,f=n.aliasToSpec;if((null==o?void 0:o.type)===exports.LinksMetaType.linkUpdate){f=o.specs.reduce((function(e,n){var t;return r(r({},e),((t={})[n.alias]=n,t))}),{}),i=p(o.specs.map((function(e){return e.alias})));var v=a(0,e.doc.nodeSize,e.doc,r(r({},n),{regex:i,aliasToSpec:f}),s),g=v.decorationsToAdd,m=v.decorationsToRemove;return m.length&&d&&d(l(m)),g.length&&u&&u(l(g)),{decorations:n.decorations.remove(m).add(e.doc,g),regex:i,aliasToSpec:f}}if(!e.mapping.maps.length)return n;var h=[],y=n.decorations.map(e.mapping,e.doc,{onRemove:function(e){h.push(e)}}),x=function(e){for(var n=e.doc.nodeSize,r=0,t=0;t<e.mapping.maps.length;t+=1){var o=e.mapping.maps[t].ranges[0],i=o+e.mapping.maps[t].ranges[2];n=Math.min(n,o,i),r=Math.max(r,o,i)}return{start:n,end:r}}(e),T=x.start,w=x.end,k=a(T,w,e.doc,r(r({},n),{decorations:y}),s),S=k.decorationsToRemove,A=k.decorationsToAdd,M=t(t([],h,!0),l(S),!0);return M.length&&d&&d(M),A.length&&u&&u(l(A)),{decorations:y.remove(S).add(e.doc,A),regex:i,aliasToSpec:f}}},props:{decorations:function(e){var r;return n.DecorationSet.create(e.doc,(null===(r=c.getState(e))||void 0===r?void 0:r.decorations.find())||[])}}})},exports.defaultAliasDecoration=function(e,r,t,o,i){var a=i.aliasToSpec[t];return n.Decoration.inline(e,r,{class:"autoLink"},a)},exports.linksKey=c;
|
package/dist/plugin.d.ts
CHANGED
|
@@ -2,11 +2,5 @@ import { Plugin, PluginKey } from "prosemirror-state";
|
|
|
2
2
|
import { Decoration } from "prosemirror-view";
|
|
3
3
|
import { Node } from "prosemirror-model";
|
|
4
4
|
import { LinksKeyState, SpecObject, SpecWithAlias } from "./types";
|
|
5
|
-
export declare const linksKey: PluginKey<LinksKeyState<any
|
|
6
|
-
export declare const autoLinkingPlugin: <T extends SpecObject>(aliasesWithSpec: SpecWithAlias<T>[], createAliasDecoration: (start: number, end: number, alias: string, matchPos: number, pluginState: LinksKeyState<T>, doc: Node) => Decoration<T
|
|
7
|
-
alias: string;
|
|
8
|
-
} & SpecObject>, onLinkAdd?: ((addedLinks: (T & {
|
|
9
|
-
alias: string;
|
|
10
|
-
})[]) => void) | undefined, onLinkRemove?: ((removedLinks: (T & {
|
|
11
|
-
alias: string;
|
|
12
|
-
})[]) => void) | undefined, regexGenerator?: (aliases: string[]) => RegExp) => Plugin<LinksKeyState<T>, any>;
|
|
5
|
+
export declare const linksKey: PluginKey<LinksKeyState<any>>;
|
|
6
|
+
export declare const autoLinkingPlugin: <T extends SpecObject>(aliasesWithSpec: SpecWithAlias<T>[], createAliasDecoration: (start: number, end: number, alias: string, matchPos: number, pluginState: LinksKeyState<T>, doc: Node) => Decoration, onLinkAdd?: ((addedLinks: SpecWithAlias<T>[]) => void) | undefined, onLinkRemove?: ((removedLinks: SpecWithAlias<T>[]) => void) | undefined, regexGenerator?: (aliases: string[]) => RegExp) => Plugin<LinksKeyState<T>>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Node } from "prosemirror-model";
|
|
2
|
+
export interface NodeWithPos {
|
|
3
|
+
node: Node;
|
|
4
|
+
pos: number;
|
|
5
|
+
}
|
|
6
|
+
export declare const flatten: (node: Node, descend?: boolean) => NodeWithPos[];
|
|
7
|
+
export declare const findChildren: (node: Node, predicate: (child: Node) => boolean, descend: boolean) => NodeWithPos[];
|
|
8
|
+
export declare const findTextNodes: (node: Node, descend?: boolean) => NodeWithPos[];
|
package/dist/types.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { DecorationSet } from "prosemirror-view";
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
export declare type SpecWithAlias<Spec extends SpecObject> = Spec & {
|
|
2
|
+
export type SpecObject = Record<string, any>;
|
|
3
|
+
export type SpecWithAlias<Spec extends SpecObject> = Spec & {
|
|
6
4
|
alias: string;
|
|
7
5
|
};
|
|
8
6
|
export interface LinksKeyState<Spec extends SpecObject = {
|
|
@@ -19,4 +17,4 @@ export interface LinksUpdateMeta<Spec extends SpecObject> {
|
|
|
19
17
|
type: LinksMetaType.linkUpdate;
|
|
20
18
|
specs: Array<SpecWithAlias<Spec>>;
|
|
21
19
|
}
|
|
22
|
-
export
|
|
20
|
+
export type LinksMeta<Spec extends SpecObject> = LinksUpdateMeta<Spec>;
|
package/package.json
CHANGED
|
@@ -1,88 +1,82 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "prosemirror-link-plugin",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "ProseMirror plugin for automatic links",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"module": "dist/index.es.js",
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
8
|
-
"scripts": {
|
|
9
|
-
"prebuild": "rimraf dist",
|
|
10
|
-
"build": "rollup -c",
|
|
11
|
-
"dev": "rollup -c -w",
|
|
12
|
-
"format": "eslint src --ext .ts --fix",
|
|
13
|
-
"prepublishOnly": "npm run build && npm test && npm run lint",
|
|
14
|
-
"version": "npm run format && git add -A src",
|
|
15
|
-
"postversion": "git push && git push --tags",
|
|
16
|
-
"lint": "tsc --project tsconfig.json --noEmit && eslint src --ext .ts",
|
|
17
|
-
"test": "jest",
|
|
18
|
-
"upgrade-interactive": "npm-check --update",
|
|
19
|
-
"install-peers": "npm-install-peers",
|
|
20
|
-
"publish:np": "np"
|
|
21
|
-
},
|
|
22
|
-
"repository": {
|
|
23
|
-
"type": "git",
|
|
24
|
-
"url": "git+https://gitlab.com/emergence-engineering/prosemirror-link-plugin.git"
|
|
25
|
-
},
|
|
26
|
-
"files": [
|
|
27
|
-
"dist/**/*"
|
|
28
|
-
],
|
|
29
|
-
"author": "Emergence Engineering",
|
|
30
|
-
"keywords": [
|
|
31
|
-
"ProseMirror",
|
|
32
|
-
"link",
|
|
33
|
-
"automatic"
|
|
34
|
-
],
|
|
35
|
-
"license": "ISC",
|
|
36
|
-
"bugs": {
|
|
37
|
-
"url": "https://gitlab.com/emergence-engineering/prosemirror-link-plugin/issues"
|
|
38
|
-
},
|
|
39
|
-
"homepage": "https://gitlab.com/emergence-engineering/prosemirror-link-plugin#readme",
|
|
40
|
-
"peerDependencies": {
|
|
41
|
-
"prosemirror-commands": "^1.1.4",
|
|
42
|
-
"prosemirror-model": "^1.11.0",
|
|
43
|
-
"prosemirror-state": "^1.3.3",
|
|
44
|
-
"prosemirror-view": "^1.15.5"
|
|
45
|
-
},
|
|
46
|
-
"devDependencies": {
|
|
47
|
-
"@types/jest": "^
|
|
48
|
-
"@
|
|
49
|
-
"@
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"eslint": "^7.
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
"
|
|
79
|
-
"
|
|
80
|
-
"prosemirror-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
"prosemirror-tables": "^1.1.1",
|
|
84
|
-
"prosemirror-transform": "^1.3.2",
|
|
85
|
-
"prosemirror-utils": "^0.9.6",
|
|
86
|
-
"prosemirror-view": "^1.15.5"
|
|
87
|
-
}
|
|
88
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "prosemirror-link-plugin",
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "ProseMirror plugin for automatic links",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.es.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"prebuild": "rimraf dist",
|
|
10
|
+
"build": "rollup -c --bundleConfigAsCjs",
|
|
11
|
+
"dev": "rollup -c -w --bundleConfigAsCjs",
|
|
12
|
+
"format": "eslint src --ext .ts --fix",
|
|
13
|
+
"prepublishOnly": "npm run build && npm test && npm run lint",
|
|
14
|
+
"version": "npm run format && git add -A src",
|
|
15
|
+
"postversion": "git push && git push --tags",
|
|
16
|
+
"lint": "tsc --project tsconfig.json --noEmit && eslint src --ext .ts",
|
|
17
|
+
"test": "jest",
|
|
18
|
+
"upgrade-interactive": "npm-check --update",
|
|
19
|
+
"install-peers": "npm-install-peers",
|
|
20
|
+
"publish:np": "np"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://gitlab.com/emergence-engineering/prosemirror-link-plugin.git"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist/**/*"
|
|
28
|
+
],
|
|
29
|
+
"author": "Emergence Engineering",
|
|
30
|
+
"keywords": [
|
|
31
|
+
"ProseMirror",
|
|
32
|
+
"link",
|
|
33
|
+
"automatic"
|
|
34
|
+
],
|
|
35
|
+
"license": "ISC",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://gitlab.com/emergence-engineering/prosemirror-link-plugin/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://gitlab.com/emergence-engineering/prosemirror-link-plugin#readme",
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"prosemirror-commands": "^1.1.4",
|
|
42
|
+
"prosemirror-model": "^1.11.0",
|
|
43
|
+
"prosemirror-state": "^1.3.3",
|
|
44
|
+
"prosemirror-view": "^1.15.5"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/jest": "^29.2.4",
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "^5.46.1",
|
|
49
|
+
"@typescript-eslint/parser": "^5.46.1",
|
|
50
|
+
"eslint": "^8.29.0",
|
|
51
|
+
"eslint-config-airbnb": "^19.0.4",
|
|
52
|
+
"eslint-config-prettier": "^8.5.0",
|
|
53
|
+
"eslint-plugin-import": "^2.26.0",
|
|
54
|
+
"eslint-plugin-jest": "^27.1.6",
|
|
55
|
+
"eslint-plugin-jsx-a11y": "^6.6.1",
|
|
56
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
57
|
+
"eslint-plugin-react": "^7.31.11",
|
|
58
|
+
"jest": "^29.3.1",
|
|
59
|
+
"np": "^7.6.2",
|
|
60
|
+
"npm-check": "^6.0.1",
|
|
61
|
+
"prettier": "^2.8.1",
|
|
62
|
+
"prosemirror-schema-basic": "^1.2.0",
|
|
63
|
+
"prosemirror-schema-list": "^1.2.2",
|
|
64
|
+
"prosemirror-test-builder": "^1.1.0",
|
|
65
|
+
"rollup": "^3.7.4",
|
|
66
|
+
"rollup-plugin-minification": "^0.2.0",
|
|
67
|
+
"rollup-plugin-typescript2": "^0.34.1",
|
|
68
|
+
"ts-jest": "^29.0.3",
|
|
69
|
+
"tslib": "^2.4.1",
|
|
70
|
+
"typescript": "^4.9.4"
|
|
71
|
+
},
|
|
72
|
+
"dependencies": {
|
|
73
|
+
"npm-install-peers": "^1.2.2",
|
|
74
|
+
"orderedmap": "^2.1.0",
|
|
75
|
+
"prosemirror-commands": "^1.1.4",
|
|
76
|
+
"prosemirror-model": "^1.11.0",
|
|
77
|
+
"prosemirror-state": "^1.3.3",
|
|
78
|
+
"prosemirror-tables": "^1.3.0",
|
|
79
|
+
"prosemirror-transform": "^1.7.0",
|
|
80
|
+
"prosemirror-view": "^1.15.5"
|
|
81
|
+
}
|
|
82
|
+
}
|