From ffeca7f94df4ce87a03677be47128e0ba7e4a70d Mon Sep 17 00:00:00 2001 From: Yassine Guedidi Date: Sun, 2 Mar 2025 14:07:15 +0100 Subject: [PATCH] Extract Annotations controller --- assets/controllers/annotations_controller.js | 57 ++++++++++++++++++++ assets/index.js | 41 -------------- templates/Entry/entry.html.twig | 23 ++++---- 3 files changed, 66 insertions(+), 55 deletions(-) create mode 100644 assets/controllers/annotations_controller.js diff --git a/assets/controllers/annotations_controller.js b/assets/controllers/annotations_controller.js new file mode 100644 index 000000000..1fd4308f6 --- /dev/null +++ b/assets/controllers/annotations_controller.js @@ -0,0 +1,57 @@ +import { Controller } from '@hotwired/stimulus'; +import annotator from 'annotator'; + +export default class extends Controller { + static values = { + entryId: Number, + createUrl: String, + updateUrl: String, + destroyUrl: String, + searchUrl: String, + }; + + connect() { + this.app = new annotator.App(); + + this.app.include(annotator.ui.main, { + element: this.element, + }); + + const authorization = { + permits() { return true; }, + }; + this.app.registry.registerUtility(authorization, 'authorizationPolicy'); + + this.app.include(annotator.storage.http, { + prefix: '', + urls: { + create: this.createUrlValue, + update: this.updateUrlValue, + destroy: this.destroyUrlValue, + search: this.searchUrlValue, + }, + entryId: this.entryIdValue, + onError(msg, xhr) { + if (!Object.prototype.hasOwnProperty.call(xhr, 'responseJSON')) { + annotator.notification.banner('An error occurred', 'error'); + return; + } + Object.values(xhr.responseJSON.children).forEach((v) => { + if (v.errors) { + Object.values(v.errors).forEach((errorText) => { + annotator.notification.banner(errorText, 'error'); + }); + } + }); + }, + }); + + this.app.start().then(() => { + this.app.annotations.load({ entry: this.entryIdValue }); + }); + } + + disconnect() { + this.app.destroy(); + } +} diff --git a/assets/index.js b/assets/index.js index 34ec07248..1f8d3256d 100755 --- a/assets/index.js +++ b/assets/index.js @@ -6,9 +6,6 @@ import $ from 'jquery'; import '@materializecss/materialize/dist/css/materialize.css'; import '@materializecss/materialize/dist/js/materialize'; -/* Annotations */ -import annotator from 'annotator'; - import ClipboardJS from 'clipboard'; import 'mathjax/es5/tex-svg'; @@ -33,45 +30,7 @@ import './js/shortcuts/entry'; /* Theme style */ import './scss/index.scss'; -/* ========================================================================== - Annotations - ========================================================================== */ - $(document).ready(() => { - if ($('#article').length) { - const app = new annotator.App(); - - app.include(annotator.ui.main, { - element: document.querySelector('article'), - }); - - const authorization = { - permits() { return true; }, - }; - app.registry.registerUtility(authorization, 'authorizationPolicy'); - - const x = JSON.parse($('#annotationroutes').html()); - app.include(annotator.storage.http, $.extend({}, x, { - onError(msg, xhr) { - if (!Object.prototype.hasOwnProperty.call(xhr, 'responseJSON')) { - annotator.notification.banner('An error occurred', 'error'); - return; - } - $.each(xhr.responseJSON.children, (k, v) => { - if (v.errors) { - $.each(v.errors, (n, errorText) => { - annotator.notification.banner(errorText, 'error'); - }); - } - }); - }, - })); - - app.start().then(() => { - app.annotations.load({ entry: x.entryId }); - }); - } - document.querySelectorAll('[data-handler=tag-rename]').forEach((item) => { const current = item; current.wallabag_edit_mode = false; diff --git a/templates/Entry/entry.html.twig b/templates/Entry/entry.html.twig index d851e97b2..a42a0723d 100644 --- a/templates/Entry/entry.html.twig +++ b/templates/Entry/entry.html.twig @@ -326,7 +326,15 @@ {% endif %} - +
{{ entry.content|raw }}
@@ -347,19 +355,6 @@ - - - {% endblock %} {% block footer %}