UNPKG

@paroicms/internal-link-plugin

Version:

Internal link plugin for ParoiCMS

767 lines (766 loc) 23.6 kB
const ht = (t, e) => t === e, O = { equals: ht }; let it = ot; const E = 1, V = 2, lt = { owned: null, cleanups: null, context: null, owner: null }; var k = null; let H = null, pt = null, w = null, b = null, I = null, U = 0; function gt(t, e) { const n = w, i = k, l = t.length === 0, r = i, o = l ? lt : { owned: null, cleanups: null, context: r ? r.context : null, owner: r }, s = l ? t : () => t(() => A(() => T(o))); k = o, w = null; try { return B(s, !0); } finally { w = n, k = i; } } function S(t, e) { e = e ? Object.assign({}, O, e) : O; const n = { value: t, observers: null, observerSlots: null, comparator: e.equals || void 0 }, i = (l) => (typeof l == "function" && (l = l(n.value)), st(n, l)); return [rt.bind(n), i]; } function C(t, e, n) { const i = q(t, e, !1, E); N(i); } function G(t, e, n) { it = yt; const i = q(t, e, !1, E); i.user = !0, I ? I.push(i) : N(i); } function P(t, e, n) { n = n ? Object.assign({}, O, n) : O; const i = q(t, e, !0, 0); return i.observers = null, i.observerSlots = null, i.comparator = n.equals || void 0, N(i), rt.bind(i); } function A(t) { if (w === null) return t(); const e = w; w = null; try { return t(); } finally { w = e; } } function rt() { if (this.sources && this.state) if (this.state === E) N(this); else { const t = b; b = null, B(() => M(this), !1), b = t; } if (w) { const t = this.observers ? this.observers.length : 0; w.sources ? (w.sources.push(this), w.sourceSlots.push(t)) : (w.sources = [this], w.sourceSlots = [t]), this.observers ? (this.observers.push(w), this.observerSlots.push(w.sources.length - 1)) : (this.observers = [w], this.observerSlots = [w.sources.length - 1]); } return this.value; } function st(t, e, n) { let i = t.value; return (!t.comparator || !t.comparator(i, e)) && (t.value = e, t.observers && t.observers.length && B(() => { for (let l = 0; l < t.observers.length; l += 1) { const r = t.observers[l], o = H && H.running; o && H.disposed.has(r), (o ? !r.tState : !r.state) && (r.pure ? b.push(r) : I.push(r), r.observers && ct(r)), o || (r.state = E); } if (b.length > 1e6) throw b = [], new Error(); }, !1)), e; } function N(t) { if (!t.fn) return; T(t); const e = U; $t(t, t.value, e); } function $t(t, e, n) { let i; const l = k, r = w; w = k = t; try { i = t.fn(e); } catch (o) { return t.pure && (t.state = E, t.owned && t.owned.forEach(T), t.owned = null), t.updatedAt = n + 1, ut(o); } finally { w = r, k = l; } (!t.updatedAt || t.updatedAt <= n) && (t.updatedAt != null && "observers" in t ? st(t, i) : t.value = i, t.updatedAt = n); } function q(t, e, n, i = E, l) { const r = { fn: t, state: i, updatedAt: null, owned: null, sources: null, sourceSlots: null, cleanups: null, value: e, owner: k, context: k ? k.context : null, pure: n }; return k === null || k !== lt && (k.owned ? k.owned.push(r) : k.owned = [r]), r; } function R(t) { if (t.state === 0) return; if (t.state === V) return M(t); if (t.suspense && A(t.suspense.inFallback)) return t.suspense.effects.push(t); const e = [t]; for (; (t = t.owner) && (!t.updatedAt || t.updatedAt < U); ) t.state && e.push(t); for (let n = e.length - 1; n >= 0; n--) if (t = e[n], t.state === E) N(t); else if (t.state === V) { const i = b; b = null, B(() => M(t, e[0]), !1), b = i; } } function B(t, e) { if (b) return t(); let n = !1; e || (b = []), I ? n = !0 : I = [], U++; try { const i = t(); return mt(n), i; } catch (i) { n || (I = null), b = null, ut(i); } } function mt(t) { if (b && (ot(b), b = null), t) return; const e = I; I = null, e.length && B(() => it(e), !1); } function ot(t) { for (let e = 0; e < t.length; e++) R(t[e]); } function yt(t) { let e, n = 0; for (e = 0; e < t.length; e++) { const i = t[e]; i.user ? t[n++] = i : R(i); } for (e = 0; e < n; e++) R(t[e]); } function M(t, e) { t.state = 0; for (let n = 0; n < t.sources.length; n += 1) { const i = t.sources[n]; if (i.sources) { const l = i.state; l === E ? i !== e && (!i.updatedAt || i.updatedAt < U) && R(i) : l === V && M(i, e); } } } function ct(t) { for (let e = 0; e < t.observers.length; e += 1) { const n = t.observers[e]; n.state || (n.state = V, n.pure ? b.push(n) : I.push(n), n.observers && ct(n)); } } function T(t) { let e; if (t.sources) for (; t.sources.length; ) { const n = t.sources.pop(), i = t.sourceSlots.pop(), l = n.observers; if (l && l.length) { const r = l.pop(), o = n.observerSlots.pop(); i < l.length && (r.sourceSlots[o] = i, l[i] = r, n.observerSlots[i] = o); } } if (t.tOwned) { for (e = t.tOwned.length - 1; e >= 0; e--) T(t.tOwned[e]); delete t.tOwned; } if (t.owned) { for (e = t.owned.length - 1; e >= 0; e--) T(t.owned[e]); t.owned = null; } if (t.cleanups) { for (e = t.cleanups.length - 1; e >= 0; e--) t.cleanups[e](); t.cleanups = null; } t.state = 0; } function wt(t) { return t instanceof Error ? t : new Error(typeof t == "string" ? t : "Unknown error", { cause: t }); } function ut(t, e = k) { throw wt(t); } function x(t, e) { return A(() => t(e || {})); } const bt = (t) => `Stale read from <${t}>.`; function W(t) { const e = t.keyed, n = P(() => t.when, void 0, void 0), i = e ? n : P(n, void 0, { equals: (l, r) => !l == !r }); return P(() => { const l = i(); if (l) { const r = t.children; return typeof r == "function" && r.length > 0 ? A(() => r(e ? l : () => { if (!A(i)) throw bt("Show"); return n(); })) : r; } return t.fallback; }, void 0, void 0); } const L = (t) => P(() => t()); function kt(t, e, n) { let i = n.length, l = e.length, r = i, o = 0, s = 0, u = e[l - 1].nextSibling, $ = null; for (; o < l || s < r; ) { if (e[o] === n[s]) { o++, s++; continue; } for (; e[l - 1] === n[r - 1]; ) l--, r--; if (l === o) { const f = r < i ? s ? n[s - 1].nextSibling : n[r - s] : u; for (; s < r; ) t.insertBefore(n[s++], f); } else if (r === s) for (; o < l; ) (!$ || !$.has(e[o])) && e[o].remove(), o++; else if (e[o] === n[r - 1] && n[s] === e[l - 1]) { const f = e[--l].nextSibling; t.insertBefore(n[s++], e[o++].nextSibling), t.insertBefore(n[--r], f), e[l] = n[r]; } else { if (!$) { $ = /* @__PURE__ */ new Map(); let h = s; for (; h < r; ) $.set(n[h], h++); } const f = $.get(e[o]); if (f != null) if (s < f && f < r) { let h = o, m = 1, c; for (; ++h < l && h < r && !((c = $.get(e[h])) == null || c !== f + m); ) m++; if (m > f - s) { const a = e[o]; for (; s < f; ) t.insertBefore(n[s++], a); } else t.replaceChild(n[s++], e[o++]); } else o++; else e[o++].remove(); } } } const X = "_$DX_DELEGATE"; function y(t, e, n, i) { let l; const r = () => { const s = document.createElement("template"); return s.innerHTML = t, s.content.firstChild; }, o = () => (l || (l = r())).cloneNode(!0); return o.cloneNode = o, o; } function K(t, e = window.document) { const n = e[X] || (e[X] = /* @__PURE__ */ new Set()); for (let i = 0, l = t.length; i < l; i++) { const r = t[i]; n.has(r) || (n.add(r), e.addEventListener(r, vt)); } } function _t(t, e) { e == null ? t.removeAttribute("class") : t.className = e; } function p(t, e, n, i) { if (n !== void 0 && !i && (i = []), typeof e != "function") return j(t, e, i, n); C((l) => j(t, e(), l, n), i); } function vt(t) { let e = t.target; const n = `$$${t.type}`, i = t.target, l = t.currentTarget, r = (u) => Object.defineProperty(t, "target", { configurable: !0, value: u }), o = () => { const u = e[n]; if (u && !e.disabled) { const $ = e[`${n}Data`]; if ($ !== void 0 ? u.call(e, $, t) : u.call(e, t), t.cancelBubble) return; } return e.host && typeof e.host != "string" && !e.host._$host && e.contains(t.target) && r(e.host), !0; }, s = () => { for (; o() && (e = e._$host || e.parentNode || e.host); ) ; }; if (Object.defineProperty(t, "currentTarget", { configurable: !0, get() { return e || document; } }), t.composedPath) { const u = t.composedPath(); r(u[0]); for (let $ = 0; $ < u.length - 2 && (e = u[$], !!o()); $++) { if (e._$host) { e = e._$host, s(); break; } if (e.parentNode === l) break; } } else s(); r(i); } function j(t, e, n, i, l) { for (; typeof n == "function"; ) n = n(); if (e === n) return n; const r = typeof e, o = i !== void 0; if (t = o && n[0] && n[0].parentNode || t, r === "string" || r === "number") { if (r === "number" && (e = e.toString(), e === n)) return n; if (o) { let s = n[0]; s && s.nodeType === 3 ? s.data !== e && (s.data = e) : s = document.createTextNode(e), n = D(t, n, i, s); } else n !== "" && typeof n == "string" ? n = t.firstChild.data = e : n = t.textContent = e; } else if (e == null || r === "boolean") n = D(t, n, i); else { if (r === "function") return C(() => { let s = e(); for (; typeof s == "function"; ) s = s(); n = j(t, s, n, i); }), () => n; if (Array.isArray(e)) { const s = [], u = n && Array.isArray(n); if (F(s, e, n, l)) return C(() => n = j(t, s, n, i, !0)), () => n; if (s.length === 0) { if (n = D(t, n, i), o) return n; } else u ? n.length === 0 ? J(t, s, i) : kt(t, n, s) : (n && D(t), J(t, s)); n = s; } else if (e.nodeType) { if (Array.isArray(n)) { if (o) return n = D(t, n, i, e); D(t, n, null, e); } else n == null || n === "" || !t.firstChild ? t.appendChild(e) : t.replaceChild(e, t.firstChild); n = e; } } return n; } function F(t, e, n, i) { let l = !1; for (let r = 0, o = e.length; r < o; r++) { let s = e[r], u = n && n[t.length], $; if (!(s == null || s === !0 || s === !1)) if (($ = typeof s) == "object" && s.nodeType) t.push(s); else if (Array.isArray(s)) l = F(t, s, u) || l; else if ($ === "function") if (i) { for (; typeof s == "function"; ) s = s(); l = F(t, Array.isArray(s) ? s : [s], Array.isArray(u) ? u : [u]) || l; } else t.push(s), l = !0; else { const f = String(s); u && u.nodeType === 3 && u.data === f ? t.push(u) : t.push(document.createTextNode(f)); } } return l; } function J(t, e, n = null) { for (let i = 0, l = e.length; i < l; i++) t.insertBefore(e[i], n); } function D(t, e, n, i) { if (n === void 0) return t.textContent = ""; const l = i || document.createTextNode(""); if (e.length) { let r = !1; for (let o = e.length - 1; o >= 0; o--) { const s = e[o]; if (l !== s) { const u = s.parentNode === t; !r && !o ? u ? t.replaceChild(l, s) : t.insertBefore(l, n) : u && s.remove(); } else r = !0; } } else t.insertBefore(l, n); return [l]; } const at = (t) => t != null && (t = Object.getPrototypeOf(t), t === Array.prototype || t === Object.prototype); function ft(t, e, n) { for (const [i, l] of Object.entries(e)) { const r = `${n}.${i}`; t[r] = l, at(l) && ft(t, l, r); } } function Lt(t) { const e = { ...t }; for (const [n, i] of Object.entries(t)) at(i) && ft(e, i, n); return e; } const It = (t) => t; function St(t, e = It) { return (n, ...i) => { n[0] === "." && (n = n.slice(1)); const l = t()?.[n]; switch (typeof l) { case "function": return l(...i); case "string": return e(l, i[0]); default: return l; } }; } const Et = { insertLink: "Insert link", change: "Change", editor: { noResult: "No results found", draft: "Draft", published: "Published", sheduled: "Sheduled" }, dialog: { title: "Insert an internal link", removeLink: "Remove link", changeLink: "Find another link", ok: "OK" } }, Dt = { InternalLink: Et }, Pt = { insertLink: "Insérer un lien", change: "Modifier", editor: { noResult: "Aucun résultat trouvé", draft: "Brouillon", published: "Publié", sheduled: "Programmé" }, dialog: { title: "Insérer un lien interne", removeLink: "Supprimer le lien", changeLink: "Trouver un autre lien", ok: "OK" } }, Ct = { InternalLink: Pt }, Y = { en: Dt, fr: Ct }; function Q(t) { const e = P(() => { const i = t(), l = i in Y ? i : "en"; return Lt(Y[l]); }); return { t: St(e) }; } function z(t) { return t.varName ? ` for '${t.varName}'` : ""; } function tt(t, e = {}) { if (t instanceof Date) return t; if (t == null || t === "") throw new Error(`Missing number value${z(e)}`); if (typeof t == "string") { const n = t.includes("Z") || t.includes("+"); return new Date(n ? t : `${t}Z`); } if (typeof t == "number") return new Date(t); throw new Error(`Cannot convert to date the value of type '${typeof t}'${z(e)}`); } var At = /* @__PURE__ */ y("<div class=ILResult>"), Tt = /* @__PURE__ */ y('<div class=ILNoResult><span class=ILNoResult-message></span><span class="pi pi-exclamation-circle">'), xt = /* @__PURE__ */ y("<div class=InternalLinkEditor><form class=ILForm><input class=InternalLinkEditor-input type=text placeholder=Search>"), Nt = /* @__PURE__ */ y("<div class=ILResultItem><div class=ILResultItem-title></div><span>"); function dt(t) { const { value: e, setValue: n, service: i } = t, [l, r] = S(""), [o, s] = S([]), { t: u } = Q(t.language); G(async () => { const f = e(); if (!f) return; const h = await i.getDocument(f); r(h?.title ?? ""), await $(); }); const $ = async (f) => { if (f?.preventDefault(), l().length < 3) return; const h = await i.searchDocuments({ searchString: l() }); s(h); }; return (() => { var f = xt(), h = f.firstChild, m = h.firstChild; return h.addEventListener("submit", $), m.$$input = (c) => r(c.currentTarget.value), p(f, x(W, { when: o, get children() { var c = At(); return p(c, () => o().map((a) => ( // biome-ignore lint/correctness/useJsxKeyInIterable: no need key here (() => { var d = Nt(), g = d.firstChild, v = g.nextSibling; return d.$$click = () => { n(a.id); }, p(g, () => a.title), p(v, (() => { var _ = L(() => !!a.ready); return () => _() ? L(() => tt(a.publishDate).getTime() < (/* @__PURE__ */ new Date()).getTime())() ? u("InternalLink.editor.published") : u("InternalLink.editor.sheduled") : u("InternalLink.editor.draft"); })()), C(() => _t(v, `ILResultItem-status ${a.ready ? tt(a.publishDate).getTime() < (/* @__PURE__ */ new Date()).getTime() ? "published" : "sheduled" : "draft"}`)), d; })() ))), c; } }), null), p(f, x(W, { get when() { return !o() || o().length === 0; }, get children() { var c = Tt(), a = c.firstChild; return p(a, () => u("InternalLink.editor.noResult")), c; } }), null), C(() => m.value = l()), f; })(); } K(["input", "click"]); var Bt = /* @__PURE__ */ y("<div class=InternalLinkDialog-header><span class=InternalLinkDialog-title>"), Ot = /* @__PURE__ */ y('<button class="PlgBtn pi pi-arrow-left"type=button>'), Vt = /* @__PURE__ */ y("<div class=InternalLinkDialog-content>"), Rt = /* @__PURE__ */ y('<div class=ILPreview><div class=ILPreview-title></div><button class="PlgBtn outlined iconBtn pi pi-pencil"type=button>'), et = /* @__PURE__ */ y("<div>"), Mt = /* @__PURE__ */ y('<div><button class="PlgBtn primary"type=button>'), jt = /* @__PURE__ */ y('<button type=button class="PlgBtn danger outlined">'), nt = /* @__PURE__ */ y("<button class=PlgBtn type=button>"), Ut = /* @__PURE__ */ y('<button class="PlgBtn primary"type=button>'), Ht = /* @__PURE__ */ y('<div class=InternalLinkDialog-linkPreview><div></div><button class="PlgBtn iconBtn pi pi-pencil"type=button>'); function Ft({ service: t, setValue: e, value: n, language: i }) { const { t: l } = Q(i), [r, o] = S(), [s, u] = S(!!n()); G(async () => { const c = n(); if (!c) return; const a = await t.getDocument(c); if (!a) return; const { title: d, typeName: g } = a; o({ title: d, typeName: g }); }); const $ = (() => { var c = Bt(), a = c.firstChild; return p(c, (() => { var d = L(() => !!(n() && !s())); return () => d() && (() => { var g = Ot(); return g.$$click = () => { u(!0); }, g; })(); })(), a), p(a, () => l("InternalLink.dialog.title")), c; })(), f = (() => { var c = Vt(); return p(c, (() => { var a = L(() => !!(s() && n())); return () => a() ? (() => { var d = Rt(), g = d.firstChild, v = g.nextSibling; return p(g, () => r()?.title), v.$$click = () => { const _ = n(), Z = r()?.typeName; !_ || !Z || (m.close(), t.openDocumentEdition(_, Z)); }, d; })() : x(dt, { service: t, language: i, value: n, setValue: (d) => { e(d), u(!0), m.hide(); } }); })()), c; })(), h = (() => { var c = et(); return p(c, (() => { var a = L(() => !!s()); return () => a() ? (() => { var d = Mt(), g = d.firstChild; return p(d, (() => { var v = L(() => !!n()); return () => v() && [(() => { var _ = jt(); return _.$$click = () => { e(void 0), m.hide(); }, p(_, () => l("InternalLink.dialog.removeLink")), _; })(), (() => { var _ = nt(); return _.$$click = () => u(!1), p(_, () => l("InternalLink.dialog.changeLink")), _; })()]; })(), g), g.$$click = () => { m.hide(); }, p(g, () => l("InternalLink.dialog.ok")), d; })() : (() => { var d = Ut(); return d.$$click = () => { m.hide(); }, p(d, () => l("InternalLink.dialog.ok")), d; })(); })()), c; })(), m = t.createModalDialog({ content: f, footer: h, header: $ }); return (() => { var c = et(); return p(c, (() => { var a = L(() => !!n()); return () => a() ? (() => { var d = Ht(), g = d.firstChild, v = g.nextSibling; return p(g, () => r()?.title), v.$$click = () => m.show(), d; })() : (() => { var d = nt(); return d.$$click = () => m.show(), p(d, () => l("InternalLink.insertLink")), d; })(); })()), c; })(); } K(["click"]); var Gt = /* @__PURE__ */ y("<div class=InternalLinkDialog-header><span class=InternalLinkDialog-title>"), qt = /* @__PURE__ */ y('<button class="PlgBtn pi pi-arrow-left"type=button>'), Kt = /* @__PURE__ */ y("<div>"), Qt = /* @__PURE__ */ y('<div><button class="PlgBtn primary"type=button>'), Zt = /* @__PURE__ */ y('<button type=button class="PlgBtn danger outlined">'), Wt = /* @__PURE__ */ y("<button class=PlgBtn type=button>"), Xt = /* @__PURE__ */ y('<button class="PlgBtn primary"type=button>'), Jt = /* @__PURE__ */ y("<div class=InternalLinkDialog-content>"), Yt = /* @__PURE__ */ y('<div class=ILPreview><div class=ILPreview-title></div><button class="PlgBtn outlined iconBtn pi pi-pencil"type=button>'); async function zt({ value: t, getLanguage: e, service: n }) { const [i, l] = S(), { t: r } = Q(e), [o, s] = S(!!t()); G(async () => { const h = t(); if (!h) return; const m = await n.getDocument(h); if (!m) return; const { title: c, typeName: a } = m; l({ title: c, typeName: a }); }); const u = (() => { var h = Gt(), m = h.firstChild; return p(h, (() => { var c = L(() => !!(t() && !o())); return () => c() && (() => { var a = qt(); return a.$$click = () => { s(!0); }, a; })(); })(), m), p(m, () => r("InternalLink.dialog.title")), h; })(), $ = (() => { var h = Kt(); return p(h, (() => { var m = L(() => !!o()); return () => m() ? (() => { var c = Qt(), a = c.firstChild; return p(c, (() => { var d = L(() => !!t()); return () => d() && [(() => { var g = Zt(); return g.$$click = () => f.close("remove"), p(g, () => r("InternalLink.dialog.removeLink")), g; })(), (() => { var g = Wt(); return g.$$click = () => s(!1), p(g, () => r("InternalLink.dialog.changeLink")), g; })()]; })(), a), a.$$click = () => f.close(), p(a, () => r("InternalLink.dialog.ok")), c; })() : (() => { var c = Xt(); return c.$$click = () => f.close(), p(c, () => r("InternalLink.dialog.ok")), c; })(); })()), h; })(), f = n.createModalDialog({ content: (() => { var h = Jt(); return p(h, (() => { var m = L(() => !!(o() && t())); return () => m() ? (() => { var c = Yt(), a = c.firstChild, d = a.nextSibling; return p(a, () => i()?.title), d.$$click = () => { const g = t(), v = i()?.typeName; !g || !v || (f.close(), n.openDocumentEdition(g, v)); }, c; })() : x(dt, { service: n, language: e, value: t, setValue: (c) => f.close(c) }); })()), h; })(), header: u, footer: $ }); return await f.open(); } K(["click"]); const le = { create: ie, init: ne }; function te(t) { t.executeHook("htmlEditorServiceProvider").forEach((n) => { n.registerInternalLinkPlugin(ee(t)); }); } function ee(t) { return { async getDocumentTitle(e) { return (await t.getDocument(e))?.title; }, async openDialog(e, n) { const i = await zt({ value: () => e, getLanguage: n.getLanguage, service: t }); if (e) { if (!i) return; if (i === "remove") return { documentId: void 0 }; } return { documentId: i }; } }; } function ne({ pluginBaseUrl: t, addAfterInitListener: e }) { const n = `${t}/bo-plugin.css`, i = document.createElement("link"); i.rel = "stylesheet", i.href = n, document.head.appendChild(i), e(te); } function ie(t) { const { fieldType: e, initialValue: n } = t; if (e.dataType !== "string") throw new Error(`field type '${e.name}' is incompatible with internal-link plugin, needs data type 'string'`); if (n !== void 0 && typeof n != "string") throw new Error(`field type '${e.name}' is incompatible, should be a string, got '${typeof n}'`); return gt((i) => { const [l, r] = S(n ?? ""), [o, s] = S(t.language); return { element: x(Ft, { language: o, value: l, setValue: (u) => { r(u ?? ""), t.setModifiedValue(u ?? null); }, service: t }), setLanguage: s, setValue: r, getValue() { return l(); }, dispose: i }; }); } export { le as default };