@griddo/ax 12.15.0-astro-rc.0 → 12.15.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/package.json +4 -4
- package/src/__tests__/components/Fields/Wysiwyg/Wysiwyg.caption.test.tsx +328 -39
- package/src/__tests__/components/Fields/Wysiwyg/Wysiwyg.test.tsx +1 -0
- package/src/__tests__/modules/GlobalSettings/Llms/Llms.uc.test.tsx +2861 -0
- package/src/api/domains.tsx +98 -2
- package/src/components/Fields/Wysiwyg/helpers.tsx +188 -0
- package/src/components/Fields/Wysiwyg/index.tsx +75 -39
- package/src/components/Fields/Wysiwyg/style.tsx +5 -0
- package/src/modules/GlobalSettings/Llms/AutomaticSelection/index.tsx +317 -0
- package/src/modules/GlobalSettings/Llms/DomainList/index.tsx +65 -0
- package/src/modules/GlobalSettings/Llms/IndexModelEditor/index.tsx +325 -0
- package/src/modules/GlobalSettings/Llms/MirrorExample/index.tsx +61 -0
- package/src/modules/GlobalSettings/Llms/MirrorOptions/index.tsx +82 -0
- package/src/modules/GlobalSettings/Llms/Outline/index.tsx +183 -0
- package/src/modules/GlobalSettings/Llms/PagePicker/index.tsx +105 -0
- package/src/modules/GlobalSettings/Llms/Preview/index.tsx +65 -0
- package/src/modules/GlobalSettings/Llms/ResolvedPages/index.tsx +191 -0
- package/src/modules/GlobalSettings/Llms/SectionEditor/index.tsx +262 -0
- package/src/modules/GlobalSettings/Llms/atoms.tsx +37 -0
- package/src/modules/GlobalSettings/Llms/index.tsx +383 -0
- package/src/modules/GlobalSettings/Llms/style.tsx +541 -0
- package/src/modules/GlobalSettings/Llms/utils.tsx +307 -0
- package/src/modules/GlobalSettings/index.tsx +7 -0
- package/src/routes/multisite.tsx +8 -1
- package/src/schemas/pages/GlobalPage.ts +2 -2
- package/src/schemas/pages/Page.ts +2 -2
- package/src/types/index.tsx +80 -12
- package/src/types/llms.tsx +315 -0
package/src/api/domains.tsx
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { IDomainRobot } from "@ax/types";
|
|
1
|
+
import type { IDomainRobot, LlmsPagesQuery, LlmsSectionPagesQuery, LlmsSettings } from "@ax/types";
|
|
2
|
+
|
|
2
3
|
import { template } from "./config";
|
|
3
4
|
import { type IServiceConfig, sendRequest } from "./utils";
|
|
4
5
|
|
|
@@ -13,8 +14,48 @@ const SERVICES: { [key: string]: IServiceConfig } = {
|
|
|
13
14
|
endpoint: "/domains/robots/bulk",
|
|
14
15
|
method: "PUT",
|
|
15
16
|
},
|
|
17
|
+
GET_DOMAINS: {
|
|
18
|
+
...template,
|
|
19
|
+
endpoint: "/domains",
|
|
20
|
+
method: "GET",
|
|
21
|
+
},
|
|
22
|
+
GET_LLMS_SETTINGS: {
|
|
23
|
+
...template,
|
|
24
|
+
endpoint: "/domains/",
|
|
25
|
+
method: "GET",
|
|
26
|
+
},
|
|
27
|
+
UPDATE_LLMS_SETTINGS: {
|
|
28
|
+
...template,
|
|
29
|
+
endpoint: "/domains/",
|
|
30
|
+
method: "PUT",
|
|
31
|
+
},
|
|
32
|
+
GET_LLMS_PAGES: {
|
|
33
|
+
...template,
|
|
34
|
+
endpoint: "/domains/",
|
|
35
|
+
method: "GET",
|
|
36
|
+
},
|
|
37
|
+
RESOLVE_LLMS_INDEX: {
|
|
38
|
+
...template,
|
|
39
|
+
endpoint: "/domains/",
|
|
40
|
+
method: "POST",
|
|
41
|
+
},
|
|
42
|
+
LIST_LLMS_SECTION_PAGES: {
|
|
43
|
+
...template,
|
|
44
|
+
endpoint: "/domains/",
|
|
45
|
+
method: "POST",
|
|
46
|
+
},
|
|
16
47
|
};
|
|
17
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Una copia del servicio con su URL completa. No se escribe en `SERVICES`, como
|
|
51
|
+
* hacen otros módulos, porque la pantalla del llms.txt lanza varias peticiones a
|
|
52
|
+
* la vez y se pisarían el `dynamicUrl`.
|
|
53
|
+
*/
|
|
54
|
+
const withPath = (service: IServiceConfig, path: string): IServiceConfig => ({
|
|
55
|
+
...service,
|
|
56
|
+
dynamicUrl: `${service.host}${service.endpoint}${path}`,
|
|
57
|
+
});
|
|
58
|
+
|
|
18
59
|
const getDomainsRobots = async () => {
|
|
19
60
|
return sendRequest(SERVICES.GET_ROBOTS);
|
|
20
61
|
};
|
|
@@ -23,4 +64,59 @@ const updateDomainsRobots = async (robots: IDomainRobot[]) => {
|
|
|
23
64
|
return sendRequest(SERVICES.UPDATE_ROBOTS, { robots });
|
|
24
65
|
};
|
|
25
66
|
|
|
26
|
-
|
|
67
|
+
/** Los dominios de la instancia, para elegir de cuál se modelan los ajustes LLM. */
|
|
68
|
+
const getDomains = async () => {
|
|
69
|
+
return sendRequest(SERVICES.GET_DOMAINS);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** Los ajustes LLM guardados del dominio, o los de Griddo si nunca ha guardado. */
|
|
73
|
+
const getLlmsSettings = async (domainId: number) => {
|
|
74
|
+
return sendRequest(withPath(SERVICES.GET_LLMS_SETTINGS, `${domainId}/llms-settings`));
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** Guarda el documento entero: la API sustituye el guardado, sin fusionarlo. */
|
|
78
|
+
const updateLlmsSettings = async (domainId: number, settings: LlmsSettings) => {
|
|
79
|
+
return sendRequest(withPath(SERVICES.UPDATE_LLMS_SETTINGS, `${domainId}/llms-settings`), { ...settings });
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** Las páginas del dominio con su elegibilidad, acotadas al texto y a los ids pedidos. */
|
|
83
|
+
const getLlmsPages = async (domainId: number, query: LlmsPagesQuery) => {
|
|
84
|
+
const params = new URLSearchParams();
|
|
85
|
+
if (query.search) params.set("search", query.search);
|
|
86
|
+
if (query.ids?.length) params.set("ids", query.ids.join(","));
|
|
87
|
+
if (query.page) params.set("page", String(query.page));
|
|
88
|
+
if (query.itemsPerPage) params.set("itemsPerPage", String(query.itemsPerPage));
|
|
89
|
+
const queryString = params.toString();
|
|
90
|
+
|
|
91
|
+
return sendRequest(
|
|
92
|
+
withPath(SERVICES.GET_LLMS_PAGES, `${domainId}/llms-pages${queryString ? `?${queryString}` : ""}`),
|
|
93
|
+
);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* El `llms.txt` de cada prefijo de URL del dominio, resuelto con los ajustes que
|
|
98
|
+
* se mandan —los que el editor tiene en pantalla, guardados o no—. No guarda nada.
|
|
99
|
+
*/
|
|
100
|
+
const resolveLlmsIndex = async (domainId: number, settings: LlmsSettings) => {
|
|
101
|
+
return sendRequest(withPath(SERVICES.RESOLVE_LLMS_INDEX, `${domainId}/llms-index`), { ...settings });
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Un tramo de las páginas que coge una sección del `llms.txt` del dominio, con cuántas
|
|
106
|
+
* coge en total y los sites que cuentan, resuelto con los ajustes que se mandan —los que
|
|
107
|
+
* el editor tiene en pantalla, guardados o no—. No guarda nada.
|
|
108
|
+
*/
|
|
109
|
+
const listLlmsSectionPages = async (domainId: number, query: LlmsSectionPagesQuery) => {
|
|
110
|
+
return sendRequest(withPath(SERVICES.LIST_LLMS_SECTION_PAGES, `${domainId}/llms-section-pages`), { ...query });
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export default {
|
|
114
|
+
getDomainsRobots,
|
|
115
|
+
updateDomainsRobots,
|
|
116
|
+
getDomains,
|
|
117
|
+
getLlmsSettings,
|
|
118
|
+
updateLlmsSettings,
|
|
119
|
+
getLlmsPages,
|
|
120
|
+
resolveLlmsIndex,
|
|
121
|
+
listLlmsSectionPages,
|
|
122
|
+
};
|
|
@@ -152,13 +152,201 @@ const getImagePercentWidth = (editor: any, $img: any) => {
|
|
|
152
152
|
return percent > 0 && percent < 100 ? `${percent}%` : "100%";
|
|
153
153
|
};
|
|
154
154
|
|
|
155
|
+
// Nothing but blank text, Froala's invisible spaces and its selection markers.
|
|
156
|
+
const isBlank = (fragment: DocumentFragment) => {
|
|
157
|
+
for (const marker of fragment.querySelectorAll(".fr-marker")) marker.remove();
|
|
158
|
+
return !fragment.textContent?.replace(/\u200B/g, "").trim() && !fragment.querySelector("img, iframe, video");
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// Moves what `paragraph` holds before `first` and after `last` out to
|
|
162
|
+
// paragraphs of their own, around `anchor`, unless it is blank.
|
|
163
|
+
const splitAround = (paragraph: HTMLElement, first: Node, last: Node, anchor: Element) => {
|
|
164
|
+
const before = document.createRange();
|
|
165
|
+
before.setStart(paragraph, 0);
|
|
166
|
+
before.setEndBefore(first);
|
|
167
|
+
const after = document.createRange();
|
|
168
|
+
after.setStartAfter(last);
|
|
169
|
+
after.setEnd(paragraph, paragraph.childNodes.length);
|
|
170
|
+
for (const [range, side] of [
|
|
171
|
+
[before, "before"],
|
|
172
|
+
[after, "after"],
|
|
173
|
+
] as const) {
|
|
174
|
+
if (isBlank(range.cloneContents())) continue;
|
|
175
|
+
const part = paragraph.cloneNode(false) as HTMLElement;
|
|
176
|
+
part.append(range.extractContents());
|
|
177
|
+
anchor[side](part);
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// The outermost element around `img`, inside `paragraph`, with nothing but
|
|
182
|
+
// blank on that `side` of it: splitting the paragraph there leaves no empty
|
|
183
|
+
// copy of a link or a bold on that side.
|
|
184
|
+
const edgeAround = (img: Element, paragraph: Element, side: "before" | "after") => {
|
|
185
|
+
let edge = img;
|
|
186
|
+
while (edge.parentElement && edge.parentElement !== paragraph) {
|
|
187
|
+
const rest = document.createRange();
|
|
188
|
+
rest.selectNodeContents(edge.parentElement);
|
|
189
|
+
if (side === "before") rest.setEndBefore(edge);
|
|
190
|
+
else rest.setStartAfter(edge);
|
|
191
|
+
if (!isBlank(rest.cloneContents())) break;
|
|
192
|
+
edge = edge.parentElement;
|
|
193
|
+
}
|
|
194
|
+
return edge;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// Froala captions an image by wrapping the whole paragraph it is in, so any
|
|
198
|
+
// text sharing it ends up inside the caption, where it can't be edited, and
|
|
199
|
+
// goes away with the caption. Leave the image alone in its paragraph first,
|
|
200
|
+
// out of the bold or the link it may be in too: their text stays around it,
|
|
201
|
+
// still bold or linked. A linked image leaves the paragraph, in its link alone:
|
|
202
|
+
// Froala wraps just the link, and the wrapper div it adds can't sit inside a <p>.
|
|
203
|
+
//
|
|
204
|
+
// Removing a caption leaves the image in that wrapper div, out of any
|
|
205
|
+
// paragraph, and captioning it there nests a second wrapper that Froala then
|
|
206
|
+
// fails to unwrap: it throws, and unwraps another image's. Put the image back
|
|
207
|
+
// in a paragraph before anything else.
|
|
208
|
+
const isolateImage = (img: HTMLElement) => {
|
|
209
|
+
const unit = img.parentElement?.tagName === "A" ? img.parentElement : img;
|
|
210
|
+
const leftoverWrap = unit.parentElement;
|
|
211
|
+
if (leftoverWrap?.classList.contains("fr-img-space-wrap")) {
|
|
212
|
+
const p = document.createElement("p");
|
|
213
|
+
p.append(...leftoverWrap.childNodes);
|
|
214
|
+
leftoverWrap.replaceWith(p);
|
|
215
|
+
}
|
|
216
|
+
const paragraph = img.closest("p");
|
|
217
|
+
if (!paragraph) return;
|
|
218
|
+
splitAround(paragraph, edgeAround(img, paragraph, "before"), edgeAround(img, paragraph, "after"), paragraph);
|
|
219
|
+
const link = img.closest("a");
|
|
220
|
+
if (link && paragraph.contains(link)) {
|
|
221
|
+
link.replaceChildren(img);
|
|
222
|
+
paragraph.replaceWith(link);
|
|
223
|
+
} else {
|
|
224
|
+
paragraph.replaceChildren(img);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
// Froala keeps the paragraph it wrapped inside the caption, so the caption's
|
|
229
|
+
// text sits in a <p> inside a <span>: it takes the paragraph style of the page,
|
|
230
|
+
// Froala's caption style misses it, and resizing measures the image against the
|
|
231
|
+
// caption itself. Leave the image and its caption text straight in the wrapper.
|
|
232
|
+
const dropCaptionParagraph = (editor: any, img: HTMLElement) => {
|
|
233
|
+
const paragraph = img.parentElement;
|
|
234
|
+
if (!paragraph?.matches(".fr-img-wrap > p")) return;
|
|
235
|
+
// Moving the caption's text drops the selection Froala leaves on it, to type over
|
|
236
|
+
editor.selection.save();
|
|
237
|
+
paragraph.replaceWith(...paragraph.childNodes);
|
|
238
|
+
editor.selection.restore();
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
// The div Froala keeps a captioned image in, and leaves it in once the caption
|
|
242
|
+
// goes. It should hold nothing but the image.
|
|
243
|
+
const imageWrapOf = (img: Element) => img.closest(".fr-img-space-wrap") || img.closest(".fr-img-caption");
|
|
244
|
+
|
|
245
|
+
// Captions put on before isolateImage still hold the text of the paragraph
|
|
246
|
+
// they wrapped, and Froala deletes it along with the caption: take it out to
|
|
247
|
+
// paragraphs around the image first.
|
|
248
|
+
const takeTextOutOfCaption = (img: HTMLElement) => {
|
|
249
|
+
const paragraph = img.parentElement;
|
|
250
|
+
const wrap = imageWrapOf(img);
|
|
251
|
+
if (!paragraph?.matches(".fr-img-wrap > p") || !wrap) return;
|
|
252
|
+
splitAround(paragraph, img, paragraph.querySelector(":scope > .fr-inner") || img, wrap);
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
// Removing an image leaves its wrapper div empty, with the caret in it, so what
|
|
256
|
+
// is typed next would not be in a paragraph: make the div one. Froala hands AX
|
|
257
|
+
// the content only when it saves an undo step, and it saved the removal with
|
|
258
|
+
// the div still there: save the paragraph as well.
|
|
259
|
+
const removeImage = (editor: any) => {
|
|
260
|
+
const wrap = imageWrapOf(editor.image.get().get(0));
|
|
261
|
+
editor.image.remove();
|
|
262
|
+
if (!wrap?.isConnected || wrap.querySelector("img")) return;
|
|
263
|
+
const p = document.createElement("p");
|
|
264
|
+
p.append(...wrap.childNodes);
|
|
265
|
+
wrap.replaceWith(p);
|
|
266
|
+
editor.selection.setAtStart(p);
|
|
267
|
+
editor.selection.restore();
|
|
268
|
+
editor.undo.saveStep();
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
// Leaving a selected image in a wrapper div with the arrows or Esc puts the
|
|
272
|
+
// caret inside the div, after the caption, and what is typed there is not in a
|
|
273
|
+
// paragraph: take the caret to the block next to the image, or to a new one.
|
|
274
|
+
const leaveWrappedImage = (editor: any, wrap: Element, key: string) => {
|
|
275
|
+
if (!["ArrowLeft", "ArrowUp", "ArrowRight", "ArrowDown", "Escape"].includes(key)) return false;
|
|
276
|
+
const forward = key !== "ArrowLeft" && key !== "ArrowUp";
|
|
277
|
+
let block = forward ? wrap.nextElementSibling : wrap.previousElementSibling;
|
|
278
|
+
if (!block?.matches("p, h1, h2, h3, h4, h5, h6, ul, ol, blockquote, pre, table")) {
|
|
279
|
+
block = document.createElement("p");
|
|
280
|
+
block.innerHTML = "<br>";
|
|
281
|
+
wrap[forward ? "after" : "before"](block);
|
|
282
|
+
}
|
|
283
|
+
editor.image.exitEdit(true);
|
|
284
|
+
if (forward) editor.selection.setAtStart(block);
|
|
285
|
+
else editor.selection.setAtEnd(block);
|
|
286
|
+
editor.selection.restore();
|
|
287
|
+
// A new paragraph has to reach AX too (see removeImage). With none, nothing is saved.
|
|
288
|
+
editor.undo.saveStep();
|
|
289
|
+
return true;
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
// Backspace at the start of the block after an image in a wrapper div merges
|
|
293
|
+
// that block into the div, and Delete at the end of the block before merges
|
|
294
|
+
// the caption out into that block. Select the image instead, dropping the
|
|
295
|
+
// block if it is empty: a second press removes the image.
|
|
296
|
+
const selectNeighbourImage = (editor: any, key: string) => {
|
|
297
|
+
const selection = editor.selection.get();
|
|
298
|
+
if ((key !== "Backspace" && key !== "Delete") || !selection.rangeCount || !selection.isCollapsed) return false;
|
|
299
|
+
const forward = key === "Delete";
|
|
300
|
+
const block: Element | undefined = editor.selection.blocks()[0];
|
|
301
|
+
const neighbour = forward ? block?.nextElementSibling : block?.previousElementSibling;
|
|
302
|
+
const img = neighbour?.matches(".fr-img-space-wrap") ? neighbour.querySelector("img") : null;
|
|
303
|
+
if (!block || !img) return false;
|
|
304
|
+
const toEdge = selection.getRangeAt(0).cloneRange();
|
|
305
|
+
if (forward) toEdge.setEnd(block, block.childNodes.length);
|
|
306
|
+
else toEdge.setStart(block, 0);
|
|
307
|
+
if (!isBlank(toEdge.cloneContents())) return false;
|
|
308
|
+
toEdge.selectNodeContents(block);
|
|
309
|
+
// Dropping the block has to reach AX too: see removeImage
|
|
310
|
+
if (isBlank(toEdge.cloneContents())) {
|
|
311
|
+
block.remove();
|
|
312
|
+
editor.undo.saveStep();
|
|
313
|
+
}
|
|
314
|
+
editor.image.edit(editor.$(img));
|
|
315
|
+
return true;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
// Keeps text out of the div Froala wraps a captioned image in, which the keys
|
|
319
|
+
// around it move text into and the caption out of.
|
|
320
|
+
const handleImageWrapKeys = (editor: any, e: KeyboardEvent) => {
|
|
321
|
+
if (e.isComposing) return;
|
|
322
|
+
const img = editor.image.get()?.get(0);
|
|
323
|
+
const wrap = img && imageWrapOf(img);
|
|
324
|
+
if (!img) {
|
|
325
|
+
if (!selectNeighbourImage(editor, e.key)) return;
|
|
326
|
+
} else if (!wrap) {
|
|
327
|
+
return;
|
|
328
|
+
} else if (e.key === "Backspace" || e.key === "Delete") {
|
|
329
|
+
removeImage(editor);
|
|
330
|
+
} else if (!leaveWrappedImage(editor, wrap, e.key)) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
e.preventDefault();
|
|
334
|
+
e.stopPropagation();
|
|
335
|
+
return false;
|
|
336
|
+
};
|
|
337
|
+
|
|
155
338
|
export {
|
|
156
339
|
createPopupFunctions,
|
|
340
|
+
dropCaptionParagraph,
|
|
157
341
|
getImagePercentWidth,
|
|
158
342
|
getLanguageMenuHtml,
|
|
159
343
|
getPopupPosition,
|
|
160
344
|
getRichTextConfig,
|
|
345
|
+
handleImageWrapKeys,
|
|
161
346
|
type IRichTextConfig,
|
|
347
|
+
isolateImage,
|
|
162
348
|
parseClassNames,
|
|
349
|
+
removeImage,
|
|
163
350
|
setLinkAttributes,
|
|
351
|
+
takeTextOutOfCaption,
|
|
164
352
|
};
|
|
@@ -11,7 +11,17 @@ import FroalaEditor from "froala-editor";
|
|
|
11
11
|
|
|
12
12
|
import { LinkPopover } from "./atoms";
|
|
13
13
|
import { buttons, buttonsFull, inlineToolbar, wysiwygConfig } from "./config";
|
|
14
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
createPopupFunctions,
|
|
16
|
+
dropCaptionParagraph,
|
|
17
|
+
getImagePercentWidth,
|
|
18
|
+
getPopupPosition,
|
|
19
|
+
handleImageWrapKeys,
|
|
20
|
+
isolateImage,
|
|
21
|
+
removeImage,
|
|
22
|
+
setLinkAttributes,
|
|
23
|
+
takeTextOutOfCaption,
|
|
24
|
+
} from "./helpers";
|
|
15
25
|
import "./vendors";
|
|
16
26
|
|
|
17
27
|
import { useModals } from "@ax/hooks";
|
|
@@ -51,6 +61,7 @@ const Wysiwyg = (props: IWysiwygProps) => {
|
|
|
51
61
|
isImageLink?: boolean;
|
|
52
62
|
} | null>(null);
|
|
53
63
|
const replaceImgRef = useRef<any>(null);
|
|
64
|
+
const captioningImgRef = useRef<HTMLElement | null>(null);
|
|
54
65
|
const captionWidthRef = useRef<any>(null);
|
|
55
66
|
const uncaptionedHeightRef = useRef<{ img: HTMLElement; height: string } | null>(null);
|
|
56
67
|
const liftedCaptionRef = useRef<{ paragraph: HTMLElement; nodes: ChildNode[] } | null>(null);
|
|
@@ -213,12 +224,12 @@ const Wysiwyg = (props: IWysiwygProps) => {
|
|
|
213
224
|
if (!editorEl) return;
|
|
214
225
|
|
|
215
226
|
// Froala resizes a percentage image against the nearest block around
|
|
216
|
-
// it, and
|
|
217
|
-
// every step of the drag measures against the width
|
|
218
|
-
// just set, and the image bounces. Lift the caption's
|
|
219
|
-
// that paragraph while dragging, so it measures against
|
|
220
|
-
// and put it back in "image.resizeEnd". Capturing runs it
|
|
221
|
-
// Froala's own mousedown on the handle.
|
|
227
|
+
// it, and in captions saved with a <p> inside (see dropCaptionParagraph)
|
|
228
|
+
// that is the <p>: every step of the drag measures against the width
|
|
229
|
+
// the step before just set, and the image bounces. Lift the caption's
|
|
230
|
+
// content out of that paragraph while dragging, so it measures against
|
|
231
|
+
// the column, and put it back in "image.resizeEnd". Capturing runs it
|
|
232
|
+
// before Froala's own mousedown on the handle.
|
|
222
233
|
if (this.opts.imageResizeWithPercent) {
|
|
223
234
|
this.$box?.get?.(0)?.addEventListener(
|
|
224
235
|
"mousedown",
|
|
@@ -233,8 +244,9 @@ const Wysiwyg = (props: IWysiwygProps) => {
|
|
|
233
244
|
true,
|
|
234
245
|
);
|
|
235
246
|
|
|
236
|
-
//
|
|
237
|
-
// popup only reads the image's own:
|
|
247
|
+
// Captions saved before the image kept its width too have it on the
|
|
248
|
+
// caption alone, but Froala's size popup only reads the image's own:
|
|
249
|
+
// show the caption's instead of auto.
|
|
238
250
|
this.popups.onRefresh("image.size", () => {
|
|
239
251
|
const caption = this.image.get()?.get(0)?.closest(".fr-img-caption");
|
|
240
252
|
if (!caption?.style.width) return;
|
|
@@ -242,6 +254,9 @@ const Wysiwyg = (props: IWysiwygProps) => {
|
|
|
242
254
|
});
|
|
243
255
|
}
|
|
244
256
|
|
|
257
|
+
// First, so it runs before Froala's own handling of these keys
|
|
258
|
+
this.events.on("keydown", (e: KeyboardEvent) => handleImageWrapKeys(this, e), true);
|
|
259
|
+
|
|
245
260
|
editorEl.addEventListener("griddo:openLinkPopover", (e: Event) => {
|
|
246
261
|
const detail = (e as CustomEvent).detail;
|
|
247
262
|
setLinkPopoverOpen(true);
|
|
@@ -331,30 +346,40 @@ const Wysiwyg = (props: IWysiwygProps) => {
|
|
|
331
346
|
// checking there is one: on an image inside a paragraph, or never resized,
|
|
332
347
|
// it throws and no caption appears. Hand it the image's own width, and
|
|
333
348
|
// once the caption is in, give that width to the caption, as Froala's
|
|
334
|
-
// resizing expects, and
|
|
335
|
-
//
|
|
349
|
+
// resizing expects, and keep it on the image: out of the editor nothing
|
|
350
|
+
// sizes the caption. In here the image fills its caption (see style.tsx),
|
|
351
|
+
// so the width is not applied twice. An image never resized has no width
|
|
352
|
+
// to keep: it leaves with none, so the site's default size still applies.
|
|
336
353
|
//
|
|
337
|
-
//
|
|
338
|
-
//
|
|
339
|
-
//
|
|
354
|
+
// In any mode, Froala's caption swallows the paragraph the image is in,
|
|
355
|
+
// and removing it deletes whatever it holds: see isolateImage,
|
|
356
|
+
// dropCaptionParagraph and takeTextOutOfCaption. Removing the image is
|
|
357
|
+
// ours too: see removeImage.
|
|
340
358
|
"commands.before": function (this: any, cmd: string) {
|
|
341
359
|
if (cmd === "fullscreen" && !this.fullscreen.isActive()) {
|
|
342
360
|
this.opts.z_index = this.opts.zIndex;
|
|
343
361
|
}
|
|
362
|
+
if (cmd === "imageRemove" && this.image.get()) {
|
|
363
|
+
removeImage(this);
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
if (cmd === "imageCaption" && this.image.get()) {
|
|
367
|
+
const img = this.image.get().get(0);
|
|
368
|
+
if (this.image.hasCaption()) {
|
|
369
|
+
takeTextOutOfCaption(img);
|
|
370
|
+
} else {
|
|
371
|
+
isolateImage(img);
|
|
372
|
+
captioningImgRef.current = img;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
344
375
|
if (cmd === "imageCaption" && this.opts.imageResizeWithPercent && !this.image.hasCaption()) {
|
|
345
376
|
const $img = this.image.get();
|
|
346
377
|
if (!$img) return;
|
|
347
378
|
const img = $img.get(0);
|
|
348
|
-
const
|
|
349
|
-
if (leftoverWrap) {
|
|
350
|
-
const p = document.createElement("p");
|
|
351
|
-
p.append(...leftoverWrap.childNodes);
|
|
352
|
-
leftoverWrap.replaceWith(p);
|
|
353
|
-
}
|
|
354
|
-
const width = getImagePercentWidth(this, $img);
|
|
379
|
+
const width = img.style.width ? getImagePercentWidth(this, $img) : null;
|
|
355
380
|
const $sized = $img.parent().is("a") || $img.parent().is("p") ? $img.parent() : $img;
|
|
356
381
|
const seeded = !$sized.attr("style") && !$sized.attr("width");
|
|
357
|
-
if (seeded) $sized.attr("width", width);
|
|
382
|
+
if (seeded) $sized.attr("width", width ?? "100%");
|
|
358
383
|
captionWidthRef.current = { img, width, $seeded: seeded ? $sized : null };
|
|
359
384
|
}
|
|
360
385
|
// Removing the caption hands the image the caption's style in place of
|
|
@@ -368,13 +393,20 @@ const Wysiwyg = (props: IWysiwygProps) => {
|
|
|
368
393
|
if (cmd === "fullscreen" && !this.fullscreen.isActive()) {
|
|
369
394
|
this.$tb.css("z-index", "");
|
|
370
395
|
}
|
|
396
|
+
if (cmd === "imageCaption" && captioningImgRef.current) {
|
|
397
|
+
dropCaptionParagraph(this, captioningImgRef.current);
|
|
398
|
+
captioningImgRef.current = null;
|
|
399
|
+
}
|
|
371
400
|
if (cmd === "imageCaption" && captionWidthRef.current) {
|
|
372
401
|
const { img, width, $seeded } = captionWidthRef.current;
|
|
373
402
|
$seeded?.removeAttr("width");
|
|
374
403
|
const caption = img.closest(".fr-img-caption");
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
404
|
+
for (const el of [caption, img]) {
|
|
405
|
+
if (!el) continue;
|
|
406
|
+
if (width) el.style.width = width;
|
|
407
|
+
else el.style.removeProperty("width");
|
|
408
|
+
if (!el.getAttribute("style")) el.removeAttribute("style");
|
|
409
|
+
}
|
|
378
410
|
captionWidthRef.current = null;
|
|
379
411
|
}
|
|
380
412
|
if (cmd === "imageCaption" && uncaptionedHeightRef.current) {
|
|
@@ -383,25 +415,29 @@ const Wysiwyg = (props: IWysiwygProps) => {
|
|
|
383
415
|
uncaptionedHeightRef.current = null;
|
|
384
416
|
}
|
|
385
417
|
// The size popup sets the width and height on the caption and on the
|
|
386
|
-
// image inside it
|
|
387
|
-
//
|
|
388
|
-
// text. Keep the width on the caption and the height on the image.
|
|
418
|
+
// image inside it. The width belongs on both, but a fixed height on the
|
|
419
|
+
// caption leaves no room for its text: the height is the image's alone.
|
|
389
420
|
if (cmd === "imageSetSize" && this.opts.imageResizeWithPercent && this.image.hasCaption()) {
|
|
390
|
-
const
|
|
391
|
-
const caption = img?.closest(".fr-img-caption");
|
|
392
|
-
img?.style.removeProperty("width");
|
|
421
|
+
const caption = this.image.get()?.get(0)?.closest(".fr-img-caption");
|
|
393
422
|
caption?.style.removeProperty("height");
|
|
394
|
-
|
|
395
|
-
if (el && !el.getAttribute("style")) el.removeAttribute("style");
|
|
396
|
-
}
|
|
423
|
+
if (caption && !caption.getAttribute("style")) caption.removeAttribute("style");
|
|
397
424
|
}
|
|
398
425
|
},
|
|
399
|
-
|
|
426
|
+
// Froala's resizing sizes a captioned image's caption alone: the image
|
|
427
|
+
// takes the same width, for outside the editor. Froala hands AX the
|
|
428
|
+
// content when it saves an undo step, and it saved the drag's before
|
|
429
|
+
// this: save again, or AX keeps the width the image had before.
|
|
430
|
+
"image.resizeEnd": function (this: any, $img: any) {
|
|
431
|
+
const img = $img?.get?.(0);
|
|
432
|
+
const caption = img?.closest(".fr-img-caption");
|
|
433
|
+
if (caption) img.style.width = caption.style.width;
|
|
400
434
|
const lifted = liftedCaptionRef.current;
|
|
401
|
-
if (
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
435
|
+
if (lifted) {
|
|
436
|
+
lifted.nodes[0].before(lifted.paragraph);
|
|
437
|
+
lifted.paragraph.append(...lifted.nodes);
|
|
438
|
+
liftedCaptionRef.current = null;
|
|
439
|
+
}
|
|
440
|
+
this.undo.saveStep();
|
|
405
441
|
},
|
|
406
442
|
// Froala's shortcut dispatcher calls plugin commands unbound, so its own
|
|
407
443
|
// Ctrl/Cmd+K throws instead of opening anything. Returning false cancels
|
|
@@ -81,6 +81,11 @@ export const EditorWrapper = styled.div<{ error: boolean | undefined; disabled?:
|
|
|
81
81
|
font-size: 17px;
|
|
82
82
|
font-weight: 600;
|
|
83
83
|
}
|
|
84
|
+
/* A captioned image carries its width for outside the editor, where nothing
|
|
85
|
+
sizes the caption. In here the caption has that width, and the image fills it. */
|
|
86
|
+
.fr-img-caption img {
|
|
87
|
+
width: 100% !important;
|
|
88
|
+
}
|
|
84
89
|
.fr-toolbar {
|
|
85
90
|
border-color: ${(p) =>
|
|
86
91
|
p.error === true ? p.theme.color.error : p.disabled ? p.theme.color.interactiveDisabled : p.theme.color.uiLine};
|