Part-DB-server/assets/controllers/elements/ckeditor_controller.js

132 lines
4.5 KiB
JavaScript
Raw Normal View History

2022-11-29 21:21:26 +01:00
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {Controller} from "@hotwired/stimulus";
import { default as FullEditor } from "../../ckeditor/markdown_full";
import { default as SingleLineEditor} from "../../ckeditor/markdown_single_line";
2022-07-26 21:23:31 +02:00
import { default as HTMLLabelEditor } from "../../ckeditor/html_label";
import {EditorWatchdog} from 'ckeditor5';
2025-08-03 20:00:36 +02:00
import "ckeditor5/ckeditor5.css";;
import "../../css/components/ckeditor.css";
const translationContext = require.context(
'ckeditor5/translations',
false,
//Only load the translation files we will really need
/(de|it|fr|ru|ja|cs|da|zh|pl|hu)\.js$/
);
function loadTranslation(language) {
if (!language || language === 'en') {
return null;
}
const lang = language.slice(0, 2);
const path = `./${lang}.js`;
if (translationContext.keys().includes(path)) {
const module = translationContext(path);
return module.default;
} else {
return null;
}
}
/* stimulusFetch: 'lazy' */
export default class extends Controller {
connect() {
const mode = this.element.dataset.mode;
let EDITOR_TYPE = "Invalid";
2022-07-26 21:23:31 +02:00
switch (mode) {
case "markdown-full":
EDITOR_TYPE = FullEditor['Editor'];
2022-07-26 21:23:31 +02:00
break;
case "markdown-single_line":
EDITOR_TYPE = SingleLineEditor['Editor'];
2022-07-26 21:23:31 +02:00
break;
case "html-label":
EDITOR_TYPE = HTMLLabelEditor['Editor'];
break;
default:
console.error("Unknown mode: " + mode);
return;
}
const language = document.body.dataset.locale ?? "en";
const emojiURL = new URL('../../ckeditor/emojis.json', import.meta.url).href;
2022-07-26 11:50:27 +02:00
const config = {
language: language,
2024-12-29 19:31:04 +01:00
licenseKey: "GPL",
emoji: {
definitionsUrl: emojiURL
}
2022-07-26 11:50:27 +02:00
}
//Load translations if not english
let translations = loadTranslation(language);
if (translations) {
//Keep existing translations (e.g. from other plugins), if any
config.translations = [window.CKEDITOR_TRANSLATIONS, translations];
}
2022-07-26 21:23:31 +02:00
const watchdog = new EditorWatchdog();
watchdog.setCreator((elementOrData, editorConfig) => {
return EDITOR_TYPE.create(elementOrData, editorConfig)
.then(editor => {
if(this.element.disabled) {
editor.enableReadOnlyMode("readonly");
}
//Apply additional styles
const editor_div = editor.ui.view.element;
const new_classes = this.element.dataset.ckClass;
if (editor_div && new_classes) {
editor_div.classList.add(...new_classes.split(","));
}
Added feature for part IPN suggest with category prefixes (#1054) * Erweiterungstätigkeiten zur IPN-Vorschlagsliste anhand von Präfixen aus den Kategorien * Umstellung Migrationen bzgl. Multi-Plattform-Support. Zunächst MySQL, SQLite Statements integrieren. * Postgre Statements integrieren * SQL-Formatierung in Migration verbessern * Erweitere IPN-Suggest um Bauteilbeschreibung. Die Implementierung berücksichtigt nun zusätzlich die Bauteilbeschreibung zu maximal 150 Zeichen Länge für die Generierung von IPN-Vorschlägen und Inkrementen. * Anpassungen aus Analyse vornehmen * IPN-Validierung für Parts überarbeiten * IPN-Vorschlagslogik um Konfiguration erweitert * Anpassungen aus phpstan Analyse * IPN-Vorschlagslogik erweitert und Bauteil-IPN vereindeutigt Die IPN-Logik wurde um eine Konfiguration zur automatischen Suffix-Anfügung und die Berücksichtigung von doppelten Beschreibungen bei Bedarf ergänzt. Zudem wurde das Datenmodell angepasst, um eine eindeutige Speicherung der IPN zu gewährleisten. * Regex-Konfigurationsmöglichkeit für IPN-Vorschläge einführen Die Einstellungen für die IPN-Vorschlagslogik wurden um eine Regex-Validierung und eine Hilfetext-Konfiguration erweitert. Tests und Änderungen an den Formularoptionen wurden implementiert. * Match range assert and form limits in suggestPartDigits * Keep existing behavior with autoAppend suffix by default * Show the regex hint in the browser validation notice. * Improved translations * Removed unnecessary service definition * Removed german comments --------- Co-authored-by: Marcel Diegelmann <marcel.diegelmann@gmail.com> Co-authored-by: Jan Böhmer <mail@jan-boehmer.de>
2025-11-03 00:31:47 +01:00
// Automatic synchronization of source input
editor.model.document.on("change:data", () => {
editor.updateSourceElement();
// Dispatch the input event for further treatment
const event = new Event("input");
this.element.dispatchEvent(event);
});
//This return is important! Otherwise we get mysterious errors in the console
//See: https://github.com/ckeditor/ckeditor5/issues/5897#issuecomment-628471302
return editor;
})
.catch(error => {
console.error(error);
});
});
2022-07-26 11:50:27 +02:00
watchdog.create(this.element, config).catch(error => {
console.error(error);
2022-07-26 11:50:27 +02:00
});
}
}