UNPKG

@paroicms/internal-link-plugin

Version:

Internal link plugin for ParoiCMS

756 lines (755 loc) 24.3 kB
const fe = (e, t) => e === t, R = { equals: fe }; let te = re; const S = 1, V = 2, ne = { owned: null, cleanups: null, context: null, owner: null }; var v = null; let H = null, de = null, b = null, y = null, I = null, K = 0; function he(e, t) { const n = b, i = v, l = e.length === 0, r = i, o = l ? ne : { owned: null, cleanups: null, context: r ? r.context : null, owner: r }, s = l ? e : () => e(() => A(() => N(o))); v = o, b = null; try { return T(s, !0); } finally { b = n, v = i; } } function E(e, t) { t = t ? Object.assign({}, R, t) : R; const n = { value: e, observers: null, observerSlots: null, comparator: t.equals || void 0 }, i = (l) => (typeof l == "function" && (l = l(n.value)), le(n, l)); return [ie.bind(n), i]; } function C(e, t, n) { const i = q(e, t, !1, S); B(i); } function G(e, t, n) { te = $e; const i = q(e, t, !1, S); i.user = !0, I ? I.push(i) : B(i); } function D(e, t, n) { n = n ? Object.assign({}, R, n) : R; const i = q(e, t, !0, 0); return i.observers = null, i.observerSlots = null, i.comparator = n.equals || void 0, B(i), ie.bind(i); } function A(e) { if (b === null) return e(); const t = b; b = null; try { return e(); } finally { b = t; } } function ie() { if (this.sources && this.state) if (this.state === S) B(this); else { const e = y; y = null, T(() => M(this), !1), y = e; } if (b) { const e = this.observers ? this.observers.length : 0; b.sources ? (b.sources.push(this), b.sourceSlots.push(e)) : (b.sources = [this], b.sourceSlots = [e]), this.observers ? (this.observers.push(b), this.observerSlots.push(b.sources.length - 1)) : (this.observers = [b], this.observerSlots = [b.sources.length - 1]); } return this.value; } function le(e, t, n) { let i = e.value; return (!e.comparator || !e.comparator(i, t)) && (e.value = t, e.observers && e.observers.length && T(() => { for (let l = 0; l < e.observers.length; l += 1) { const r = e.observers[l], o = H && H.running; o && H.disposed.has(r), (o ? !r.tState : !r.state) && (r.pure ? y.push(r) : I.push(r), r.observers && se(r)), o || (r.state = S); } if (y.length > 1e6) throw y = [], new Error(); }, !1)), t; } function B(e) { if (!e.fn) return; N(e); const t = K; pe(e, e.value, t); } function pe(e, t, n) { let i; const l = v, r = b; b = v = e; try { i = e.fn(t); } catch (o) { return e.pure && (e.state = S, e.owned && e.owned.forEach(N), e.owned = null), e.updatedAt = n + 1, oe(o); } finally { b = r, v = l; } (!e.updatedAt || e.updatedAt <= n) && (e.updatedAt != null && "observers" in e ? le(e, i) : e.value = i, e.updatedAt = n); } function q(e, t, n, i = S, l) { const r = { fn: e, state: i, updatedAt: null, owned: null, sources: null, sourceSlots: null, cleanups: null, value: t, owner: v, context: v ? v.context : null, pure: n }; return v === null || v !== ne && (v.owned ? v.owned.push(r) : v.owned = [r]), r; } function j(e) { if (e.state === 0) return; if (e.state === V) return M(e); if (e.suspense && A(e.suspense.inFallback)) return e.suspense.effects.push(e); const t = [e]; for (; (e = e.owner) && (!e.updatedAt || e.updatedAt < K); ) e.state && t.push(e); for (let n = t.length - 1; n >= 0; n--) if (e = t[n], e.state === S) B(e); else if (e.state === V) { const i = y; y = null, T(() => M(e, t[0]), !1), y = i; } } function T(e, t) { if (y) return e(); let n = !1; t || (y = []), I ? n = !0 : I = [], K++; try { const i = e(); return ge(n), i; } catch (i) { n || (I = null), y = null, oe(i); } } function ge(e) { if (y && (re(y), y = null), e) return; const t = I; I = null, t.length && T(() => te(t), !1); } function re(e) { for (let t = 0; t < e.length; t++) j(e[t]); } function $e(e) { let t, n = 0; for (t = 0; t < e.length; t++) { const i = e[t]; i.user ? e[n++] = i : j(i); } for (t = 0; t < n; t++) j(e[t]); } function M(e, t) { e.state = 0; for (let n = 0; n < e.sources.length; n += 1) { const i = e.sources[n]; if (i.sources) { const l = i.state; l === S ? i !== t && (!i.updatedAt || i.updatedAt < K) && j(i) : l === V && M(i, t); } } } function se(e) { for (let t = 0; t < e.observers.length; t += 1) { const n = e.observers[t]; n.state || (n.state = V, n.pure ? y.push(n) : I.push(n), n.observers && se(n)); } } function N(e) { let t; if (e.sources) for (; e.sources.length; ) { const n = e.sources.pop(), i = e.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 (e.tOwned) { for (t = e.tOwned.length - 1; t >= 0; t--) N(e.tOwned[t]); delete e.tOwned; } if (e.owned) { for (t = e.owned.length - 1; t >= 0; t--) N(e.owned[t]); e.owned = null; } if (e.cleanups) { for (t = e.cleanups.length - 1; t >= 0; t--) e.cleanups[t](); e.cleanups = null; } e.state = 0; } function ke(e) { return e instanceof Error ? e : new Error(typeof e == "string" ? e : "Unknown error", { cause: e }); } function oe(e, t = v) { throw ke(e); } function x(e, t) { return A(() => e(t || {})); } const me = (e) => `Stale read from <${e}>.`; function X(e) { const t = e.keyed, n = D(() => e.when, void 0, void 0), i = t ? n : D(n, void 0, { equals: (l, r) => !l == !r }); return D(() => { const l = i(); if (l) { const r = e.children; return typeof r == "function" && r.length > 0 ? A(() => r(t ? l : () => { if (!A(i)) throw me("Show"); return n(); })) : r; } return e.fallback; }, void 0, void 0); } const _ = (e) => D(() => e()); function be(e, t, n) { let i = n.length, l = t.length, r = i, o = 0, s = 0, a = t[l - 1].nextSibling, k = null; for (; o < l || s < r; ) { if (t[o] === n[s]) { o++, s++; continue; } for (; t[l - 1] === n[r - 1]; ) l--, r--; if (l === o) { const $ = r < i ? s ? n[s - 1].nextSibling : n[r - s] : a; for (; s < r; ) e.insertBefore(n[s++], $); } else if (r === s) for (; o < l; ) (!k || !k.has(t[o])) && t[o].remove(), o++; else if (t[o] === n[r - 1] && n[s] === t[l - 1]) { const $ = t[--l].nextSibling; e.insertBefore(n[s++], t[o++].nextSibling), e.insertBefore(n[--r], $), t[l] = n[r]; } else { if (!k) { k = /* @__PURE__ */ new Map(); let f = s; for (; f < r; ) k.set(n[f], f++); } const $ = k.get(t[o]); if ($ != null) if (s < $ && $ < r) { let f = o, g = 1, c; for (; ++f < l && f < r && !((c = k.get(t[f])) == null || c !== $ + g); ) g++; if (g > $ - s) { const d = t[o]; for (; s < $; ) e.insertBefore(n[s++], d); } else e.replaceChild(n[s++], t[o++]); } else o++; else t[o++].remove(); } } } const J = "_$DX_DELEGATE"; function m(e, t, n, i) { let l; const r = () => { const s = document.createElement("template"); return s.innerHTML = e, s.content.firstChild; }, o = () => (l || (l = r())).cloneNode(!0); return o.cloneNode = o, o; } function Q(e, t = window.document) { const n = t[J] || (t[J] = /* @__PURE__ */ new Set()); for (let i = 0, l = e.length; i < l; i++) { const r = e[i]; n.has(r) || (n.add(r), t.addEventListener(r, ve)); } } function ye(e, t) { t == null ? e.removeAttribute("class") : e.className = t; } function p(e, t, n, i) { if (n !== void 0 && !i && (i = []), typeof t != "function") return U(e, t, i, n); C((l) => U(e, t(), l, n), i); } function ve(e) { let t = e.target; const n = `$$${e.type}`, i = e.target, l = e.currentTarget, r = (a) => Object.defineProperty(e, "target", { configurable: !0, value: a }), o = () => { const a = t[n]; if (a && !t.disabled) { const k = t[`${n}Data`]; if (k !== void 0 ? a.call(t, k, e) : a.call(t, e), e.cancelBubble) return; } return t.host && typeof t.host != "string" && !t.host._$host && t.contains(e.target) && r(t.host), !0; }, s = () => { for (; o() && (t = t._$host || t.parentNode || t.host); ) ; }; if (Object.defineProperty(e, "currentTarget", { configurable: !0, get() { return t || document; } }), e.composedPath) { const a = e.composedPath(); r(a[0]); for (let k = 0; k < a.length - 2 && (t = a[k], !!o()); k++) { if (t._$host) { t = t._$host, s(); break; } if (t.parentNode === l) break; } } else s(); r(i); } function U(e, t, n, i, l) { for (; typeof n == "function"; ) n = n(); if (t === n) return n; const r = typeof t, o = i !== void 0; if (e = o && n[0] && n[0].parentNode || e, r === "string" || r === "number") { if (r === "number" && (t = t.toString(), t === n)) return n; if (o) { let s = n[0]; s && s.nodeType === 3 ? s.data !== t && (s.data = t) : s = document.createTextNode(t), n = P(e, n, i, s); } else n !== "" && typeof n == "string" ? n = e.firstChild.data = t : n = e.textContent = t; } else if (t == null || r === "boolean") n = P(e, n, i); else { if (r === "function") return C(() => { let s = t(); for (; typeof s == "function"; ) s = s(); n = U(e, s, n, i); }), () => n; if (Array.isArray(t)) { const s = [], a = n && Array.isArray(n); if (F(s, t, n, l)) return C(() => n = U(e, s, n, i, !0)), () => n; if (s.length === 0) { if (n = P(e, n, i), o) return n; } else a ? n.length === 0 ? Y(e, s, i) : be(e, n, s) : (n && P(e), Y(e, s)); n = s; } else if (t.nodeType) { if (Array.isArray(n)) { if (o) return n = P(e, n, i, t); P(e, n, null, t); } else n == null || n === "" || !e.firstChild ? e.appendChild(t) : e.replaceChild(t, e.firstChild); n = t; } } return n; } function F(e, t, n, i) { let l = !1; for (let r = 0, o = t.length; r < o; r++) { let s = t[r], a = n && n[e.length], k; if (!(s == null || s === !0 || s === !1)) if ((k = typeof s) == "object" && s.nodeType) e.push(s); else if (Array.isArray(s)) l = F(e, s, a) || l; else if (k === "function") if (i) { for (; typeof s == "function"; ) s = s(); l = F(e, Array.isArray(s) ? s : [s], Array.isArray(a) ? a : [a]) || l; } else e.push(s), l = !0; else { const $ = String(s); a && a.nodeType === 3 && a.data === $ ? e.push(a) : e.push(document.createTextNode($)); } } return l; } function Y(e, t, n = null) { for (let i = 0, l = t.length; i < l; i++) e.insertBefore(t[i], n); } function P(e, t, n, i) { if (n === void 0) return e.textContent = ""; const l = i || document.createTextNode(""); if (t.length) { let r = !1; for (let o = t.length - 1; o >= 0; o--) { const s = t[o]; if (l !== s) { const a = s.parentNode === e; !r && !o ? a ? e.replaceChild(l, s) : e.insertBefore(l, n) : a && s.remove(); } else r = !0; } } else e.insertBefore(l, n); return [l]; } const ae = (e) => e != null && (e = Object.getPrototypeOf(e), e === Array.prototype || e === Object.prototype); function ce(e, t, n) { for (const [i, l] of Object.entries(t)) { const r = `${n}.${i}`; e[r] = l, ae(l) && ce(e, l, r); } } function Le(e) { const t = { ...e }; for (const [n, i] of Object.entries(e)) ae(i) && ce(t, i, n); return t; } const we = (e) => e; function _e(e, t = we) { return (n, ...i) => { n[0] === "." && (n = n.slice(1)); const l = e()?.[n]; switch (typeof l) { case "function": return l(...i); case "string": return t(l, i[0]); default: return l; } }; } const Ie = { insertLink: "Link einfügen", change: "Ändern", editor: { noResult: "Keine Ergebnisse gefunden", draft: "Entwurf", published: "Veröffentlicht", sheduled: "Geplant" }, dialog: { title: "Einen internen Link einfügen", removeLink: "Link entfernen", changeLink: "Einen anderen Link finden", ok: "OK" } }, Ee = { InternalLink: Ie }, Se = { 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" } }, Pe = { InternalLink: Se }, De = { insertLink: "Insertar enlace", change: "Cambiar", editor: { noResult: "No se encontraron resultados", draft: "Borrador", published: "Publicado", sheduled: "Programado" }, dialog: { title: "Insertar un enlace interno", removeLink: "Eliminar enlace", changeLink: "Encontrar otro enlace", ok: "OK" } }, Ce = { InternalLink: De }, Ae = { 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" } }, Ne = { InternalLink: Ae }, xe = { insertLink: "Inserisci link", change: "Cambia", editor: { noResult: "Nessun risultato trovato", draft: "Bozza", published: "Pubblicato", sheduled: "Programmato" }, dialog: { title: "Inserisci un link interno", removeLink: "Rimuovi link", changeLink: "Trova un altro link", ok: "OK" } }, Be = { InternalLink: xe }, Te = { insertLink: "Inserir link", change: "Alterar", editor: { noResult: "Nenhum resultado encontrado", draft: "Rascunho", published: "Publicado", sheduled: "Agendado" }, dialog: { title: "Inserir um link interno", removeLink: "Remover link", changeLink: "Encontrar outro link", ok: "OK" } }, Oe = { InternalLink: Te }, Z = { en: Pe, fr: Ne, es: Ce, de: Ee, it: Be, pt: Oe }; function W(e) { const t = D(() => { const i = e(), l = i in Z ? i : "en"; return Le(Z[l]); }); return { t: _e(t) }; } var Re = /* @__PURE__ */ m("<div class=ILResult>"), Ve = /* @__PURE__ */ m('<div class=ILNoResult><span class=ILNoResult-message></span><span class="pi pi-exclamation-circle">'), je = /* @__PURE__ */ m("<div class=InternalLinkEditor><form class=ILForm><input class=InternalLinkEditor-input type=text placeholder=Search>"), Me = /* @__PURE__ */ m("<div class=ILResultItem><div class=ILResultItem-title></div><span>"); function ue(e) { const { value: t, setValue: n, service: i } = e, [l, r] = E(""), [o, s] = E([]), { t: a } = W(e.language); G(async () => { const f = t(); if (!f) return; const g = await i.getDocument(f); r(g?.title ?? ""), await k(); }); const k = async (f) => { if (f?.preventDefault(), l().length < 3) return; const g = await i.searchDocuments({ searchString: l() }); s(g); }, $ = (f) => f && new Date(f).getTime() < Date.now(); return (() => { var f = je(), g = f.firstChild, c = g.firstChild; return g.addEventListener("submit", k), c.$$input = (d) => r(d.currentTarget.value), p(f, x(X, { when: o, get children() { var d = Re(); return p(d, () => o().map((u) => (() => { var h = Me(), w = h.firstChild, L = w.nextSibling; return h.$$click = () => { n(u.id); }, p(w, () => u.title), p(L, (() => { var O = _(() => !!u.ready); return () => O() ? _(() => !!$(u.publishDate))() ? a("InternalLink.editor.published") : a("InternalLink.editor.sheduled") : a("InternalLink.editor.draft"); })()), C(() => ye(L, `ILResultItem-status ${u.ready ? $(u.publishDate) ? "published" : "sheduled" : "draft"}`)), h; })())), d; } }), null), p(f, x(X, { get when() { return !o() || o().length === 0; }, get children() { var d = Ve(), u = d.firstChild; return p(u, () => a("InternalLink.editor.noResult")), d; } }), null), C(() => c.value = l()), f; })(); } Q(["input", "click"]); var Ue = /* @__PURE__ */ m("<div class=InternalLinkDialog-header><span class=InternalLinkDialog-title>"), Ke = /* @__PURE__ */ m('<button class="PlgBtn pi pi-arrow-left"type=button>'), He = /* @__PURE__ */ m("<div class=InternalLinkDialog-content>"), Fe = /* @__PURE__ */ m('<div class=ILPreview><div class=ILPreview-title></div><button class="PlgBtn outlined iconBtn pi pi-pencil"type=button>'), z = /* @__PURE__ */ m("<div>"), Ge = /* @__PURE__ */ m('<div><button class="PlgBtn primary"type=button>'), qe = /* @__PURE__ */ m('<button type=button class="PlgBtn danger outlined">'), ee = /* @__PURE__ */ m("<button class=PlgBtn type=button>"), Qe = /* @__PURE__ */ m('<button class="PlgBtn primary"type=button>'), We = /* @__PURE__ */ m('<div class=InternalLinkDialog-linkPreview><div></div><button class="PlgBtn iconBtn pi pi-pencil"type=button>'); function Xe({ service: e, setValue: t, value: n, language: i }) { const { t: l } = W(i), [r, o] = E(), [s, a] = E(!!n()); G(async () => { const c = n(); if (!c) return; const d = await e.getDocument(c); if (!d) return; const { title: u, typeName: h } = d; o({ title: u, typeName: h }); }); const k = (() => { var c = Ue(), d = c.firstChild; return p(c, (() => { var u = _(() => !!(n() && !s())); return () => u() && (() => { var h = Ke(); return h.$$click = () => { a(!0); }, h; })(); })(), d), p(d, () => l("InternalLink.dialog.title")), c; })(), $ = (() => { var c = He(); return p(c, (() => { var d = _(() => !!(s() && n())); return () => d() ? (() => { var u = Fe(), h = u.firstChild, w = h.nextSibling; return p(h, () => r()?.title), w.$$click = () => { const L = n(), O = r()?.typeName; !L || !O || (g.close(), e.openDocumentEdition(L, O)); }, u; })() : x(ue, { service: e, language: i, value: n, setValue: (u) => { t(u), a(!0), g.hide(); } }); })()), c; })(), f = (() => { var c = z(); return p(c, (() => { var d = _(() => !!s()); return () => d() ? (() => { var u = Ge(), h = u.firstChild; return p(u, (() => { var w = _(() => !!n()); return () => w() && [(() => { var L = qe(); return L.$$click = () => { t(void 0), g.hide(); }, p(L, () => l("InternalLink.dialog.removeLink")), L; })(), (() => { var L = ee(); return L.$$click = () => a(!1), p(L, () => l("InternalLink.dialog.changeLink")), L; })()]; })(), h), h.$$click = () => { g.hide(); }, p(h, () => l("InternalLink.dialog.ok")), u; })() : (() => { var u = Qe(); return u.$$click = () => { g.hide(); }, p(u, () => l("InternalLink.dialog.ok")), u; })(); })()), c; })(), g = e.createModalDialog({ content: $, footer: f, header: k }); return (() => { var c = z(); return p(c, (() => { var d = _(() => !!n()); return () => d() ? (() => { var u = We(), h = u.firstChild, w = h.nextSibling; return p(h, () => r()?.title), w.$$click = () => g.show(), u; })() : (() => { var u = ee(); return u.$$click = () => g.show(), p(u, () => l("InternalLink.insertLink")), u; })(); })()), c; })(); } Q(["click"]); var Je = /* @__PURE__ */ m("<div class=InternalLinkDialog-header><span class=InternalLinkDialog-title>"), Ye = /* @__PURE__ */ m('<button class="PlgBtn pi pi-arrow-left"type=button>'), Ze = /* @__PURE__ */ m("<div>"), ze = /* @__PURE__ */ m('<div><button class="PlgBtn primary"type=button>'), et = /* @__PURE__ */ m('<button type=button class="PlgBtn danger outlined">'), tt = /* @__PURE__ */ m("<button class=PlgBtn type=button>"), nt = /* @__PURE__ */ m('<button class="PlgBtn primary"type=button>'), it = /* @__PURE__ */ m("<div class=InternalLinkDialog-content>"), lt = /* @__PURE__ */ m('<div class=ILPreview><div class=ILPreview-title></div><button class="PlgBtn outlined iconBtn pi pi-pencil"type=button>'); async function rt({ value: e, getLanguage: t, service: n }) { const [i, l] = E(), { t: r } = W(t), [o, s] = E(!!e()); G(async () => { const f = e(); if (!f) return; const g = await n.getDocument(f); if (!g) return; const { title: c, typeName: d } = g; l({ title: c, typeName: d }); }); const a = (() => { var f = Je(), g = f.firstChild; return p(f, (() => { var c = _(() => !!(e() && !o())); return () => c() && (() => { var d = Ye(); return d.$$click = () => { s(!0); }, d; })(); })(), g), p(g, () => r("InternalLink.dialog.title")), f; })(), k = (() => { var f = Ze(); return p(f, (() => { var g = _(() => !!o()); return () => g() ? (() => { var c = ze(), d = c.firstChild; return p(c, (() => { var u = _(() => !!e()); return () => u() && [(() => { var h = et(); return h.$$click = () => $.close("remove"), p(h, () => r("InternalLink.dialog.removeLink")), h; })(), (() => { var h = tt(); return h.$$click = () => s(!1), p(h, () => r("InternalLink.dialog.changeLink")), h; })()]; })(), d), d.$$click = () => $.close(), p(d, () => r("InternalLink.dialog.ok")), c; })() : (() => { var c = nt(); return c.$$click = () => $.close(), p(c, () => r("InternalLink.dialog.ok")), c; })(); })()), f; })(), $ = n.createModalDialog({ content: (() => { var f = it(); return p(f, (() => { var g = _(() => !!(o() && e())); return () => g() ? (() => { var c = lt(), d = c.firstChild, u = d.nextSibling; return p(d, () => i()?.title), u.$$click = () => { const h = e(), w = i()?.typeName; !h || !w || ($.close(), n.openDocumentEdition(h, w)); }, c; })() : x(ue, { service: n, language: t, value: e, setValue: (c) => $.close(c) }); })()), f; })(), header: a, footer: k }); return await $.open(); } Q(["click"]); const ut = { create: ct, init: at }; function st(e) { e.executeHook("htmlEditorServiceProvider").forEach((n) => { n.registerInternalLinkPlugin(ot(e)); }); } function ot(e) { return { async getDocumentTitle(t) { return (await e.getDocument(t))?.title; }, async openDialog(t, n) { const i = await rt({ value: () => t, getLanguage: n.getLanguage, service: e }); if (t) { if (!i) return; if (i === "remove") return { documentId: void 0 }; } return { documentId: i }; } }; } function at({ pluginBaseUrl: e, addAfterInitListener: t }) { const n = `${e}/admin-ui-plugin.css`, i = document.createElement("link"); i.rel = "stylesheet", i.href = n, document.head.appendChild(i), t(st); } function ct(e) { const { fieldType: t, initialValue: n } = e; if (t.dataType !== "string") throw new Error(`field type '${t.name}' is incompatible with internal-link plugin, needs data type 'string'`); if (n !== void 0 && typeof n != "string") throw new Error(`field type '${t.name}' is incompatible, should be a string, got '${typeof n}'`); return he((i) => { const [l, r] = E(n ?? ""), [o, s] = E(e.language); return { element: x(Xe, { language: o, value: l, setValue: (a) => { r(a ?? ""), e.setModifiedValue(a ?? null); }, service: e }), setLanguage: s, setValue: r, getValue() { return l(); }, dispose: i }; }); } export { ut as default };