@fluxfiles/ckeditor5 1.0.1 → 1.0.3

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
@@ -78,13 +78,20 @@ and checks the iframe's origin on every message.
78
78
  | `locale` | string | | UI language code |
79
79
  | `theme` | string | | `light` / `dark` / `auto` |
80
80
  | `multiple` | boolean | `false` | Allow multi-file selection |
81
- | `allowedTypes` | string[] | | Restrict selectable types |
81
+ | `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
82
  | `maxUploadMb` | number | | Max size per uploaded file, MB |
83
83
  | `maxFiles` | number | | Max files per upload batch |
84
84
  | `onTokenRefresh` | (ctx) => Promise\<string \| null\> | | Called when the picker gets a 401; return a fresh JWT |
85
85
  | `imageInsert` | boolean | `true` | Offer FluxFiles in the image insert dropdown |
86
86
  | `onInsert` | (files) => void | | Called after files were inserted |
87
87
 
88
+ **Files with no permanent URL.** Two cases:
89
+
90
+ - 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`.
91
+ - 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.
92
+
93
+ Only `http(s)` URLs are ever inserted.
94
+
88
95
  ## API
89
96
 
90
97
  - 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.
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
- function fileInfo(file) {
11
- const url = file.permanent_url || file.url || "";
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
- console.warn(
17
- '[FluxFiles] "' + 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.'
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);
@@ -128,7 +163,7 @@ var FluxFilesCommand = class extends Command {
128
163
  };
129
164
 
130
165
  // src/fluxfiles.ts
131
- var ICON = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none"><path d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M10 16.5v-6h4.5M10 13.5h3.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
166
+ var ICON = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none"><path d="M3 5a2 2 0 012-2h5l2 2h7a2 2 0 012 2v12a2 2 0 01-2 2H5a2 2 0 01-2-2V5z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M10 16.5v-7h4.5M10 13h3.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
132
167
  var FluxFiles = class extends Plugin {
133
168
  static get pluginName() {
134
169
  return "FluxFiles";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluxfiles/ckeditor5",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "CKEditor 5 plugin for FluxFiles",
5
5
  "license": "MIT",
6
6
  "type": "module",
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
- const url = file.permanent_url || file.url || '';
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
- console.warn(
32
- '[FluxFiles] "' + 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.'
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/fluxfiles.ts CHANGED
@@ -3,13 +3,14 @@ import type { Locale } from 'ckeditor5';
3
3
  import type { FluxFilesConfig } from './config';
4
4
  import { FluxFilesCommand } from './fluxfilescommand';
5
5
 
6
- // The same F-folder glyph the TinyMCE and CKEditor 4 plugins use. currentColor
6
+ // The FluxFiles logo's square folder silhouette with an "F", as a line icon, identical
7
+ // in the TinyMCE, CKEditor 4 and Summernote plugins. currentColor
7
8
  // lets CKEditor 5 recolour it for hover / disabled / dark themes.
8
9
  const ICON =
9
10
  '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none">' +
10
- '<path d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z" ' +
11
+ '<path d="M3 5a2 2 0 012-2h5l2 2h7a2 2 0 012 2v12a2 2 0 01-2 2H5a2 2 0 01-2-2V5z" ' +
11
12
  'stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>' +
12
- '<path d="M10 16.5v-6h4.5M10 13.5h3.5" ' +
13
+ '<path d="M10 16.5v-7h4.5M10 13h3.5" ' +
13
14
  'stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
14
15
 
15
16
  /** Minimal shape of the optional ImageInsertUI plugin (CKEditor's image package). */
@@ -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: Record<string, unknown> = { src: info.url, alt: info.alt };
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);