ldc-boutique-cnc/resources/ts/components/CkEditorComponent.vue

274 lines
7 KiB
Vue

<template>
<div>
<div ref="wordCounter" />
<div ref="ckeditor" />
<input
:id="inTid"
class="form-control"
type="hidden"
:name="inTname"
:value="intText"
:required="intRequired"
@change="domInputUpdated"
>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import "../../js/vendor/ckeditor/build/ckeditor";
import trans from "../modules/trans";
// import { CKUploadAdaptater } from "../../bo/CkUploadAdaptater";
export default defineComponent({
name: "CkEditorComponent",
emits: ["input"],
mixins: [trans],
props: {
id: {
type: String,
default: String(Math.pow(10, 16) / Math.random()),
},
required: {
type: Boolean,
default: false,
},
name: {
type: String,
default: "",
},
dbtext: {
type: String,
default: "",
},
},
beforeMount() {
if (!this.$attrs.json) {
throw new Error("this component requires json");
}
},
mounted() {
const json = String(this.$attrs.json ?? "{}"),
data = JSON.parse(json);
this.inTid = data.id ?? this.id;
this.inTname = data.name ?? this.name;
this.intText = data.text ? String(data.text) : this.dbtext;
this.intRequired = Boolean(data.required ?? this.required);
this.makeSuperBuild();
},
data(): {
// @ts-ignore
editor: ClassicEditor | null;
inTid: string;
inTname: string;
intText: string;
intRequired: boolean;
words: number;
chars: number;
} {
return {
inTname: "",
intText: "",
inTid: "",
intRequired: false,
editor: null,
words: 0,
chars: 0,
};
},
watch: {
dbtext() {
if (this.intText != this.dbtext) {
this.intText = this.dbtext;
if (this.editor) {
this.editor.setData(this.intText);
}
}
},
},
methods: {
makeSuperBuild() {
const self = this,
ckeditor = this.$refs.ckeditor as HTMLElement,
wordCounter = this.$refs.wordCounter as HTMLElement,
verticalTopMenuOffset = document.getElementById(
"top-nav-bar"
) as HTMLElement | null;
if (!verticalTopMenuOffset) {
throw new Error("Could not find bo top-nav-bar");
}
// @ts-ignore Do this for Crud policies to use our own editor
window.ClassicEditor = ClassicEditor;
// @ts-ignore
ClassicEditor.create(ckeditor, {
extraPlugins: [
// // @ts-ignore
// (editor) => {
// editor.plugins.get("FileRepository").createUploadAdapter = (
// // @ts-ignore
// loader
// ) => {
// // Configure the URL to the upload script in your back-end here!
// return new CKUploadAdaptater(loader);
// };
// },
],
wordCount: {
container: wordCounter,
// @ts-ignore
onUpdate: (stats) => {
this.chars = stats.characters;
this.words = stats.words;
},
},
link: {
// Automatically add target="_blank" and rel="noopener noreferrer" to all external links.
addTargetToExternalLinks: true,
// Let the users control the "download" attribute of each link.
decorators: [
{
mode: "manual",
label: "Ouvrir dans un nouvel onglet",
attributes: {
target: "_blank",
rel: "noopener noreferrer",
},
},
],
},
mediaEmbed: {
previewsInData: false,
removeProviders: [
"instagram",
"twitter",
"googleMaps",
"flickr",
"facebook",
"dailymotion",
"spotify",
],
},
toolbar: {
items: [
"heading",
"|",
"bold",
"italic",
"underline",
"link",
"bulletedList",
"numberedList",
"|",
"outdent",
"indent",
"alignment",
"|",
"undo",
"redo",
"|",
"blockQuote",
"|",
// "insertTable",
// "tableColumn",
// "tableRow",
// "mergeTableCells",
"|",
// "fontColor",
// "fontFamily",
"fontSize",
"horizontalLine",
"|",
"removeFormat",
"|",
"sourceEditing",
],
},
ui: {
viewportOffset: {
top: verticalTopMenuOffset.getBoundingClientRect().height,
right: 0,
bottom: 0,
left: 0,
},
},
// table: {
// contentToolbar: [
// "tableColumn",
// "tableRow",
// "mergeTableCells",
// "tableProperties",
// "tableCellProperties",
// "toggleTableCaption",
// ],
// },
width: "auto",
language: "fr",
})
// @ts-ignore
.then((editor: ClassicEditor) => {
self.editor = Object.freeze(editor);
editor.setData(this.intText);
editor.model.document.on("change:data", () => {
// * Keep track of internal editor
let data = editor.getData();
self.intText = data;
self.$emit("input", data);
});
})
// @ts-ignore
.catch((error) => {
console.error("Oops, something went wrong!");
console.error(
"Please, report the following error on https://github.com/ckeditor/ckeditor5/issues with the build id and the error stack trace:"
);
console.warn("Build id: x0tjc6omqq4d-iydt761tuj51");
console.error(error);
});
},
updateCkEditorText(text: string): void {
if (this.editor) {
// https://stackoverflow.com/questions/65693108/threejs-component-working-in-vuejs-2-but-not-3/65732553#65732553
// @ts-ignore NON reactive prop
this.editor.setData(text);
}
},
domInputUpdated(e: Event) {
const target = e.target as HTMLInputElement | null;
if (target) {
this.updateCkEditorText(target.value);
}
},
},
});
</script>
<style lang="scss">
@import url("./../../js/vendor/ckeditor/build/styles.css");
.ck-rounded-corners {
border-radius: 0 0 0.375rem 0.375rem !important;
}
.ck-word-count {
background-color: #fafafa;
border: 1px solid #c4c4c4;
border-bottom: 0px;
border-top-left-radius: 0.375rem;
border-top-right-radius: 0.375rem;
color: initial;
display: flex;
flex-direction: row;
justify-content: space-around !important;
}
.ck-content {
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
word-break: break-word;
}
.ck-focused {
border-color: #86b7fe !important;
box-shadow: 0 0 0 0.25rem rgb(13 110 253 / 25%) !important;
outline: 0 !important;
}
</style>