@editorjs-mentions/plugin 0.1.4 → 0.1.5

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 CHANGED
@@ -1,20 +1,8 @@
1
1
  # @editorjs-mentions/plugin
2
2
 
3
- [![Last Commit](https://img.shields.io/github/last-commit/pmalirz/editorjs-mentions?style=flat-square)](https://github.com/pmalirz/editorjs-mentions)
4
- [![npm latest](https://img.shields.io/npm/v/%40editorjs-mentions%2Fplugin/latest?style=flat-square)](https://www.npmjs.com/package/@editorjs-mentions/plugin)
5
- [![License](https://img.shields.io/github/license/pmalirz/editorjs-mentions?style=flat-square)](https://github.com/pmalirz/editorjs-mentions/blob/main/LICENSE)
6
- [![Open Issues](https://img.shields.io/github/issues/pmalirz/editorjs-mentions?style=flat-square)](https://github.com/pmalirz/editorjs-mentions/issues)
7
- [![Editor.js 2.x](https://img.shields.io/badge/Editor.js-2.x-0ea5e9?style=flat-square)](https://www.npmjs.com/package/@editorjs/editorjs)
3
+ Mentions autocomplete plugin for Editor.js.
8
4
 
9
- Mentions autocomplete plugin for Editor.js with:
10
-
11
- - configurable trigger symbols
12
- - pluggable provider API
13
- - structured mention serialization (`text + entities`) for stable backend IDs
14
- - mention tooltip with optional link
15
- - metadata-preserving copy/paste in Editor.js
16
-
17
- ![Mentions autocomplete example](docs/mentions-example-autocomplete.png)
5
+ ![Mentions usage demo](docs/mentions-usage-example.gif)
18
6
 
19
7
  ## Install
20
8
 
@@ -22,28 +10,33 @@ Mentions autocomplete plugin for Editor.js with:
22
10
  npm i @editorjs-mentions/plugin
23
11
  ```
24
12
 
13
+ Peer dependency:
14
+
15
+ ```bash
16
+ npm i @editorjs/editorjs
17
+ ```
18
+
25
19
  ## Quick Start
26
20
 
27
21
  ```ts
28
- import { EditorJSMentions, createRestMentionProvider } from "@editorjs-mentions/plugin";
22
+ import EditorJS from "@editorjs/editorjs";
23
+ import {
24
+ EditorJSMentions,
25
+ createRestMentionProvider
26
+ } from "@editorjs-mentions/plugin";
27
+
28
+ const editor = new EditorJS({ holder: "editor" });
29
+ await editor.isReady;
29
30
 
30
31
  const mentions = new EditorJSMentions({
31
32
  holder: "editor",
32
33
  triggerSymbols: ["@"],
33
- mentionRenderContext: { currentUserId: "u-1002" },
34
- renderMention: ({ item, defaultText, element, context }) => {
35
- const ctx = context as { currentUserId?: string } | undefined;
36
- const isCurrentUser = ctx?.currentUserId === item.id;
37
- element.textContent = defaultText;
38
- element.style.fontWeight = isCurrentUser ? "700" : "400";
39
- },
40
34
  provider: createRestMentionProvider({
41
35
  endpoint: "http://localhost:3001/api/mentions/users"
42
36
  })
43
37
  });
44
38
 
45
- mentions.setMentionRenderContext({ currentUserId: "u-1001" });
46
-
39
+ // later:
47
40
  // mentions.destroy();
48
41
  ```
49
42
 
@@ -57,11 +50,11 @@ mentions.setMentionRenderContext({ currentUserId: "u-1001" });
57
50
  - `debounceMs?: number` - defaults to `160`.
58
51
  - `className?: string` - custom dropdown class.
59
52
  - `onSelect?: (item) => void`.
60
- - `renderItem?: (item) => string` - custom item renderer.
61
- - `renderMention?: (args) => void` - customize rendered mention anchor (style/content/classes).
62
- - `mentionRenderContext?: unknown` - dynamic context available in `renderMention`.
53
+ - `renderItem?: (item) => string`.
54
+ - `renderMention?: (args) => void`.
55
+ - `mentionRenderContext?: unknown`.
63
56
 
64
- ## Data Model
57
+ ## Mention Data Model
65
58
 
66
59
  ```ts
67
60
  type MentionItem = {
@@ -75,7 +68,7 @@ type MentionItem = {
75
68
 
76
69
  ## Save/Load with Stable IDs
77
70
 
78
- Serialize editor output before sending to backend:
71
+ Use helper functions to send stable mention IDs to backend and restore output for Editor.js.
79
72
 
80
73
  ```ts
81
74
  import { encodeMentionsInOutput, decodeMentionsInOutput } from "@editorjs-mentions/plugin";
@@ -83,12 +76,11 @@ import { encodeMentionsInOutput, decodeMentionsInOutput } from "@editorjs-mentio
83
76
  const nativeOutput = await editor.save();
84
77
  const payloadForServer = encodeMentionsInOutput(nativeOutput);
85
78
 
86
- // later when loading existing content:
87
- const payloadFromServer = await fetch(...).then((r) => r.json());
88
- const payloadForEditor = decodeMentionsInOutput(payloadFromServer);
79
+ // later when loading:
80
+ const payloadForEditor = decodeMentionsInOutput(payloadForServer);
89
81
  ```
90
82
 
91
- Example stored paragraph:
83
+ Example serialized paragraph:
92
84
 
93
85
  ```json
94
86
  {
@@ -101,41 +93,49 @@ Example stored paragraph:
101
93
  "id": "u-1001",
102
94
  "displayName": "John Doe",
103
95
  "start": 0,
104
- "end": 9,
105
- "description": "Engineering",
106
- "link": "https://example.local/users/u-1001"
96
+ "end": 9
107
97
  }
108
98
  ]
109
99
  }
110
100
  }
111
101
  ```
112
102
 
113
- ## Runtime Styling Updates
114
-
115
- Use ID-based context and re-render mentions when app state changes:
103
+ ## Dynamic Mention Styling
116
104
 
117
105
  ```ts
118
- mentions.setMentionRenderContext({ currentUserId: loggedUser.id });
106
+ const mentions = new EditorJSMentions({
107
+ holder: "editor",
108
+ provider,
109
+ mentionRenderContext: { currentUserId: "u-1002" },
110
+ renderMention: ({ item, defaultText, element, context }) => {
111
+ const ctx = context as { currentUserId?: string } | undefined;
112
+ element.textContent = defaultText;
113
+ element.style.fontWeight = ctx?.currentUserId === item.id ? "700" : "400";
114
+ }
115
+ });
116
+
117
+ mentions.setMentionRenderContext({ currentUserId: "u-1001" });
119
118
  mentions.refreshMentionRendering();
120
119
  ```
121
120
 
122
- `renderMention` receives:
121
+ ## REST Provider
122
+
123
+ Use built-in provider factory:
123
124
 
124
125
  ```ts
125
- {
126
- item: MentionItem; // full mention metadata (id, displayName, ...)
127
- trigger: string; // matched trigger, e.g. "@"
128
- defaultText: string; // default visible text, e.g. "@John Doe"
129
- element: HTMLAnchorElement; // mention DOM element to customize
130
- source: "insert" | "paste" | "refresh";
131
- context?: unknown; // mentionRenderContext passed by app
132
- }
126
+ createRestMentionProvider({
127
+ endpoint: "http://localhost:3001/api/mentions/users"
128
+ });
133
129
  ```
134
130
 
131
+ Expected endpoint example:
132
+
133
+ `GET /api/mentions/users?query=jo&trigger=@&limit=8`
134
+
135
135
  ## Clipboard Notes
136
136
 
137
- - In-editor copy/paste keeps mention structure via custom clipboard payload + HTML normalization.
138
- - Pasting into external apps falls back to plain text representation (for example `@John Doe`).
137
+ - Copy/paste inside Editor.js keeps mention metadata.
138
+ - Pasting to external apps falls back to plain mention text (for example `@John Doe`).
139
139
 
140
140
  ## Public API
141
141
 
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@editorjs-mentions/plugin",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Mentions autocomplete plugin for Editor.js",
5
5
  "license": "MIT",
6
6
  "author": "editorjs-mentions contributors",
@@ -27,7 +27,8 @@
27
27
  "dist",
28
28
  "README.md",
29
29
  "LICENSE",
30
- "docs/mentions-example-autocomplete.png"
30
+ "docs/mentions-example-autocomplete.png",
31
+ "docs/mentions-usage-example.gif"
31
32
  ],
32
33
  "sideEffects": false,
33
34
  "scripts": {