@fluxfiles/ckeditor5 1.0.2 → 1.0.4
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 +11 -7
- package/dist/index.d.ts +6 -5
- package/dist/index.js +48 -13
- package/package.json +20 -6
- package/src/convert.ts +44 -11
- package/src/fluxfilescommand.ts +21 -9
package/README.md
CHANGED
|
@@ -47,13 +47,13 @@ ClassicEditor.create(document.querySelector('#editor'), {
|
|
|
47
47
|
1. **Toolbar button** `fluxfiles` (command `fluxfiles`): opens the picker and inserts the
|
|
48
48
|
selection at the caret.
|
|
49
49
|
- Images become image elements with `src`, and `alt` from the file's alt text (file name if
|
|
50
|
-
none).
|
|
51
|
-
token) and the image plugin allows a `srcset` attribute, it is added too.
|
|
50
|
+
none). `img_srcset` is never inserted: its `/api/fm/img?token=` URLs expire.
|
|
52
51
|
- Other files become links with the metadata title (or file name) as text.
|
|
53
52
|
- With `multiple: true`, every selected file is inserted in order. Folders are skipped.
|
|
54
53
|
- Without the image plugin, images are inserted as links; without the link plugin, as plain text.
|
|
55
54
|
2. **Image insert dropdown**: when `ImageInsert` is loaded, the image toolbar's insert dropdown
|
|
56
|
-
offers FluxFiles next to "Insert image via URL". Turn it off with `imageInsert: false`.
|
|
55
|
+
offers FluxFiles next to "Insert image via URL". Turn it off with `imageInsert: false`. If you set
|
|
56
|
+
your own `image.insert.integrations`, add `'fluxfiles'` to it.
|
|
57
57
|
|
|
58
58
|
The main JWT is never put in a URL: the SDK hands it to the picker iframe with `postMessage`
|
|
59
59
|
and checks the iframe's origin on every message.
|
|
@@ -63,9 +63,6 @@ and checks the iframe's origin on every message.
|
|
|
63
63
|
> Use a **public** disk or a `public_url`. A preview-only selection (no embeddable URL, e.g. a
|
|
64
64
|
> watermark overlay with `allow_download=false`) is refused, as in the other editor plugins.
|
|
65
65
|
|
|
66
|
-
> CKEditor 5's image model has no `sizes` attribute and writes `sizes="100vw"` itself, so
|
|
67
|
-
> `img_sizes` is only used by a custom image plugin that registers one.
|
|
68
|
-
|
|
69
66
|
## Configuration (`config.fluxfiles`)
|
|
70
67
|
|
|
71
68
|
| Option | Type | Default | Description |
|
|
@@ -78,13 +75,20 @@ and checks the iframe's origin on every message.
|
|
|
78
75
|
| `locale` | string | | UI language code |
|
|
79
76
|
| `theme` | string | | `light` / `dark` / `auto` |
|
|
80
77
|
| `multiple` | boolean | `false` | Allow multi-file selection |
|
|
81
|
-
| `allowedTypes` | string[] | |
|
|
78
|
+
| `allowedTypes` | string[] | | Extensions (`.pdf` or `pdf`) or MIME types / patterns (`image/*`). Picker mode only: non-matching files are hidden and cannot be selected. UI convenience, not a security boundary (the token's `allowed_ext` claim is the server-side check) |
|
|
82
79
|
| `maxUploadMb` | number | | Max size per uploaded file, MB |
|
|
83
80
|
| `maxFiles` | number | | Max files per upload batch |
|
|
84
81
|
| `onTokenRefresh` | (ctx) => Promise\<string \| null\> | | Called when the picker gets a 401; return a fresh JWT |
|
|
85
82
|
| `imageInsert` | boolean | `true` | Offer FluxFiles in the image insert dropdown |
|
|
86
83
|
| `onInsert` | (files) => void | | Called after files were inserted |
|
|
87
84
|
|
|
85
|
+
**Files with no permanent URL.** Two cases:
|
|
86
|
+
|
|
87
|
+
- On a gated local disk (`FLUXFILES_LOCAL_PRIVATE`) or an SFTP disk without `public_url`, `permanent_url` is `null` and the only link is an expiring `/api/fm/img|stream?token=` URL. The plugin never saves that into content: it shows one notification (the Notification plugin, else `alert`) listing the refused files. Use a public disk or set `public_url`.
|
|
88
|
+
- A private bucket without `public_url` yields a presigned URL (up to 24h). That one **is** still inserted, with a `console.warn`, and it stops working when it expires.
|
|
89
|
+
|
|
90
|
+
Only `http(s)` URLs are ever inserted.
|
|
91
|
+
|
|
88
92
|
## API
|
|
89
93
|
|
|
90
94
|
- Plugin: `FluxFiles` (`pluginName: 'FluxFiles'`), named and default export
|
package/dist/index.d.ts
CHANGED
|
@@ -28,15 +28,13 @@ interface FileInfo {
|
|
|
28
28
|
title: string;
|
|
29
29
|
width: number | null;
|
|
30
30
|
height: number | null;
|
|
31
|
-
srcset: string;
|
|
32
|
-
sizes: string;
|
|
33
31
|
}
|
|
34
32
|
/**
|
|
35
33
|
* Normalise a FluxFiles `onSelect` entry. Same rules as the CKEditor 4 / TinyMCE
|
|
36
34
|
* plugins: prefer `permanent_url`, refuse a preview-only selection (its only view
|
|
37
|
-
* is a short-lived token URL), warn about presigned (expiring) URLs.
|
|
35
|
+
* is a short-lived token URL, so `img_srcset` is never inserted either), warn about presigned (expiring) URLs.
|
|
38
36
|
*/
|
|
39
|
-
declare function fileInfo(file: FluxFile): FileInfo;
|
|
37
|
+
declare function fileInfo(file: FluxFile, endpoint?: string, notify?: (message: string) => void): FileInfo;
|
|
40
38
|
/** `onSelect` hands over one file, an array, or folders; keep only real files. */
|
|
41
39
|
declare function selectedFiles(payload: FluxFile | FluxFile[] | null | undefined): FluxFile[];
|
|
42
40
|
|
|
@@ -92,6 +90,8 @@ declare class FluxFilesCommand extends Command {
|
|
|
92
90
|
refresh(): void;
|
|
93
91
|
/** Resolves once the picker has been opened (not when a file is chosen). */
|
|
94
92
|
execute(): Promise<void>;
|
|
93
|
+
/** Show a refusal to the editor user (Notification plugin when loaded, else alert). */
|
|
94
|
+
private _notify;
|
|
95
95
|
private _insert;
|
|
96
96
|
/**
|
|
97
97
|
* A freshly inserted image stays selected, so the next insert would replace it.
|
|
@@ -107,4 +107,5 @@ declare module '@ckeditor/ckeditor5-core' {
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
export {
|
|
110
|
+
export { FluxFiles, FluxFilesCommand, FluxFiles as default, fileInfo, selectedFiles };
|
|
111
|
+
export type { FileInfo, FluxFilesConfig };
|
package/dist/index.js
CHANGED
|
@@ -7,15 +7,41 @@ import FluxFilesSdk from "fluxfiles";
|
|
|
7
7
|
|
|
8
8
|
// src/convert.ts
|
|
9
9
|
var IMAGE_EXT = /\.(jpe?g|png|gif|webp|svg|bmp|ico|avif)$/i;
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
var TOKEN_URL = /\/(img|stream)(\.php)?\?([^#]*&)?token=/;
|
|
11
|
+
function absUrl(u, endpoint) {
|
|
12
|
+
const t = String(u || "").replace(/^[\u0000-\u0020]+/, "");
|
|
13
|
+
if (t === "") return "";
|
|
14
|
+
let r;
|
|
15
|
+
try {
|
|
16
|
+
r = new URL(t, endpoint || window.location.href);
|
|
17
|
+
} catch {
|
|
18
|
+
try {
|
|
19
|
+
r = new URL(t, window.location.href);
|
|
20
|
+
} catch {
|
|
21
|
+
return "";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return r.protocol === "http:" || r.protocol === "https:" ? r.href : "";
|
|
25
|
+
}
|
|
26
|
+
function refusalMessage(reason, name) {
|
|
27
|
+
if (reason === "token") {
|
|
28
|
+
return '"' + name + '" is only available through an expiring link (private disk without a public URL), so it was not inserted. Use a public disk or set public_url.';
|
|
29
|
+
}
|
|
30
|
+
if (reason === "unsafe") return '"' + name + '" has a URL that is not http(s), so it was not inserted.';
|
|
31
|
+
return '"' + name + '" is preview-only (no embeddable URL, e.g. a watermark overlay with allow_download=false), so it was not inserted. Burn in the watermark (Watermark editor, Apply) or issue a downloadable token to embed it.';
|
|
32
|
+
}
|
|
33
|
+
function fileInfo(file, endpoint = "", notify) {
|
|
34
|
+
const raw = file.permanent_url || file.url || "";
|
|
35
|
+
let url = absUrl(raw, endpoint);
|
|
12
36
|
const name = file.name || file.basename || file.path || "";
|
|
13
37
|
const meta = file.meta || {};
|
|
38
|
+
const reason = raw === "" ? "preview" : url === "" ? "unsafe" : TOKEN_URL.test(url) ? "token" : "";
|
|
39
|
+
if (reason) url = "";
|
|
14
40
|
const embeddable = url !== "";
|
|
15
41
|
if (!embeddable) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
);
|
|
42
|
+
const msg = refusalMessage(reason, name);
|
|
43
|
+
console.warn("[FluxFiles] " + msg);
|
|
44
|
+
notify?.(msg);
|
|
19
45
|
} else if (/[?&](X-Amz-|Signature=)/.test(url)) {
|
|
20
46
|
console.warn("[FluxFiles] Inserting a presigned (expiring) URL, it will break when the URL expires. Use a public disk or public_url for embeds.");
|
|
21
47
|
}
|
|
@@ -28,10 +54,7 @@ function fileInfo(file) {
|
|
|
28
54
|
alt: meta.alt_text || name,
|
|
29
55
|
title: meta.title || name,
|
|
30
56
|
width: file.width || null,
|
|
31
|
-
height: file.height || null
|
|
32
|
-
// Only a permanent URL set can carry a srcset; img_srcset is built from img_base.
|
|
33
|
-
srcset: isImage ? file.img_srcset || "" : "",
|
|
34
|
-
sizes: isImage ? file.img_sizes || "" : ""
|
|
57
|
+
height: file.height || null
|
|
35
58
|
};
|
|
36
59
|
}
|
|
37
60
|
function selectedFiles(payload) {
|
|
@@ -77,18 +100,29 @@ var FluxFilesCommand = class extends Command {
|
|
|
77
100
|
if (cfg.onTokenRefresh) options.onTokenRefresh = cfg.onTokenRefresh;
|
|
78
101
|
sdk().open(options);
|
|
79
102
|
}
|
|
103
|
+
/** Show a refusal to the editor user (Notification plugin when loaded, else alert). */
|
|
104
|
+
_notify(messages) {
|
|
105
|
+
if (!messages.length) return;
|
|
106
|
+
const lines = messages.slice(0, 5);
|
|
107
|
+
if (messages.length > 5) lines.push("\u2026 and " + (messages.length - 5) + " more");
|
|
108
|
+
const message = lines.join("\n");
|
|
109
|
+
const { plugins } = this.editor;
|
|
110
|
+
if (plugins.has("Notification")) {
|
|
111
|
+
plugins.get("Notification").showWarning(message, { namespace: "fluxfiles" });
|
|
112
|
+
} else if (typeof window.alert === "function") {
|
|
113
|
+
window.alert(message);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
80
116
|
_insert(files) {
|
|
81
117
|
const { editor } = this;
|
|
82
118
|
const schema = editor.model.schema;
|
|
83
119
|
const inserted = [];
|
|
120
|
+
const refused = [];
|
|
84
121
|
for (const file of files) {
|
|
85
|
-
const info = fileInfo(file);
|
|
122
|
+
const info = fileInfo(file, this._cfg.endpoint, (m) => refused.push(m));
|
|
86
123
|
if (!info.embeddable) continue;
|
|
87
124
|
if (info.isImage && editor.commands.get("insertImage")) {
|
|
88
125
|
const source = { src: info.url, alt: info.alt };
|
|
89
|
-
const type = schema.isRegistered("imageBlock") ? "imageBlock" : "imageInline";
|
|
90
|
-
if (info.srcset && schema.checkAttribute(type, "srcset")) source.srcset = info.srcset;
|
|
91
|
-
if (info.sizes && schema.checkAttribute(type, "sizes")) source.sizes = info.sizes;
|
|
92
126
|
editor.execute("insertImage", { source: [source] });
|
|
93
127
|
this._leaveImage();
|
|
94
128
|
} else {
|
|
@@ -101,6 +135,7 @@ var FluxFilesCommand = class extends Command {
|
|
|
101
135
|
}
|
|
102
136
|
inserted.push(file);
|
|
103
137
|
}
|
|
138
|
+
this._notify(refused);
|
|
104
139
|
if (inserted.length) {
|
|
105
140
|
editor.editing.view.focus();
|
|
106
141
|
this._cfg.onInsert?.(inserted);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fluxfiles/ckeditor5",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "CKEditor 5 plugin for FluxFiles",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -35,11 +35,13 @@
|
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"ckeditor5": "^49.1.0",
|
|
37
37
|
"jsdom": "^25.0.1",
|
|
38
|
-
"tsup": "^
|
|
38
|
+
"tsup": "^9.0.0",
|
|
39
39
|
"typescript": "^5.0.0",
|
|
40
|
-
"vitest": "^
|
|
40
|
+
"vitest": "^4.1.12"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
41
44
|
},
|
|
42
|
-
"publishConfig": { "access": "public" },
|
|
43
45
|
"author": "thai-pc",
|
|
44
46
|
"homepage": "https://github.com/thai-pc/fluxfiles#framework-adapters",
|
|
45
47
|
"repository": {
|
|
@@ -47,6 +49,18 @@
|
|
|
47
49
|
"url": "git+https://github.com/thai-pc/fluxfiles.git",
|
|
48
50
|
"directory": "packages/ckeditor5"
|
|
49
51
|
},
|
|
50
|
-
"bugs": {
|
|
51
|
-
|
|
52
|
+
"bugs": {
|
|
53
|
+
"url": "https://github.com/thai-pc/fluxfiles/issues"
|
|
54
|
+
},
|
|
55
|
+
"keywords": [
|
|
56
|
+
"fluxfiles",
|
|
57
|
+
"file-manager",
|
|
58
|
+
"ckeditor",
|
|
59
|
+
"ckeditor5",
|
|
60
|
+
"plugin",
|
|
61
|
+
"s3",
|
|
62
|
+
"r2",
|
|
63
|
+
"upload",
|
|
64
|
+
"media"
|
|
65
|
+
]
|
|
52
66
|
}
|
package/src/convert.ts
CHANGED
|
@@ -11,26 +11,62 @@ export interface FileInfo {
|
|
|
11
11
|
title: string;
|
|
12
12
|
width: number | null;
|
|
13
13
|
height: number | null;
|
|
14
|
-
srcset: string;
|
|
15
|
-
sizes: string;
|
|
16
14
|
}
|
|
17
15
|
|
|
18
16
|
const IMAGE_EXT = /\.(jpe?g|png|gif|webp|svg|bmp|ico|avif)$/i;
|
|
17
|
+
const TOKEN_URL = /\/(img|stream)(\.php)?\?([^#]*&)?token=/;
|
|
18
|
+
|
|
19
|
+
function absUrl(u: string, endpoint: string): string {
|
|
20
|
+
// Leading whitespace/control chars are ignored by browsers: strip them before
|
|
21
|
+
// the scheme check so " javascript:..." cannot slip through.
|
|
22
|
+
// eslint-disable-next-line no-control-regex
|
|
23
|
+
const t = String(u || '').replace(/^[\u0000-\u0020]+/, '');
|
|
24
|
+
if (t === '') return '';
|
|
25
|
+
let r: URL;
|
|
26
|
+
try {
|
|
27
|
+
r = new URL(t, endpoint || window.location.href);
|
|
28
|
+
} catch {
|
|
29
|
+
try {
|
|
30
|
+
r = new URL(t, window.location.href);
|
|
31
|
+
} catch {
|
|
32
|
+
return '';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// Only http(s) may reach saved content (drops javascript:, data:, vbscript:, ...).
|
|
36
|
+
return r.protocol === 'http:' || r.protocol === 'https:' ? r.href : '';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Why a selection cannot be inserted; `''` when it can. */
|
|
40
|
+
export type Refusal = '' | 'preview' | 'unsafe' | 'token';
|
|
41
|
+
|
|
42
|
+
export function refusalMessage(reason: Refusal, name: string): string {
|
|
43
|
+
if (reason === 'token') {
|
|
44
|
+
return '"' + name + '" is only available through an expiring link (private disk without a public URL), so it was not inserted. Use a public disk or set public_url.';
|
|
45
|
+
}
|
|
46
|
+
if (reason === 'unsafe') return '"' + name + '" has a URL that is not http(s), so it was not inserted.';
|
|
47
|
+
return '"' + name + '" is preview-only (no embeddable URL, e.g. a watermark overlay with allow_download=false), so it was not inserted. Burn in the watermark (Watermark editor, Apply) or issue a downloadable token to embed it.';
|
|
48
|
+
}
|
|
19
49
|
|
|
20
50
|
/**
|
|
21
51
|
* Normalise a FluxFiles `onSelect` entry. Same rules as the CKEditor 4 / TinyMCE
|
|
22
52
|
* plugins: prefer `permanent_url`, refuse a preview-only selection (its only view
|
|
23
|
-
* is a short-lived token URL), warn about presigned (expiring) URLs.
|
|
53
|
+
* is a short-lived token URL, so `img_srcset` is never inserted either), warn about presigned (expiring) URLs.
|
|
24
54
|
*/
|
|
25
|
-
export function fileInfo(file: FluxFile): FileInfo {
|
|
26
|
-
|
|
55
|
+
export function fileInfo(file: FluxFile, endpoint = '', notify?: (message: string) => void): FileInfo {
|
|
56
|
+
// Absolute and permanent only: an older core returns a root-relative url that
|
|
57
|
+
// would resolve against the HOST page, and an /img|stream ?token= URL expires
|
|
58
|
+
// (and would persist a short-lived token into saved content).
|
|
59
|
+
const raw = file.permanent_url || file.url || '';
|
|
60
|
+
let url = absUrl(raw, endpoint);
|
|
27
61
|
const name = file.name || file.basename || file.path || '';
|
|
28
62
|
const meta = file.meta || {};
|
|
63
|
+
const reason: Refusal = raw === '' ? 'preview' : url === '' ? 'unsafe' : TOKEN_URL.test(url) ? 'token' : '';
|
|
64
|
+
if (reason) url = '';
|
|
29
65
|
const embeddable = url !== '';
|
|
30
66
|
if (!embeddable) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
);
|
|
67
|
+
const msg = refusalMessage(reason, name);
|
|
68
|
+
console.warn('[FluxFiles] ' + msg);
|
|
69
|
+
notify?.(msg);
|
|
34
70
|
} else if (/[?&](X-Amz-|Signature=)/.test(url)) {
|
|
35
71
|
console.warn('[FluxFiles] Inserting a presigned (expiring) URL, it will break when the URL expires. Use a public disk or public_url for embeds.');
|
|
36
72
|
}
|
|
@@ -44,9 +80,6 @@ export function fileInfo(file: FluxFile): FileInfo {
|
|
|
44
80
|
title: meta.title || name,
|
|
45
81
|
width: file.width || null,
|
|
46
82
|
height: file.height || null,
|
|
47
|
-
// Only a permanent URL set can carry a srcset; img_srcset is built from img_base.
|
|
48
|
-
srcset: isImage ? file.img_srcset || '' : '',
|
|
49
|
-
sizes: isImage ? file.img_sizes || '' : '',
|
|
50
83
|
};
|
|
51
84
|
}
|
|
52
85
|
|
package/src/fluxfilescommand.ts
CHANGED
|
@@ -55,24 +55,34 @@ export class FluxFilesCommand extends Command {
|
|
|
55
55
|
sdk().open(options);
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
/** Show a refusal to the editor user (Notification plugin when loaded, else alert). */
|
|
59
|
+
private _notify(messages: string[]): void {
|
|
60
|
+
if (!messages.length) return;
|
|
61
|
+
// One combined message per selection (at most 5 lines). The Notification
|
|
62
|
+
// plugin shows plain text, so file names need no escaping.
|
|
63
|
+
const lines = messages.slice(0, 5);
|
|
64
|
+
if (messages.length > 5) lines.push('… and ' + (messages.length - 5) + ' more');
|
|
65
|
+
const message = lines.join('\n');
|
|
66
|
+
const { plugins } = this.editor;
|
|
67
|
+
if (plugins.has('Notification')) {
|
|
68
|
+
(plugins.get('Notification') as { showWarning(m: string, o?: object): void }).showWarning(message, { namespace: 'fluxfiles' });
|
|
69
|
+
} else if (typeof window.alert === 'function') {
|
|
70
|
+
window.alert(message);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
58
74
|
private _insert(files: FluxFile[]): void {
|
|
59
75
|
const { editor } = this;
|
|
60
76
|
const schema = editor.model.schema;
|
|
61
77
|
const inserted: FluxFile[] = [];
|
|
78
|
+
const refused: string[] = [];
|
|
62
79
|
|
|
63
80
|
for (const file of files) {
|
|
64
|
-
const info = fileInfo(file);
|
|
81
|
+
const info = fileInfo(file, this._cfg.endpoint, (m) => refused.push(m));
|
|
65
82
|
if (!info.embeddable) continue; // preview-only: already warned
|
|
66
83
|
|
|
67
84
|
if (info.isImage && editor.commands.get('insertImage')) {
|
|
68
|
-
const source
|
|
69
|
-
// Responsive attributes only when the image plugin's schema knows them
|
|
70
|
-
// (core ImageEditing registers `srcset` as a plain string; it has no `sizes`
|
|
71
|
-
// model attribute and always writes sizes="100vw", so img_sizes is only
|
|
72
|
-
// honoured by a custom image plugin that registers one).
|
|
73
|
-
const type = schema.isRegistered('imageBlock') ? 'imageBlock' : 'imageInline';
|
|
74
|
-
if (info.srcset && schema.checkAttribute(type, 'srcset')) source.srcset = info.srcset;
|
|
75
|
-
if (info.sizes && schema.checkAttribute(type, 'sizes')) source.sizes = info.sizes;
|
|
85
|
+
const source = { src: info.url, alt: info.alt };
|
|
76
86
|
editor.execute('insertImage', { source: [source] });
|
|
77
87
|
this._leaveImage();
|
|
78
88
|
} else {
|
|
@@ -87,6 +97,8 @@ export class FluxFilesCommand extends Command {
|
|
|
87
97
|
inserted.push(file);
|
|
88
98
|
}
|
|
89
99
|
|
|
100
|
+
this._notify(refused);
|
|
101
|
+
|
|
90
102
|
if (inserted.length) {
|
|
91
103
|
editor.editing.view.focus();
|
|
92
104
|
this._cfg.onInsert?.(inserted);
|