diff --git a/bibcite_footnotes.libraries.yml b/bibcite_footnotes.libraries.yml index 5babc75..2eeb95b 100644 --- a/bibcite_footnotes.libraries.yml +++ b/bibcite_footnotes.libraries.yml @@ -26,6 +26,7 @@ footnote_picker: - core/ckeditor5 - core/drupal - core/drupal.dialog + - core/drupalSettings - core/jquery slick: diff --git a/css/footnote_picker.admin.css b/css/footnote_picker.admin.css index 5fda10e..69c2a53 100644 --- a/css/footnote_picker.admin.css +++ b/css/footnote_picker.admin.css @@ -3,3 +3,17 @@ background-image: url("../icons/footnotes.svg"); } + +/* Editing-only marker. Saved HTML remains . */ +.ck-content .bibcite-footnote-widget { + display: inline-block; + padding: 0 .3em; + margin: 0 .12em; + border: 1px solid #8aa3bd; + border-radius: 3px; + background: #eaf3fc; + color: #17446e; + font-size: .85em; + cursor: pointer; + white-space: nowrap; +} diff --git a/js/ckeditor5_plugins/footnotepicker2/build/footnotepicker2.js b/js/ckeditor5_plugins/footnotepicker2/build/footnotepicker2.js index 0cfe96e..5d51eb0 100644 --- a/js/ckeditor5_plugins/footnotepicker2/build/footnotepicker2.js +++ b/js/ckeditor5_plugins/footnotepicker2/build/footnotepicker2.js @@ -1,13 +1,4 @@ -/** - * Prebuilt CKEditor 5 plugin bundle for Drupal (sandbox quality). - * - * Provides a toolbar button "Footnote" that opens a Drupal dialog asking for: - * - Reference ID / key (free text for now) - * - Optional page - * Then inserts: - * - * Replace this with a proper build + entity autocomplete later. - */ +/* Drupal CKEditor 5 footnote widget: browser-ready bundle, no build required. */ (function () { const w = window; @@ -15,11 +6,12 @@ w.CKEditor5 = w.CKEditor5 || {}; w.CKEditor5.footnotepicker2 = w.CKEditor5.footnotepicker2 || {}; - const Plugin = (w.CKEditor5 && w.CKEditor5.core && w.CKEditor5.core.Plugin) || null; - const ButtonView = (w.CKEditor5 && w.CKEditor5.ui && w.CKEditor5.ui.ButtonView) || null; + const { Plugin, ButtonView, Widget, toWidget, viewToModelPositionOutsideModelElement, DomEventObserver } = w.CKEDITOR || { + ...w.CKEditor5.core, ...w.CKEditor5.ui, ...w.CKEditor5.widget, ...w.CKEditor5.engine + }; - if (!Plugin || !ButtonView) { + if (!Plugin || !ButtonView || !Widget || !toWidget || !viewToModelPositionOutsideModelElement || !DomEventObserver) { // eslint-disable-next-line no-console console.warn('[bibcite_footnotes] CKEditor5 globals not found; FootnotePicker2 not registered.'); return; @@ -83,16 +75,17 @@ } - function openFootnoteDialog(onSubmit) { + function openFootnoteDialog(onSubmit, initial = {}) { const $ = w.jQuery; const Drupal = w.Drupal; // Fallback: prompt() if dialog isn't available. if (!Drupal || !Drupal.dialog || !$) { - const reference = w.prompt('Reference ID (optional):', '') || ''; - const text = w.prompt('Free-form footnote text (optional):', '') || ''; - const page = w.prompt('Page(s) (optional):', '') || ''; - const value = w.prompt('Value (optional):', '') || ''; + const reference = w.prompt('Reference ID (optional):', initial.reference || ''); + const text = w.prompt('Free-form footnote text (optional):', initial.text || ''); + const page = w.prompt('Page(s) (optional):', initial.page || ''); + const value = w.prompt('Value (optional):', initial.value || ''); + if ([reference, text, page, value].some(v => v === null)) return; onSubmit({reference: reference.trim(), text: text.trim(), page: page.trim(), value: value.trim()}); return; } @@ -107,6 +100,14 @@ `); + const references = w.drupalSettings?.bibcite_footnotes?.references || []; + for (const [label, id] of references) { + $ref.append($(''; - - let viewFragment, modelFragment; - - try { - viewFragment = editor.data.processor.toView(html); - modelFragment = editor.data.toModel(viewFragment); - } catch (e) { - console.error('[footnotes] toModel failed', e); - return; - } - - console.log('[footnotes] inserting html:', html); - console.log('[footnotes] modelFragment childCount:', modelFragment ? modelFragment.childCount : null); - - - editor.model.change((writer) => { - // If got stripped, modelFragment will be empty. - if (!modelFragment || modelFragment.childCount === 0) { - console.warn('[footnotes] was stripped; inserting as plain text fallback'); - editor.model.insertContent(writer.createText(html), editor.model.document.selection); - return; - } - - editor.model.insertContent(modelFragment, editor.model.document.selection); - }); - - }); + const selected = model.document.selection.getSelectedElement(); + this.open(selected && selected.is('element', modelName) ? selected : null); }); - - return button; }); } - } + open(element) { + const editor = this.editor; + if (editor.isReadOnly) return; + const model = editor.model; + const initial = element ? { + editing: true, + reference: element.getAttribute('reference') || '', + page: element.getAttribute('page') || '', + value: element.getAttribute('value') || '', + text: element.getAttribute('body') || '', + } : {}; + openFootnoteDialog(({ reference, text, page, value }) => { + if (editor.isReadOnly) return; + const attrs = { reference, page, value, body: element && text === initial.text ? initial.text : sanitizeFootnoteHtml(text) }; + model.change(writer => { + if (element) { + if (element.root.rootName === '$graveyard' || !element.parent) return; + for (const key of attributes) { + if (attrs[key] || key === 'body') writer.setAttribute(key, attrs[key], element); + else writer.removeAttribute(key, element); + } + writer.setSelection(element, 'on'); + } else { + const newElement = writer.createElement(modelName, Object.fromEntries( + Object.entries(attrs).filter(([key, val]) => val || key === 'body') + )); + model.insertObject(newElement, null, null, { setSelection: 'on' }); + } + }); + editor.editing.view.focus(); + }, initial); + } + } w.CKEditor5.footnotepicker2.FootnotePicker2 = FootnotePicker2; })(); diff --git a/js/ckeditor5_plugins/footnotepicker2/src/plugin.js b/js/ckeditor5_plugins/footnotepicker2/src/plugin.js index 2175f81..173e0d8 100644 --- a/js/ckeditor5_plugins/footnotepicker2/src/plugin.js +++ b/js/ckeditor5_plugins/footnotepicker2/src/plugin.js @@ -1,31 +1,332 @@ -import { Plugin } from 'ckeditor5/src/core'; -import { ButtonView } from 'ckeditor5/src/ui'; - -/** - * Minimal citation picker for Drupal CKEditor5 (sandbox). - * - * NOTE: This source file is not used by Drupal unless you build it. - * The build output lives in ../build/footnotepicker2.js - */ - - -export default class FootnotePicker2 extends Plugin { - init() { - const editor = this.editor; - - editor.ui.componentFactory.add('footnotePicker2', (locale) => { - const button = new ButtonView(locale); - button.set({ - label: 'Reference Footnotes', - withText: true, - tooltip: 'Insert reference footnote', - }); - - button.on('execute', () => { - // Implemented in the built file for now. - }); - - return button; - }); - } -} +/* Browser-ready source. Copy to ../build/footnotepicker2.js after changes. */ +(function () { + const w = window; + + // Guard: CKEditor5 globals must exist. + w.CKEditor5 = w.CKEditor5 || {}; + w.CKEditor5.footnotepicker2 = w.CKEditor5.footnotepicker2 || {}; + + const { Plugin, ButtonView, Widget, toWidget, viewToModelPositionOutsideModelElement, DomEventObserver } = w.CKEDITOR || { + ...w.CKEditor5.core, ...w.CKEditor5.ui, ...w.CKEditor5.widget, ...w.CKEditor5.engine + }; + + + if (!Plugin || !ButtonView || !Widget || !toWidget || !viewToModelPositionOutsideModelElement || !DomEventObserver) { + // eslint-disable-next-line no-console + console.warn('[bibcite_footnotes] CKEditor5 globals not found; FootnotePicker2 not registered.'); + return; + } + // Sanitize ONLY the footnote body (not the article). + // Allows: , , and plain text. + function sanitizeFootnoteHtml(input) { + if (!input) return ''; + + const allowedTags = new Set(['STRONG', 'EM', 'A']); + const allowedAttrs = { + A: new Set(['href', 'title', 'target', 'rel']), + }; + + const template = document.createElement('template'); + template.innerHTML = input; + + const cleanNode = (node) => { + // Remove comments. + if (node.nodeType === Node.COMMENT_NODE) { + node.remove(); + return; + } + + // Text nodes are fine. + if (node.nodeType === Node.TEXT_NODE) return; + + // Element nodes. + if (node.nodeType === Node.ELEMENT_NODE) { + // Strip disallowed elements but keep their text content. + if (!allowedTags.has(node.tagName)) { + const textNode = document.createTextNode(node.textContent || ''); + node.replaceWith(textNode); + return; + } + + // Strip disallowed attributes. + [...node.attributes].forEach((attr) => { + const name = attr.name.toLowerCase(); + const ok = allowedAttrs[node.tagName] && allowedAttrs[node.tagName].has(name); + if (!ok) node.removeAttribute(attr.name); + }); + + // Extra link safety. + if (node.tagName === 'A') { + const href = node.getAttribute('href') || ''; + // Allow only http(s), mailto, and relative URLs. + if (!/^(https?:|mailto:|\/)/i.test(href)) { + node.removeAttribute('href'); + } + node.setAttribute('rel', 'noopener noreferrer'); + } + } + + // Recurse. + [...node.childNodes].forEach(cleanNode); + }; + + [...template.content.childNodes].forEach(cleanNode); + return template.innerHTML; + } + + + function openFootnoteDialog(onSubmit, initial = {}) { + const $ = w.jQuery; + const Drupal = w.Drupal; + + // Fallback: prompt() if dialog isn't available. + if (!Drupal || !Drupal.dialog || !$) { + const reference = w.prompt('Reference ID (optional):', initial.reference || ''); + const text = w.prompt('Free-form footnote text (optional):', initial.text || ''); + const page = w.prompt('Page(s) (optional):', initial.page || ''); + const value = w.prompt('Value (optional):', initial.value || ''); + if ([reference, text, page, value].some(v => v === null)) return; + onSubmit({reference: reference.trim(), text: text.trim(), page: page.trim(), value: value.trim()}); + return; + } + + const $wrapper = $('
'); + + // Row 1: reference selector + $wrapper.append('
'); + const $ref = $(` + + `); + const references = w.drupalSettings?.bibcite_footnotes?.references || []; + for (const [label, id] of references) { + $ref.append($('