@mzeahmed/rich-editor 0.0.0-stage → 0.1.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/CHANGELOG.md +65 -0
- package/LICENSE +21 -0
- package/README.md +301 -2
- package/dist/Editor.d.ts +273 -0
- package/dist/Editor.js +959 -0
- package/dist/EmojiPicker.d.ts +33 -0
- package/dist/EmojiPicker.js +268 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +5 -0
- package/dist/messages.d.ts +61 -0
- package/dist/messages.js +111 -0
- package/dist/security.d.ts +20 -0
- package/dist/security.js +124 -0
- package/dist/style.css +486 -0
- package/dist/styles.d.ts +22 -0
- package/dist/styles.js +515 -0
- package/dist/types.d.ts +28 -0
- package/dist/types.js +1 -0
- package/package.json +60 -4
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { EmojiPickerOptions } from './types.js';
|
|
2
|
+
export default class EmojiPicker {
|
|
3
|
+
private static root;
|
|
4
|
+
private static panel;
|
|
5
|
+
private static tabs;
|
|
6
|
+
private static onSelect;
|
|
7
|
+
private static previousFocus;
|
|
8
|
+
private static previousBodyOverflow;
|
|
9
|
+
private static data;
|
|
10
|
+
private static dataLocale;
|
|
11
|
+
private static pending;
|
|
12
|
+
/**
|
|
13
|
+
* Opens the picker: the window appears at once, the emojis arrive as soon as their data
|
|
14
|
+
* is loaded. The promise resolves once the emojis are displayed (or, if loading fails, once the
|
|
15
|
+
* error message is displayed): it is never rejected.
|
|
16
|
+
*/
|
|
17
|
+
static open(options: EmojiPickerOptions): Promise<void>;
|
|
18
|
+
static close(): void;
|
|
19
|
+
static destroy(): void;
|
|
20
|
+
/** The displayed data is that of the current language (it may change between two openings). */
|
|
21
|
+
private static hasCurrentData;
|
|
22
|
+
/** Only one setup at a time: reopening during loading waits for the same one. */
|
|
23
|
+
private static showEmojis;
|
|
24
|
+
private static focusSelectedTab;
|
|
25
|
+
/** Displays the categories and the emojis, or the loading error; never rejects. */
|
|
26
|
+
private static renderEmojis;
|
|
27
|
+
private static setPanelMessage;
|
|
28
|
+
private static ensureMounted;
|
|
29
|
+
private static createTab;
|
|
30
|
+
private static activateGroup;
|
|
31
|
+
private static handleTabKeydown;
|
|
32
|
+
private static handleDocumentKeydown;
|
|
33
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { getEditorLocale, message } from './messages.js';
|
|
3
|
+
import { escapeHtml } from './security.js';
|
|
4
|
+
import { EMOJI_PICKER_CSS, injectStyleSheet, removeStyleSheet } from './styles.js';
|
|
5
|
+
const ROOT_CLASS = 'mzre-emoji-picker';
|
|
6
|
+
const STYLE_ID = 'mzre-emoji-picker-styles';
|
|
7
|
+
/**
|
|
8
|
+
* Emoji data loaders, one per language. The imports are written out literally:
|
|
9
|
+
* bundlers turn each into a separate file per language, loaded on demand.
|
|
10
|
+
*/
|
|
11
|
+
const LOADERS = {
|
|
12
|
+
en: () => Promise.all([import('emojibase-data/en/data.json'), import('emojibase-data/en/messages.json')]),
|
|
13
|
+
fr: () => Promise.all([import('emojibase-data/fr/data.json'), import('emojibase-data/fr/messages.json')])
|
|
14
|
+
};
|
|
15
|
+
const emojiData = new Map();
|
|
16
|
+
/**
|
|
17
|
+
* Loads a language's emoji data on first open only (once per language).
|
|
18
|
+
* A static import would weigh down the package for all its users (~1 MB) and break its
|
|
19
|
+
* server-side import: Node rejects a JSON import without an attribute. On failure, the next
|
|
20
|
+
* open retries.
|
|
21
|
+
*/
|
|
22
|
+
function loadEmojiData(locale) {
|
|
23
|
+
let loading = emojiData.get(locale);
|
|
24
|
+
if (!loading) {
|
|
25
|
+
loading = LOADERS[locale]().then(([data, messages]) => {
|
|
26
|
+
const emojis = data.default;
|
|
27
|
+
return {
|
|
28
|
+
emojis,
|
|
29
|
+
groups: messages.default.groups
|
|
30
|
+
.filter(group => emojis.some(emoji => emoji.group === group.order))
|
|
31
|
+
};
|
|
32
|
+
}).catch((error) => {
|
|
33
|
+
emojiData.delete(locale);
|
|
34
|
+
throw error;
|
|
35
|
+
});
|
|
36
|
+
emojiData.set(locale, loading);
|
|
37
|
+
}
|
|
38
|
+
return loading;
|
|
39
|
+
}
|
|
40
|
+
class EmojiPicker {
|
|
41
|
+
/**
|
|
42
|
+
* Opens the picker: the window appears at once, the emojis arrive as soon as their data
|
|
43
|
+
* is loaded. The promise resolves once the emojis are displayed (or, if loading fails, once the
|
|
44
|
+
* error message is displayed): it is never rejected.
|
|
45
|
+
*/
|
|
46
|
+
static open(options) {
|
|
47
|
+
this.ensureMounted();
|
|
48
|
+
this.onSelect = options.onSelect;
|
|
49
|
+
this.previousFocus = document.activeElement instanceof HTMLElement
|
|
50
|
+
? document.activeElement
|
|
51
|
+
: null;
|
|
52
|
+
const title = this.root.querySelector('.mzre-emoji-picker__title');
|
|
53
|
+
const close = this.root.querySelector('.mzre-emoji-picker__close');
|
|
54
|
+
if (title)
|
|
55
|
+
title.textContent = options.title ?? message('chooseEmoji');
|
|
56
|
+
if (close)
|
|
57
|
+
close.setAttribute('aria-label', options.closeLabel ?? message('close'));
|
|
58
|
+
this.root.querySelector('.mzre-emoji-picker__tabs')?.setAttribute('aria-label', message('emojiCategories'));
|
|
59
|
+
this.previousBodyOverflow = document.body.style.overflow;
|
|
60
|
+
document.body.style.overflow = 'hidden';
|
|
61
|
+
this.root.hidden = false;
|
|
62
|
+
document.addEventListener('keydown', this.handleDocumentKeydown);
|
|
63
|
+
// Data already loaded: fall back to the active tab; otherwise to the panel, while waiting.
|
|
64
|
+
if (this.hasCurrentData())
|
|
65
|
+
this.focusSelectedTab();
|
|
66
|
+
else
|
|
67
|
+
this.panel?.focus();
|
|
68
|
+
return this.showEmojis();
|
|
69
|
+
}
|
|
70
|
+
static close() {
|
|
71
|
+
if (!this.root || this.root.hidden)
|
|
72
|
+
return;
|
|
73
|
+
this.root.hidden = true;
|
|
74
|
+
this.onSelect = null;
|
|
75
|
+
document.body.style.overflow = this.previousBodyOverflow;
|
|
76
|
+
document.removeEventListener('keydown', this.handleDocumentKeydown);
|
|
77
|
+
this.previousFocus?.focus();
|
|
78
|
+
this.previousFocus = null;
|
|
79
|
+
}
|
|
80
|
+
static destroy() {
|
|
81
|
+
if (this.root && !this.root.hidden)
|
|
82
|
+
this.close();
|
|
83
|
+
this.root?.remove();
|
|
84
|
+
removeStyleSheet(STYLE_ID);
|
|
85
|
+
this.root = null;
|
|
86
|
+
this.panel = null;
|
|
87
|
+
this.tabs = [];
|
|
88
|
+
this.onSelect = null;
|
|
89
|
+
this.data = null;
|
|
90
|
+
this.dataLocale = null;
|
|
91
|
+
this.pending = null;
|
|
92
|
+
}
|
|
93
|
+
/** The displayed data is that of the current language (it may change between two openings). */
|
|
94
|
+
static hasCurrentData() {
|
|
95
|
+
return this.data !== null && this.dataLocale === getEditorLocale();
|
|
96
|
+
}
|
|
97
|
+
/** Only one setup at a time: reopening during loading waits for the same one. */
|
|
98
|
+
static showEmojis() {
|
|
99
|
+
if (this.hasCurrentData())
|
|
100
|
+
return Promise.resolve();
|
|
101
|
+
if (this.pending)
|
|
102
|
+
return this.pending;
|
|
103
|
+
const pending = this.renderEmojis().finally(() => {
|
|
104
|
+
// Only if it is still the current one: a destroy() then a new open() may have replaced it.
|
|
105
|
+
if (this.pending === pending)
|
|
106
|
+
this.pending = null;
|
|
107
|
+
});
|
|
108
|
+
this.pending = pending;
|
|
109
|
+
return pending;
|
|
110
|
+
}
|
|
111
|
+
static focusSelectedTab() {
|
|
112
|
+
this.tabs.find(tab => tab.getAttribute('aria-selected') === 'true')?.focus();
|
|
113
|
+
}
|
|
114
|
+
/** Displays the categories and the emojis, or the loading error; never rejects. */
|
|
115
|
+
static async renderEmojis() {
|
|
116
|
+
const root = this.root;
|
|
117
|
+
const locale = getEditorLocale();
|
|
118
|
+
this.panel?.setAttribute('aria-busy', 'true');
|
|
119
|
+
this.setPanelMessage(message('emojiLoading'));
|
|
120
|
+
try {
|
|
121
|
+
const data = await loadEmojiData(locale);
|
|
122
|
+
// Destroyed or remounted in the meantime: this data no longer concerns this window.
|
|
123
|
+
if (this.root !== root || !root)
|
|
124
|
+
return;
|
|
125
|
+
this.data = data;
|
|
126
|
+
this.dataLocale = locale;
|
|
127
|
+
const tabsContainer = root.querySelector('.mzre-emoji-picker__tabs');
|
|
128
|
+
tabsContainer.replaceChildren();
|
|
129
|
+
this.tabs = data.groups.map((group, index) => this.createTab(group, index, tabsContainer));
|
|
130
|
+
this.activateGroup(0);
|
|
131
|
+
if (!root.hidden)
|
|
132
|
+
this.focusSelectedTab();
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
console.error('[EmojiPicker] could not load the emojis', error);
|
|
136
|
+
if (this.root === root)
|
|
137
|
+
this.setPanelMessage(message('emojiLoadError'));
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
if (this.root === root)
|
|
141
|
+
this.panel?.removeAttribute('aria-busy');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
static setPanelMessage(message) {
|
|
145
|
+
const paragraph = document.createElement('p');
|
|
146
|
+
paragraph.className = 'mzre-emoji-picker__status';
|
|
147
|
+
paragraph.setAttribute('role', 'status');
|
|
148
|
+
paragraph.textContent = message;
|
|
149
|
+
this.panel?.replaceChildren(paragraph);
|
|
150
|
+
}
|
|
151
|
+
static ensureMounted() {
|
|
152
|
+
if (this.root?.isConnected)
|
|
153
|
+
return;
|
|
154
|
+
injectStyleSheet(STYLE_ID, EMOJI_PICKER_CSS);
|
|
155
|
+
const root = document.createElement('div');
|
|
156
|
+
root.className = ROOT_CLASS;
|
|
157
|
+
root.hidden = true;
|
|
158
|
+
root.innerHTML = `
|
|
159
|
+
<section class="mzre-emoji-picker__dialog" role="dialog" aria-modal="true" aria-labelledby="mzre-emoji-picker-title">
|
|
160
|
+
<header class="mzre-emoji-picker__header">
|
|
161
|
+
<h2 class="mzre-emoji-picker__title" id="mzre-emoji-picker-title">${escapeHtml(message('chooseEmoji'))}</h2>
|
|
162
|
+
<button class="mzre-emoji-picker__close" type="button" aria-label="${escapeHtml(message('close'))}">×</button>
|
|
163
|
+
</header>
|
|
164
|
+
<div class="mzre-emoji-picker__tabs" role="tablist" aria-label="${escapeHtml(message('emojiCategories'))}"></div>
|
|
165
|
+
<div class="mzre-emoji-picker__panel" role="tabpanel" tabindex="0"></div>
|
|
166
|
+
</section>
|
|
167
|
+
`;
|
|
168
|
+
root.addEventListener('click', event => {
|
|
169
|
+
if (event.target === root)
|
|
170
|
+
this.close();
|
|
171
|
+
});
|
|
172
|
+
root.querySelector('.mzre-emoji-picker__close')?.addEventListener('click', () => this.close());
|
|
173
|
+
this.root = root;
|
|
174
|
+
this.panel = root.querySelector('.mzre-emoji-picker__panel');
|
|
175
|
+
document.body.appendChild(root);
|
|
176
|
+
}
|
|
177
|
+
static createTab(group, index, container) {
|
|
178
|
+
const tab = document.createElement('button');
|
|
179
|
+
tab.type = 'button';
|
|
180
|
+
tab.className = 'mzre-emoji-picker__tab';
|
|
181
|
+
tab.id = `mzre-emoji-tab-${group.key}`;
|
|
182
|
+
tab.setAttribute('role', 'tab');
|
|
183
|
+
tab.setAttribute('aria-selected', String(index === 0));
|
|
184
|
+
tab.setAttribute('aria-controls', 'mzre-emoji-picker-panel');
|
|
185
|
+
tab.tabIndex = index === 0 ? 0 : -1;
|
|
186
|
+
tab.textContent = group.message;
|
|
187
|
+
tab.addEventListener('click', () => this.activateGroup(index));
|
|
188
|
+
tab.addEventListener('keydown', event => this.handleTabKeydown(event, index));
|
|
189
|
+
container.appendChild(tab);
|
|
190
|
+
return tab;
|
|
191
|
+
}
|
|
192
|
+
static activateGroup(index) {
|
|
193
|
+
const group = this.data?.groups[index];
|
|
194
|
+
if (!group || !this.panel)
|
|
195
|
+
return;
|
|
196
|
+
this.tabs.forEach((tab, tabIndex) => {
|
|
197
|
+
tab.setAttribute('aria-selected', String(tabIndex === index));
|
|
198
|
+
tab.tabIndex = tabIndex === index ? 0 : -1;
|
|
199
|
+
});
|
|
200
|
+
this.panel.id = 'mzre-emoji-picker-panel';
|
|
201
|
+
this.panel.setAttribute('aria-labelledby', `mzre-emoji-tab-${group.key}`);
|
|
202
|
+
this.panel.replaceChildren();
|
|
203
|
+
this.data.emojis.filter(emoji => emoji.group === group.order).forEach(emoji => {
|
|
204
|
+
const button = document.createElement('button');
|
|
205
|
+
button.type = 'button';
|
|
206
|
+
button.className = 'mzre-emoji-picker__item';
|
|
207
|
+
button.textContent = emoji.emoji;
|
|
208
|
+
button.title = emoji.label;
|
|
209
|
+
button.setAttribute('aria-label', emoji.label);
|
|
210
|
+
button.addEventListener('click', () => {
|
|
211
|
+
const callback = this.onSelect;
|
|
212
|
+
this.close();
|
|
213
|
+
callback?.(emoji.emoji);
|
|
214
|
+
});
|
|
215
|
+
this.panel.appendChild(button);
|
|
216
|
+
});
|
|
217
|
+
this.panel.scrollTop = 0;
|
|
218
|
+
}
|
|
219
|
+
static handleTabKeydown(event, index) {
|
|
220
|
+
let nextIndex = null;
|
|
221
|
+
if (event.key === 'ArrowRight')
|
|
222
|
+
nextIndex = (index + 1) % this.tabs.length;
|
|
223
|
+
if (event.key === 'ArrowLeft')
|
|
224
|
+
nextIndex = (index - 1 + this.tabs.length) % this.tabs.length;
|
|
225
|
+
if (event.key === 'Home')
|
|
226
|
+
nextIndex = 0;
|
|
227
|
+
if (event.key === 'End')
|
|
228
|
+
nextIndex = this.tabs.length - 1;
|
|
229
|
+
if (nextIndex === null)
|
|
230
|
+
return;
|
|
231
|
+
event.preventDefault();
|
|
232
|
+
this.activateGroup(nextIndex);
|
|
233
|
+
this.tabs[nextIndex]?.focus();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
_a = EmojiPicker;
|
|
237
|
+
EmojiPicker.root = null;
|
|
238
|
+
EmojiPicker.panel = null;
|
|
239
|
+
EmojiPicker.tabs = [];
|
|
240
|
+
EmojiPicker.onSelect = null;
|
|
241
|
+
EmojiPicker.previousFocus = null;
|
|
242
|
+
EmojiPicker.previousBodyOverflow = '';
|
|
243
|
+
EmojiPicker.data = null;
|
|
244
|
+
EmojiPicker.dataLocale = null;
|
|
245
|
+
EmojiPicker.pending = null;
|
|
246
|
+
EmojiPicker.handleDocumentKeydown = (event) => {
|
|
247
|
+
if (event.key === 'Escape') {
|
|
248
|
+
event.preventDefault();
|
|
249
|
+
_a.close();
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (event.key !== 'Tab' || !_a.root)
|
|
253
|
+
return;
|
|
254
|
+
const focusable = Array.from(_a.root.querySelectorAll('button:not([disabled]), [href], input:not([disabled]), [tabindex]:not([tabindex="-1"])')).filter(element => !element.hidden);
|
|
255
|
+
const first = focusable[0];
|
|
256
|
+
const last = focusable[focusable.length - 1];
|
|
257
|
+
if (!first || !last)
|
|
258
|
+
return;
|
|
259
|
+
if (event.shiftKey && document.activeElement === first) {
|
|
260
|
+
event.preventDefault();
|
|
261
|
+
last.focus();
|
|
262
|
+
}
|
|
263
|
+
else if (!event.shiftKey && document.activeElement === last) {
|
|
264
|
+
event.preventDefault();
|
|
265
|
+
first.focus();
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
export default EmojiPicker;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { default as Editor } from './Editor.js';
|
|
2
|
+
export { default } from './Editor.js';
|
|
3
|
+
export { default as EmojiPicker } from './EmojiPicker.js';
|
|
4
|
+
export type { EditorChange, EditorOptions } from './Editor.js';
|
|
5
|
+
export { messagesEn, messagesFr, setEditorLocale, setEditorMessages } from './messages.js';
|
|
6
|
+
export type { EditorLocale, EditorMessages } from './messages.js';
|
|
7
|
+
export { setEditorStyles } from './styles.js';
|
|
8
|
+
export type { StyleOptions } from './styles.js';
|
|
9
|
+
export type { EmojiPickerOptions, EditorIntegrations, EditorLoader, EditorMentionIntegration, EditorUser, } from './types.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { default as Editor } from './Editor.js';
|
|
2
|
+
export { default } from './Editor.js';
|
|
3
|
+
export { default as EmojiPicker } from './EmojiPicker.js';
|
|
4
|
+
export { messagesEn, messagesFr, setEditorLocale, setEditorMessages } from './messages.js';
|
|
5
|
+
export { setEditorStyles } from './styles.js';
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface texts of the editor. English by default; French is provided. Each text
|
|
3
|
+
* can be overridden (`integrations.messages`) and another language is set with `setEditorLocale()`
|
|
4
|
+
* or the `locale` option.
|
|
5
|
+
*
|
|
6
|
+
* The language is a page-wide setting: the last `init()`/`mount()`/`setEditorLocale()`
|
|
7
|
+
* wins, for all editors and for the emoji picker.
|
|
8
|
+
*/
|
|
9
|
+
export type EditorLocale = 'en' | 'fr';
|
|
10
|
+
export interface EditorMessages {
|
|
11
|
+
toggleToolbar: string;
|
|
12
|
+
formatting: string;
|
|
13
|
+
textStyle: string;
|
|
14
|
+
paragraph: string;
|
|
15
|
+
heading: string;
|
|
16
|
+
subheading: string;
|
|
17
|
+
quote: string;
|
|
18
|
+
bold: string;
|
|
19
|
+
italic: string;
|
|
20
|
+
underline: string;
|
|
21
|
+
strikethrough: string;
|
|
22
|
+
bulletList: string;
|
|
23
|
+
numberedList: string;
|
|
24
|
+
alignLeft: string;
|
|
25
|
+
alignCenter: string;
|
|
26
|
+
alignRight: string;
|
|
27
|
+
insertLink: string;
|
|
28
|
+
clearFormatting: string;
|
|
29
|
+
undo: string;
|
|
30
|
+
redo: string;
|
|
31
|
+
insert: string;
|
|
32
|
+
remove: string;
|
|
33
|
+
placeholder: string;
|
|
34
|
+
character: string;
|
|
35
|
+
characters: string;
|
|
36
|
+
html: string;
|
|
37
|
+
clear: string;
|
|
38
|
+
copyContent: string;
|
|
39
|
+
clearConfirm: string;
|
|
40
|
+
copied: string;
|
|
41
|
+
addEmoji: string;
|
|
42
|
+
chooseEmoji: string;
|
|
43
|
+
close: string;
|
|
44
|
+
noUserFound: string;
|
|
45
|
+
mentionSuggestions: string;
|
|
46
|
+
/** `{count}` is replaced by the number of suggestions. */
|
|
47
|
+
mentionResultsOne: string;
|
|
48
|
+
mentionResultsMany: string;
|
|
49
|
+
emojiCategories: string;
|
|
50
|
+
emojiLoading: string;
|
|
51
|
+
emojiLoadError: string;
|
|
52
|
+
}
|
|
53
|
+
export declare const messagesEn: EditorMessages;
|
|
54
|
+
export declare const messagesFr: EditorMessages;
|
|
55
|
+
/** Language of the interface and of the emoji data (`en` by default). An unknown value is ignored. */
|
|
56
|
+
export declare function setEditorLocale(locale: EditorLocale): void;
|
|
57
|
+
/** Overrides some texts, whatever the language; `undefined` or `{}` removes the overrides. */
|
|
58
|
+
export declare function setEditorMessages(messages: Partial<EditorMessages> | undefined): void;
|
|
59
|
+
export declare function getEditorLocale(): EditorLocale;
|
|
60
|
+
/** Plain text (to be escaped before inserting it into HTML). */
|
|
61
|
+
export declare function message(key: keyof EditorMessages): string;
|
package/dist/messages.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface texts of the editor. English by default; French is provided. Each text
|
|
3
|
+
* can be overridden (`integrations.messages`) and another language is set with `setEditorLocale()`
|
|
4
|
+
* or the `locale` option.
|
|
5
|
+
*
|
|
6
|
+
* The language is a page-wide setting: the last `init()`/`mount()`/`setEditorLocale()`
|
|
7
|
+
* wins, for all editors and for the emoji picker.
|
|
8
|
+
*/
|
|
9
|
+
export const messagesEn = {
|
|
10
|
+
toggleToolbar: 'Show / hide the toolbar',
|
|
11
|
+
formatting: 'Formatting',
|
|
12
|
+
textStyle: 'Text style',
|
|
13
|
+
paragraph: 'Paragraph',
|
|
14
|
+
heading: 'Heading',
|
|
15
|
+
subheading: 'Subheading',
|
|
16
|
+
quote: 'Quote',
|
|
17
|
+
bold: 'Bold',
|
|
18
|
+
italic: 'Italic',
|
|
19
|
+
underline: 'Underline',
|
|
20
|
+
strikethrough: 'Strikethrough',
|
|
21
|
+
bulletList: 'Bulleted list',
|
|
22
|
+
numberedList: 'Numbered list',
|
|
23
|
+
alignLeft: 'Align left',
|
|
24
|
+
alignCenter: 'Center',
|
|
25
|
+
alignRight: 'Align right',
|
|
26
|
+
insertLink: 'Insert a link',
|
|
27
|
+
clearFormatting: 'Clear formatting',
|
|
28
|
+
undo: 'Undo',
|
|
29
|
+
redo: 'Redo',
|
|
30
|
+
insert: 'Insert',
|
|
31
|
+
remove: 'Remove',
|
|
32
|
+
placeholder: 'Write here…',
|
|
33
|
+
character: 'character',
|
|
34
|
+
characters: 'characters',
|
|
35
|
+
html: 'HTML',
|
|
36
|
+
clear: 'Clear',
|
|
37
|
+
copyContent: 'Copy content',
|
|
38
|
+
clearConfirm: 'Clear all content?',
|
|
39
|
+
copied: 'Copied ✓',
|
|
40
|
+
addEmoji: 'Add an emoji',
|
|
41
|
+
chooseEmoji: 'Choose an emoji',
|
|
42
|
+
close: 'Close',
|
|
43
|
+
noUserFound: 'No user found',
|
|
44
|
+
mentionSuggestions: 'Mention suggestions',
|
|
45
|
+
mentionResultsOne: '1 suggestion available. Use the up and down arrow keys to choose, then Enter to insert.',
|
|
46
|
+
mentionResultsMany: '{count} suggestions available. Use the up and down arrow keys to choose, then Enter to insert.',
|
|
47
|
+
emojiCategories: 'Emoji categories',
|
|
48
|
+
emojiLoading: 'Loading emojis…',
|
|
49
|
+
emojiLoadError: 'Could not load the emojis. Close and reopen the picker.'
|
|
50
|
+
};
|
|
51
|
+
export const messagesFr = {
|
|
52
|
+
toggleToolbar: "Afficher / masquer la barre d'outils",
|
|
53
|
+
formatting: 'Mise en forme',
|
|
54
|
+
textStyle: 'Style du texte',
|
|
55
|
+
paragraph: 'Paragraphe',
|
|
56
|
+
heading: 'Titre',
|
|
57
|
+
subheading: 'Sous-titre',
|
|
58
|
+
quote: 'Citation',
|
|
59
|
+
bold: 'Gras',
|
|
60
|
+
italic: 'Italique',
|
|
61
|
+
underline: 'Souligné',
|
|
62
|
+
strikethrough: 'Barré',
|
|
63
|
+
bulletList: 'Liste à puces',
|
|
64
|
+
numberedList: 'Liste numérotée',
|
|
65
|
+
alignLeft: 'Aligner à gauche',
|
|
66
|
+
alignCenter: 'Centrer',
|
|
67
|
+
alignRight: 'Aligner à droite',
|
|
68
|
+
insertLink: 'Insérer un lien',
|
|
69
|
+
clearFormatting: 'Effacer la mise en forme',
|
|
70
|
+
undo: 'Annuler',
|
|
71
|
+
redo: 'Rétablir',
|
|
72
|
+
insert: 'Insérer',
|
|
73
|
+
remove: 'Supprimer',
|
|
74
|
+
placeholder: 'Écrivez ici…',
|
|
75
|
+
character: 'caractère',
|
|
76
|
+
characters: 'caractères',
|
|
77
|
+
html: 'HTML',
|
|
78
|
+
clear: 'Effacer',
|
|
79
|
+
copyContent: 'Copier le contenu',
|
|
80
|
+
clearConfirm: 'Effacer tout le contenu ?',
|
|
81
|
+
copied: 'Copié ✓',
|
|
82
|
+
addEmoji: 'Ajouter un emoji',
|
|
83
|
+
chooseEmoji: 'Choisir un emoji',
|
|
84
|
+
close: 'Fermer',
|
|
85
|
+
noUserFound: 'Aucun utilisateur trouvé',
|
|
86
|
+
mentionSuggestions: 'Suggestions de mention',
|
|
87
|
+
mentionResultsOne: '1 suggestion disponible. Utilisez les flèches haut et bas pour choisir, puis Entrée pour insérer.',
|
|
88
|
+
mentionResultsMany: '{count} suggestions disponibles. Utilisez les flèches haut et bas pour choisir, puis Entrée pour insérer.',
|
|
89
|
+
emojiCategories: "Catégories d'emojis",
|
|
90
|
+
emojiLoading: 'Chargement des emojis…',
|
|
91
|
+
emojiLoadError: 'Impossible de charger les emojis. Fermez puis rouvrez le sélecteur.'
|
|
92
|
+
};
|
|
93
|
+
const CATALOGS = { en: messagesEn, fr: messagesFr };
|
|
94
|
+
let currentLocale = 'en';
|
|
95
|
+
let overrides = {};
|
|
96
|
+
/** Language of the interface and of the emoji data (`en` by default). An unknown value is ignored. */
|
|
97
|
+
export function setEditorLocale(locale) {
|
|
98
|
+
if (locale in CATALOGS)
|
|
99
|
+
currentLocale = locale;
|
|
100
|
+
}
|
|
101
|
+
/** Overrides some texts, whatever the language; `undefined` or `{}` removes the overrides. */
|
|
102
|
+
export function setEditorMessages(messages) {
|
|
103
|
+
overrides = { ...messages };
|
|
104
|
+
}
|
|
105
|
+
export function getEditorLocale() {
|
|
106
|
+
return currentLocale;
|
|
107
|
+
}
|
|
108
|
+
/** Plain text (to be escaped before inserting it into HTML). */
|
|
109
|
+
export function message(key) {
|
|
110
|
+
return overrides[key] ?? CATALOGS[currentLocale][key];
|
|
111
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Security helpers of the editor: HTML escaping, accepted URLs and sanitizing of the HTML
|
|
3
|
+
* loaded into the writing area. Internal to the package (not exported by `index.ts`).
|
|
4
|
+
*/
|
|
5
|
+
/** Escapes plain text so it can be inserted into HTML, including inside a quoted attribute. */
|
|
6
|
+
export declare function escapeHtml(value: unknown): string;
|
|
7
|
+
/** `http(s)` URL (absolute, or relative resolved against the page); null otherwise (`javascript:`, `data:`…). */
|
|
8
|
+
export declare function safeHttpUrl(value: unknown): string | null;
|
|
9
|
+
/**
|
|
10
|
+
* Path of a profile on the site (no domain, query or anchor), or null if the URL is
|
|
11
|
+
* unusable. Leading slashes are collapsed to one: `//other-site.test` would otherwise
|
|
12
|
+
* be a link to another domain.
|
|
13
|
+
*/
|
|
14
|
+
export declare function safeSitePath(value: unknown): string | null;
|
|
15
|
+
/**
|
|
16
|
+
* Sanitizes HTML before loading it into the writing area: allow-list of tags and
|
|
17
|
+
* attributes, no script, no event handler, no `javascript:` URL. The HTML is
|
|
18
|
+
* parsed in an inert document (`DOMParser`): nothing runs during sanitizing.
|
|
19
|
+
*/
|
|
20
|
+
export declare function sanitizeHtml(html: string): string;
|
package/dist/security.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Security helpers of the editor: HTML escaping, accepted URLs and sanitizing of the HTML
|
|
3
|
+
* loaded into the writing area. Internal to the package (not exported by `index.ts`).
|
|
4
|
+
*/
|
|
5
|
+
const HTML_ESCAPES = {
|
|
6
|
+
'&': '&',
|
|
7
|
+
'<': '<',
|
|
8
|
+
'>': '>',
|
|
9
|
+
'"': '"',
|
|
10
|
+
"'": '''
|
|
11
|
+
};
|
|
12
|
+
/** Escapes plain text so it can be inserted into HTML, including inside a quoted attribute. */
|
|
13
|
+
export function escapeHtml(value) {
|
|
14
|
+
return String(value ?? '').replace(/[&<>"']/g, (char) => HTML_ESCAPES[char]);
|
|
15
|
+
}
|
|
16
|
+
function resolveUrl(value) {
|
|
17
|
+
if (typeof value !== 'string' || value.trim() === '')
|
|
18
|
+
return null;
|
|
19
|
+
try {
|
|
20
|
+
return new URL(value.trim(), typeof window === 'undefined' ? 'http://localhost/' : window.location.href);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** `http(s)` URL (absolute, or relative resolved against the page); null otherwise (`javascript:`, `data:`…). */
|
|
27
|
+
export function safeHttpUrl(value) {
|
|
28
|
+
const url = resolveUrl(value);
|
|
29
|
+
return url && (url.protocol === 'http:' || url.protocol === 'https:') ? url.href : null;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Path of a profile on the site (no domain, query or anchor), or null if the URL is
|
|
33
|
+
* unusable. Leading slashes are collapsed to one: `//other-site.test` would otherwise
|
|
34
|
+
* be a link to another domain.
|
|
35
|
+
*/
|
|
36
|
+
export function safeSitePath(value) {
|
|
37
|
+
const url = resolveUrl(value);
|
|
38
|
+
if (!url || (url.protocol !== 'http:' && url.protocol !== 'https:'))
|
|
39
|
+
return null;
|
|
40
|
+
return url.pathname.replace(/^\/{2,}/, '/');
|
|
41
|
+
}
|
|
42
|
+
/* ─────────────────────────────────────────
|
|
43
|
+
Sanitizing the HTML loaded into the editor
|
|
44
|
+
───────────────────────────────────────── */
|
|
45
|
+
/** Kept tags: those the toolbar produces, plus links and mentions. */
|
|
46
|
+
const ALLOWED_TAGS = new Set([
|
|
47
|
+
'a', 'b', 'blockquote', 'br', 'code', 'div', 'em', 'h2', 'h3', 'i', 'li', 'ol', 'p', 'pre',
|
|
48
|
+
's', 'span', 'strike', 'strong', 'u', 'ul'
|
|
49
|
+
]);
|
|
50
|
+
/** Tags removed along with their content (other unknown tags are stripped, text kept). */
|
|
51
|
+
const DROPPED_WITH_CONTENT = new Set([
|
|
52
|
+
'script', 'style', 'iframe', 'frame', 'frameset', 'object', 'embed', 'noscript', 'template',
|
|
53
|
+
'svg', 'math', 'textarea', 'select', 'option', 'title', 'head'
|
|
54
|
+
]);
|
|
55
|
+
const TEXT_ALIGN = /^\s*text-align\s*:\s*(left|right|center|justify)\s*;?\s*$/i;
|
|
56
|
+
function isAllowedLink(value) {
|
|
57
|
+
const href = value.trim();
|
|
58
|
+
// Site-relative path (a single "/", never "//"), anchor or explicitly allowed scheme.
|
|
59
|
+
if (/^\/(?!\/)/.test(href) || href.startsWith('#'))
|
|
60
|
+
return true;
|
|
61
|
+
return /^(https?:|mailto:|tel:)/i.test(href);
|
|
62
|
+
}
|
|
63
|
+
function cleanAttributes(element, tag) {
|
|
64
|
+
for (const attribute of Array.from(element.attributes)) {
|
|
65
|
+
const name = attribute.name.toLowerCase();
|
|
66
|
+
const value = attribute.value;
|
|
67
|
+
let keep = false;
|
|
68
|
+
if (tag === 'a') {
|
|
69
|
+
if (name === 'href')
|
|
70
|
+
keep = isAllowedLink(value);
|
|
71
|
+
else if (name === 'target')
|
|
72
|
+
keep = value === '_blank';
|
|
73
|
+
else if (name === 'class')
|
|
74
|
+
keep = value === 'mention';
|
|
75
|
+
else if (name === 'data-user-id')
|
|
76
|
+
keep = true;
|
|
77
|
+
}
|
|
78
|
+
else if (name === 'style') {
|
|
79
|
+
keep = TEXT_ALIGN.test(value);
|
|
80
|
+
}
|
|
81
|
+
else if (name === 'align') {
|
|
82
|
+
keep = /^(left|right|center|justify)$/i.test(value);
|
|
83
|
+
}
|
|
84
|
+
if (!keep)
|
|
85
|
+
element.removeAttribute(attribute.name);
|
|
86
|
+
}
|
|
87
|
+
if (tag === 'a' && element.getAttribute('target') === '_blank') {
|
|
88
|
+
element.setAttribute('rel', 'noopener noreferrer');
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function cleanNode(parent) {
|
|
92
|
+
for (const child of Array.from(parent.childNodes)) {
|
|
93
|
+
if (child.nodeType === Node.TEXT_NODE)
|
|
94
|
+
continue;
|
|
95
|
+
if (child.nodeType !== Node.ELEMENT_NODE) {
|
|
96
|
+
parent.removeChild(child); // commentaires, instructions de traitement
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const element = child;
|
|
100
|
+
const tag = element.tagName.toLowerCase();
|
|
101
|
+
if (DROPPED_WITH_CONTENT.has(tag)) {
|
|
102
|
+
parent.removeChild(element);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
cleanNode(element);
|
|
106
|
+
if (!ALLOWED_TAGS.has(tag)) {
|
|
107
|
+
while (element.firstChild)
|
|
108
|
+
parent.insertBefore(element.firstChild, element);
|
|
109
|
+
parent.removeChild(element);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
cleanAttributes(element, tag);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Sanitizes HTML before loading it into the writing area: allow-list of tags and
|
|
117
|
+
* attributes, no script, no event handler, no `javascript:` URL. The HTML is
|
|
118
|
+
* parsed in an inert document (`DOMParser`): nothing runs during sanitizing.
|
|
119
|
+
*/
|
|
120
|
+
export function sanitizeHtml(html) {
|
|
121
|
+
const document = new DOMParser().parseFromString(html, 'text/html');
|
|
122
|
+
cleanNode(document.body);
|
|
123
|
+
return document.body.innerHTML;
|
|
124
|
+
}
|