tinymce-solid 0.0.8 → 0.0.9
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 +5 -6
- package/dist/dev.js +21 -20
- package/dist/dev.jsx +23 -24
- package/dist/index.js +11 -10
- package/dist/index.jsx +13 -14
- package/package.json +28 -30
- package/eslint.config.mjs +0 -5
- package/src/Events.ts +0 -93
- package/src/ScriptLoader2.ts +0 -182
- package/src/TinyMCE.ts +0 -11
- package/src/Utils.ts +0 -138
- package/src/components/Editor.tsx +0 -427
- package/src/components/EditorPropTypes.ts +0 -191
- package/src/index.tsx +0 -6
- package/test/fixtures/Editor.tsx +0 -6
- package/test/index.test.tsx +0 -227
- package/tsconfig.json +0 -25
- package/tsup.config.ts +0 -40
- package/vitest.config-ui.ts +0 -31
- package/vitest.config.ts +0 -30
package/src/ScriptLoader2.ts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import { uuid } from "./Utils";
|
|
2
|
-
|
|
3
|
-
export type CallbackFn = () => void;
|
|
4
|
-
|
|
5
|
-
export interface ScriptItem {
|
|
6
|
-
src: string;
|
|
7
|
-
async?: boolean;
|
|
8
|
-
defer?: boolean;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
interface Id {
|
|
12
|
-
id: string;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const injectScriptTag = (
|
|
16
|
-
doc: Document,
|
|
17
|
-
item: ScriptItem & Id,
|
|
18
|
-
handler: (id: string, err?: unknown) => void,
|
|
19
|
-
) => {
|
|
20
|
-
const scriptTag = doc.createElement("script");
|
|
21
|
-
scriptTag.referrerPolicy = "origin";
|
|
22
|
-
scriptTag.type = "application/javascript";
|
|
23
|
-
scriptTag.id = item.id;
|
|
24
|
-
scriptTag.src = item.src;
|
|
25
|
-
scriptTag.async = item.async ?? false;
|
|
26
|
-
scriptTag.defer = item.defer ?? false;
|
|
27
|
-
|
|
28
|
-
const loadHandler = () => {
|
|
29
|
-
scriptTag.removeEventListener("load", loadHandler);
|
|
30
|
-
scriptTag.removeEventListener("error", errorHandler);
|
|
31
|
-
handler(item.src);
|
|
32
|
-
};
|
|
33
|
-
/* istanbul ignore next @preserve - this should be covered but the browser mode won't register this part */
|
|
34
|
-
const errorHandler = (err: unknown) => {
|
|
35
|
-
scriptTag.removeEventListener("load", loadHandler);
|
|
36
|
-
scriptTag.removeEventListener("error", errorHandler);
|
|
37
|
-
handler(item.src, err);
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
scriptTag.addEventListener("load", loadHandler);
|
|
41
|
-
scriptTag.addEventListener("error", errorHandler);
|
|
42
|
-
|
|
43
|
-
/* istanbul ignore else @preserve */
|
|
44
|
-
if (doc.head) {
|
|
45
|
-
doc.head.appendChild(scriptTag);
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
interface ScriptState {
|
|
50
|
-
id: string;
|
|
51
|
-
src: string;
|
|
52
|
-
done: boolean;
|
|
53
|
-
error?: unknown;
|
|
54
|
-
handlers: ((src: string, err?: unknown) => void)[];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const createDocumentScriptLoader = (doc: Document) => {
|
|
58
|
-
let lookup: Record<string, ScriptState> = {};
|
|
59
|
-
|
|
60
|
-
const scriptLoadOrErrorHandler = (src: string, err?: unknown) => {
|
|
61
|
-
const item = lookup[src];
|
|
62
|
-
item.done = true;
|
|
63
|
-
item.error = err;
|
|
64
|
-
for (const h of item.handlers) {
|
|
65
|
-
h(src, err);
|
|
66
|
-
}
|
|
67
|
-
item.handlers = [];
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
/* istanbul ignore next @preserve - this should be covered but the browser mode won't register this part */
|
|
71
|
-
const loadScripts = (
|
|
72
|
-
items: ScriptItem[],
|
|
73
|
-
success: () => void,
|
|
74
|
-
failure?: (err: unknown) => void,
|
|
75
|
-
) => {
|
|
76
|
-
const failureOrLog = (err: unknown) =>
|
|
77
|
-
failure !== undefined ? failure(err) : console.error(err);
|
|
78
|
-
if (items.length === 0) {
|
|
79
|
-
failureOrLog(new Error("At least one script must be provided"));
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
let successCount = 0;
|
|
83
|
-
let failed = false;
|
|
84
|
-
const loaded = (_src: string, err?: unknown) => {
|
|
85
|
-
if (failed) {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
if (err) {
|
|
89
|
-
failed = true;
|
|
90
|
-
failureOrLog(err);
|
|
91
|
-
} else if (++successCount === items.length) {
|
|
92
|
-
success();
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
for (const item of items) {
|
|
96
|
-
const existing = lookup[item.src];
|
|
97
|
-
if (existing) {
|
|
98
|
-
if (existing.done) {
|
|
99
|
-
loaded(item.src, existing.error);
|
|
100
|
-
} else {
|
|
101
|
-
existing.handlers.push(loaded);
|
|
102
|
-
}
|
|
103
|
-
} else {
|
|
104
|
-
// create a new entry
|
|
105
|
-
const id = uuid("tiny-");
|
|
106
|
-
lookup[item.src] = {
|
|
107
|
-
id,
|
|
108
|
-
src: item.src,
|
|
109
|
-
done: false,
|
|
110
|
-
error: null,
|
|
111
|
-
handlers: [loaded],
|
|
112
|
-
};
|
|
113
|
-
injectScriptTag(doc, { id, ...item }, scriptLoadOrErrorHandler);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
/* istanbul ignore next @preserve - this should be covered but the browser mode won't register this part */
|
|
119
|
-
const deleteScripts = () => {
|
|
120
|
-
for (const item of Object.values(lookup)) {
|
|
121
|
-
const scriptTag = doc.getElementById(item.id);
|
|
122
|
-
if (scriptTag != null && scriptTag.tagName === "SCRIPT") {
|
|
123
|
-
scriptTag.parentNode?.removeChild(scriptTag);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
lookup = {};
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
const getDocument = () => doc;
|
|
130
|
-
|
|
131
|
-
return {
|
|
132
|
-
loadScripts,
|
|
133
|
-
deleteScripts,
|
|
134
|
-
getDocument,
|
|
135
|
-
};
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
type DocumentScriptLoader = ReturnType<typeof createDocumentScriptLoader>;
|
|
139
|
-
|
|
140
|
-
const createScriptLoader = () => {
|
|
141
|
-
const cache: DocumentScriptLoader[] = [];
|
|
142
|
-
|
|
143
|
-
const getDocumentScriptLoader = (doc: Document) => {
|
|
144
|
-
let loader = cache.find((l) => l.getDocument() === doc);
|
|
145
|
-
if (loader === undefined) {
|
|
146
|
-
loader = createDocumentScriptLoader(doc);
|
|
147
|
-
cache.push(loader);
|
|
148
|
-
}
|
|
149
|
-
return loader;
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
const loadList = (
|
|
153
|
-
doc: Document,
|
|
154
|
-
items: ScriptItem[],
|
|
155
|
-
delay: number,
|
|
156
|
-
success: () => void,
|
|
157
|
-
failure?: (err: unknown) => void,
|
|
158
|
-
) => {
|
|
159
|
-
const doLoad = () =>
|
|
160
|
-
getDocumentScriptLoader(doc).loadScripts(items, success, failure);
|
|
161
|
-
/* istanbul ignore next @preserve - this should be covered but the browser mode won't register this part */
|
|
162
|
-
if (delay > 0) {
|
|
163
|
-
setTimeout(doLoad, delay);
|
|
164
|
-
} else {
|
|
165
|
-
doLoad();
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
/* istanbul ignore next @preserve - this should be covered but the browser mode won't register this part */
|
|
170
|
-
const reinitialize = () => {
|
|
171
|
-
for (let loader = cache.pop(); loader != null; loader = cache.pop()) {
|
|
172
|
-
loader.deleteScripts();
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
return {
|
|
177
|
-
loadList,
|
|
178
|
-
reinitialize,
|
|
179
|
-
};
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
export const ScriptLoader = createScriptLoader();
|
package/src/TinyMCE.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { type TinyMCE as TinyMCEGlobal } from "tinymce";
|
|
2
|
-
|
|
3
|
-
const getTinymce = (view: Window): TinyMCEGlobal | null => {
|
|
4
|
-
const global = view as unknown as { tinymce: TinyMCEGlobal | null };
|
|
5
|
-
|
|
6
|
-
return global && global.tinymce
|
|
7
|
-
? /* istanbul ignore next -- @preserve */ global.tinymce
|
|
8
|
-
: null;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export { getTinymce };
|
package/src/Utils.ts
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { eventPropTypes, IEventPropTypes } from "./components/EditorPropTypes";
|
|
2
|
-
import { IAllProps } from "./components/EditorPropTypes";
|
|
3
|
-
import { Editor as TinyMCEEditor, EditorEvent } from "tinymce";
|
|
4
|
-
|
|
5
|
-
export const isFunction = (x: unknown): x is typeof Function =>
|
|
6
|
-
typeof x === "function";
|
|
7
|
-
|
|
8
|
-
const isEventProp = (name: string): name is keyof IEventPropTypes =>
|
|
9
|
-
name in eventPropTypes;
|
|
10
|
-
|
|
11
|
-
const eventAttrToEventName = <T extends string>(attrName: `on${T}`): T =>
|
|
12
|
-
String(attrName.slice(2)) as T;
|
|
13
|
-
|
|
14
|
-
type PropLookup = <K extends keyof IAllProps>(key: K) => IAllProps[K];
|
|
15
|
-
|
|
16
|
-
export const configHandlers2 = <H>(
|
|
17
|
-
handlerLookup: PropLookup,
|
|
18
|
-
on: (name: string, handler: H) => void,
|
|
19
|
-
off: (name: string, handler: H) => void,
|
|
20
|
-
adapter: <K extends keyof IEventPropTypes>(lookup: PropLookup, key: K) => H,
|
|
21
|
-
prevProps: Partial<IAllProps>,
|
|
22
|
-
props: Partial<IAllProps>,
|
|
23
|
-
boundHandlers: Record<string, H>,
|
|
24
|
-
): void => {
|
|
25
|
-
const prevEventKeys = Object.keys(prevProps).filter(isEventProp);
|
|
26
|
-
const currEventKeys = Object.keys(props).filter(isEventProp);
|
|
27
|
-
|
|
28
|
-
/* istanbul ignore next @preserve - this should be covered but the browser mode won't register this part */
|
|
29
|
-
const removedKeys = prevEventKeys.filter((key) => props[key] === undefined);
|
|
30
|
-
const addedKeys = currEventKeys.filter((key) => prevProps[key] === undefined);
|
|
31
|
-
|
|
32
|
-
/* istanbul ignore next @preserve - this should be covered but the browser mode won't register this part */
|
|
33
|
-
removedKeys.forEach((key) => {
|
|
34
|
-
// remove event handler
|
|
35
|
-
const eventName = eventAttrToEventName(key);
|
|
36
|
-
const wrappedHandler = boundHandlers[eventName];
|
|
37
|
-
off(eventName, wrappedHandler);
|
|
38
|
-
delete boundHandlers[eventName];
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
addedKeys.forEach((key) => {
|
|
42
|
-
const wrappedHandler = adapter(handlerLookup, key);
|
|
43
|
-
const eventName = eventAttrToEventName(key);
|
|
44
|
-
boundHandlers[eventName] = wrappedHandler;
|
|
45
|
-
on(eventName, wrappedHandler);
|
|
46
|
-
});
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export const configHandlers = (
|
|
50
|
-
editor: TinyMCEEditor,
|
|
51
|
-
prevProps: Partial<IAllProps>,
|
|
52
|
-
props: Partial<IAllProps>,
|
|
53
|
-
boundHandlers: Record<string, (event: EditorEvent<unknown>) => unknown>,
|
|
54
|
-
lookup: PropLookup,
|
|
55
|
-
): void =>
|
|
56
|
-
configHandlers2(
|
|
57
|
-
lookup,
|
|
58
|
-
editor.on.bind(editor),
|
|
59
|
-
editor.off.bind(editor),
|
|
60
|
-
// @ts-expect-error: typescript cannot infer the type of the lookup function
|
|
61
|
-
(handlerLookup, key) => (e) => handlerLookup(key)?.(e, editor),
|
|
62
|
-
prevProps,
|
|
63
|
-
props,
|
|
64
|
-
boundHandlers,
|
|
65
|
-
);
|
|
66
|
-
|
|
67
|
-
let unique = 0;
|
|
68
|
-
|
|
69
|
-
export const uuid = (prefix: string): string => {
|
|
70
|
-
const time = Date.now();
|
|
71
|
-
const random = Math.floor(Math.random() * 1000000000);
|
|
72
|
-
|
|
73
|
-
unique++;
|
|
74
|
-
|
|
75
|
-
return prefix + "_" + random + unique + String(time);
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
export const isTextareaOrInput = (
|
|
79
|
-
element: Element | null,
|
|
80
|
-
): element is HTMLTextAreaElement | HTMLInputElement =>
|
|
81
|
-
element !== null &&
|
|
82
|
-
(element.tagName.toLowerCase() === "textarea" ||
|
|
83
|
-
element.tagName.toLowerCase() === "input");
|
|
84
|
-
|
|
85
|
-
const normalizePluginArray = (plugins?: string | string[]): string[] => {
|
|
86
|
-
if (typeof plugins === "undefined" || plugins === "") {
|
|
87
|
-
return [];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return Array.isArray(plugins) ? plugins : plugins.split(" ");
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
export const mergePlugins = (
|
|
94
|
-
initPlugins: string | string[] | undefined,
|
|
95
|
-
inputPlugins: string | string[] | undefined,
|
|
96
|
-
): string[] =>
|
|
97
|
-
normalizePluginArray(initPlugins).concat(normalizePluginArray(inputPlugins));
|
|
98
|
-
|
|
99
|
-
export const isBeforeInputEventAvailable = () =>
|
|
100
|
-
window.InputEvent &&
|
|
101
|
-
typeof InputEvent.prototype.getTargetRanges === "function";
|
|
102
|
-
|
|
103
|
-
export const isInDoc = (elem: Node) => {
|
|
104
|
-
/* istanbul ignore next @preserve - this here seems to be for legacy browsers, we ignore it */
|
|
105
|
-
if (!("isConnected" in Node.prototype)) {
|
|
106
|
-
// Fallback for IE and old Edge
|
|
107
|
-
let current = elem;
|
|
108
|
-
let parent = elem.parentNode;
|
|
109
|
-
while (parent != null) {
|
|
110
|
-
current = parent;
|
|
111
|
-
parent = current.parentNode;
|
|
112
|
-
}
|
|
113
|
-
return current === elem.ownerDocument;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return elem.isConnected;
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
export const setMode = (
|
|
120
|
-
editor: TinyMCEEditor | undefined,
|
|
121
|
-
mode: "readonly" | "design",
|
|
122
|
-
) => {
|
|
123
|
-
/* istanbul ignore else @preserve */
|
|
124
|
-
if (editor !== undefined) {
|
|
125
|
-
/* istanbul ignore else @preserve */
|
|
126
|
-
if (
|
|
127
|
-
editor.mode != null &&
|
|
128
|
-
typeof editor.mode === "object" &&
|
|
129
|
-
typeof editor.mode.set === "function"
|
|
130
|
-
) {
|
|
131
|
-
editor.mode.set(mode);
|
|
132
|
-
} else {
|
|
133
|
-
// support TinyMCE 4
|
|
134
|
-
// @ts-expect-error: no point in explaining this to IE
|
|
135
|
-
editor.setMode(mode);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
};
|