426 lines
11 KiB
Vue
426 lines
11 KiB
Vue
<template>
|
|
<div
|
|
ref="ckEditorComponent"
|
|
class="ck-editor-component"
|
|
>
|
|
<div ref="wordCounter" />
|
|
<div ref="ckEditor" />
|
|
<input
|
|
ref="hiddenInput"
|
|
:id="id"
|
|
class="form-control"
|
|
type="hidden"
|
|
:name="name"
|
|
:value="text"
|
|
:required="required"
|
|
>
|
|
</div>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import { ClassicEditor, Editor, type EditorConfig } from "ckeditor5";
|
|
import {
|
|
computed,
|
|
onBeforeUnmount,
|
|
onMounted,
|
|
ref,
|
|
shallowRef,
|
|
useAttrs,
|
|
type PropType,
|
|
} from "vue";
|
|
|
|
import { builtinPlugins, frTranslations } from "@modules/ckeditor";
|
|
import { uniqueId } from "@modules/uniqueid";
|
|
import parseLaravelErrorMessages from "../../modules/parseLaravelErrorMessages";
|
|
|
|
import CkUploadAdapterPlugin from "@bo/ckUploadAdapter";
|
|
import "ckeditor5/ckeditor5.css";
|
|
|
|
defineOptions({
|
|
name: "CkEditorComponent",
|
|
});
|
|
|
|
// * EMITS
|
|
const emits = defineEmits<{
|
|
input: [value: string];
|
|
}>();
|
|
|
|
// * ATTRIBUTES
|
|
const attrs = useAttrs();
|
|
|
|
// * REFS
|
|
const ckEditorComponent = ref<HTMLDivElement | null>(null);
|
|
const ckEditor = ref<HTMLDivElement | null>(null);
|
|
const wordCounter = ref<HTMLDivElement | null>(null);
|
|
const hiddenInput = ref<HTMLInputElement | null>(null);
|
|
const editorInstance = shallowRef<Editor | null>(null);
|
|
|
|
// * PROPS
|
|
const props = defineProps({
|
|
id: {
|
|
type: String,
|
|
default: () => uniqueId(),
|
|
},
|
|
name: {
|
|
type: String,
|
|
default: "",
|
|
},
|
|
dbtext: {
|
|
type: String,
|
|
default: "",
|
|
},
|
|
placeholder: {
|
|
type: String,
|
|
default: "",
|
|
},
|
|
required: {
|
|
type: Boolean,
|
|
default: false,
|
|
},
|
|
errors: {
|
|
type: Object as PropType<Record<string, string[]> | unknown[]>,
|
|
default: () => {},
|
|
},
|
|
editorConfig: {
|
|
type: String,
|
|
default: "basic",
|
|
},
|
|
modelName: {
|
|
type: String,
|
|
default: "",
|
|
},
|
|
});
|
|
|
|
// * DATA
|
|
const id = ref<string>(props.id);
|
|
const name = ref<string>(props.name);
|
|
const text = ref<string>(props.dbtext);
|
|
const placeholder = ref<string>(props.placeholder);
|
|
const required = ref<boolean>(props.required);
|
|
const allErrors = ref(props.errors);
|
|
const editorConfig = ref<string>(props.editorConfig);
|
|
const modelName = ref<string>(props.modelName);
|
|
|
|
// * COMPUTED
|
|
const isUsedWithProps = computed<boolean>(() => attrs.json === undefined);
|
|
|
|
// * LIFECYCLE
|
|
onMounted(() => {
|
|
const json = String(attrs.json ?? "{}");
|
|
try {
|
|
const data = JSON.parse(json);
|
|
if (!isUsedWithProps.value) {
|
|
if (data.id) id.value = String(data.id);
|
|
if (data.name) name.value = String(data.name);
|
|
if (data.text) text.value = String(data.text);
|
|
if (data.placeholder) placeholder.value = String(data.placeholder);
|
|
if (data.modelName) modelName.value = String(data.modelName);
|
|
if (data.errors) allErrors.value = data.errors;
|
|
if (data.required !== undefined) required.value = Boolean(data.required);
|
|
if (data.editorConfig) editorConfig.value = String(data.editorConfig);
|
|
}
|
|
} catch (e) {
|
|
console.error("Erreur lors du parsing JSON des attributs:", e);
|
|
}
|
|
|
|
// Écoute des mises à jour externes programmatiques
|
|
hiddenInput.value?.addEventListener("ss:ck:set-data", onExternalSetData as EventListener);
|
|
|
|
initializeEditor();
|
|
});
|
|
|
|
onBeforeUnmount(() => {
|
|
hiddenInput.value?.removeEventListener("ss:ck:set-data", onExternalSetData as EventListener);
|
|
|
|
if (editorInstance.value) {
|
|
void editorInstance.value.destroy();
|
|
editorInstance.value = null;
|
|
}
|
|
});
|
|
|
|
// * METHODS
|
|
function initializeEditor(): void {
|
|
const verticalTopMenuOffset = document.querySelector("body > #top-navbar") as HTMLElement | null;
|
|
if (!verticalTopMenuOffset) {
|
|
console.warn("Could not find #top-navbar");
|
|
}
|
|
|
|
// Configuration partagée
|
|
const sharedConfig: EditorConfig = {
|
|
plugins: builtinPlugins,
|
|
extraPlugins: [CkUploadAdapterPlugin],
|
|
translations: [frTranslations],
|
|
language: "fr",
|
|
placeholder: placeholder.value,
|
|
ui: verticalTopMenuOffset
|
|
? {
|
|
viewportOffset: {
|
|
top: verticalTopMenuOffset.getBoundingClientRect().height,
|
|
right: 0,
|
|
bottom: 0,
|
|
left: 0,
|
|
},
|
|
}
|
|
: undefined,
|
|
wordCount: {
|
|
container: wordCounter.value as HTMLElement,
|
|
onUpdate: (stats: { characters: number; words: number }) => {
|
|
const wordCounterNode = wordCounter.value?.querySelector(".ck-word-count__words");
|
|
if (wordCounterNode) {
|
|
const spanWords = document.createElement("span");
|
|
spanWords.classList.add("fw-bold");
|
|
spanWords.textContent = String(stats.words);
|
|
wordCounterNode.textContent = "Mots : ";
|
|
wordCounterNode.appendChild(spanWords);
|
|
}
|
|
|
|
const characterCounterNode = wordCounter.value?.querySelector(".ck-word-count__characters");
|
|
if (characterCounterNode) {
|
|
const spanCharacters = document.createElement("span");
|
|
spanCharacters.classList.add("fw-bold");
|
|
spanCharacters.textContent = String(stats.characters);
|
|
characterCounterNode.textContent = "Caractères : ";
|
|
characterCounterNode.appendChild(spanCharacters);
|
|
}
|
|
},
|
|
},
|
|
typing: {
|
|
transformations: {
|
|
include: [],
|
|
extra: [
|
|
{ from: ":)", to: "😊" },
|
|
{ from: ":-P", to: "😛" },
|
|
{ from: "8-)", to: "😎" },
|
|
{ from: ";-)", to: "😉" },
|
|
{ from: ":-D", to: "😄" },
|
|
{ from: ":twisted:", to: "😈" },
|
|
{ from: ":mrgreen:", to: "😁" },
|
|
{ from: ":lol:", to: "😆" },
|
|
{ from: ":roll:", to: "🙄" },
|
|
{ from: ":-?", to: "😕" },
|
|
{ from: ":cry:", to: "😢" },
|
|
{ from: ":-o", to: "😮" },
|
|
{ from: ":evil:", to: "😡" },
|
|
{ from: ":-|", to: "😐" },
|
|
{ from: ":oops:", to: "😳" },
|
|
{ from: ":-x", to: "😠" },
|
|
{ from: "8-O", to: "😲" },
|
|
{ from: ":-(", to: "😧" },
|
|
{ from: ":arrow:", to: "➡" },
|
|
{ from: ":idea:", to: "💡" },
|
|
],
|
|
},
|
|
},
|
|
link: {
|
|
addTargetToExternalLinks: true,
|
|
decorators: {
|
|
openInNewTab: {
|
|
mode: "manual",
|
|
label: "Ouvrir dans un nouvel onglet",
|
|
attributes: { target: "_blank" },
|
|
},
|
|
},
|
|
},
|
|
mediaEmbed: {
|
|
previewsInData: false,
|
|
removeProviders: [
|
|
"dailymotion",
|
|
"spotify",
|
|
"vimeo",
|
|
"youtube",
|
|
"instagram",
|
|
"twitter",
|
|
"googleMaps",
|
|
"flickr",
|
|
"facebook",
|
|
],
|
|
},
|
|
};
|
|
|
|
// Configurations de barre d'outils
|
|
const minimalToolbar = [
|
|
"findAndReplace",
|
|
"|",
|
|
"bold",
|
|
"italic",
|
|
"underline",
|
|
"strikethrough",
|
|
"|",
|
|
"undo",
|
|
"redo",
|
|
"selectAll",
|
|
"|",
|
|
"removeFormat",
|
|
];
|
|
|
|
const basicToolbar = [
|
|
"heading",
|
|
"|",
|
|
"findAndReplace",
|
|
"|",
|
|
"bold",
|
|
"italic",
|
|
"underline",
|
|
"strikethrough",
|
|
"link",
|
|
"bulletedList",
|
|
"numberedList",
|
|
"|",
|
|
"outdent",
|
|
"indent",
|
|
"alignment",
|
|
"|",
|
|
"undo",
|
|
"redo",
|
|
"selectAll",
|
|
"|",
|
|
"blockQuote",
|
|
"|",
|
|
"fontSize",
|
|
"horizontalLine",
|
|
"|",
|
|
"removeFormat",
|
|
"|",
|
|
"sourceEditing",
|
|
"|",
|
|
"imageUpload",
|
|
];
|
|
|
|
const isMinimal = editorConfig.value === "minimal";
|
|
if (!isMinimal && editorConfig.value !== "basic") {
|
|
console.warn(`${editorConfig.value} is not a valid configuration. Falling back to "basic".`);
|
|
}
|
|
|
|
const finalConfig: EditorConfig = {
|
|
...sharedConfig,
|
|
licenseKey: "GPL",
|
|
toolbar: {
|
|
items: isMinimal ? minimalToolbar : basicToolbar,
|
|
},
|
|
image: {
|
|
toolbar: isMinimal
|
|
? []
|
|
: [
|
|
"toggleImageCaption",
|
|
"imageTextAlternative",
|
|
"imageStyle:inline",
|
|
"imageStyle:alignLeft",
|
|
"imageStyle:alignRight",
|
|
"imageStyle:alignCenter",
|
|
"imageStyle:side",
|
|
"imageResize",
|
|
"linkImage",
|
|
],
|
|
},
|
|
// Propriété passée au plugin d'upload personnalisé
|
|
...(modelName.value ? { folder: modelName.value } : {}),
|
|
};
|
|
|
|
ClassicEditor.create(ckEditor.value as HTMLElement, finalConfig)
|
|
.then((editor: Editor) => {
|
|
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
editorInstance.value = editor;
|
|
|
|
editor.setData(text.value);
|
|
|
|
editor.model.document.on("change:data", () => {
|
|
if (timer) {
|
|
clearTimeout(timer);
|
|
}
|
|
timer = setTimeout(() => {
|
|
const data = editor.getData();
|
|
text.value = data;
|
|
emits("input", data);
|
|
timer = null;
|
|
}, 300);
|
|
});
|
|
|
|
// Application des styles d'erreurs Laravel
|
|
applyValidationErrors();
|
|
})
|
|
.catch((error: unknown) => {
|
|
console.error("Oops, something went wrong while initializing CKEditor!");
|
|
console.error(error);
|
|
});
|
|
}
|
|
|
|
function applyValidationErrors(): void {
|
|
if (getErrorMessage() !== null) {
|
|
const mainWrapper = ckEditorComponent.value?.querySelector(".ck.ck-editor__main");
|
|
mainWrapper?.classList.add("ck-invalid");
|
|
}
|
|
}
|
|
|
|
function getErrorMessage(): string | null {
|
|
return parseLaravelErrorMessages?.methods?.parseValidationInput
|
|
? parseLaravelErrorMessages.methods.parseValidationInput(`${name.value}`, allErrors.value)
|
|
: null;
|
|
}
|
|
|
|
function onExternalSetData(event: Event): void {
|
|
const customEvent = event as CustomEvent<{ data?: string }>;
|
|
const payload = customEvent.detail?.data;
|
|
if (typeof payload !== "string") {
|
|
return;
|
|
}
|
|
|
|
text.value = payload;
|
|
if (hiddenInput.value) {
|
|
hiddenInput.value.value = payload;
|
|
}
|
|
|
|
if (editorInstance.value) {
|
|
editorInstance.value.setData(payload);
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<style lang="scss" scoped>
|
|
@import "bootstrap/scss/functions";
|
|
@import "bootstrap/scss/variables";
|
|
|
|
.ck-editor-component {
|
|
.ck-rounded-corners {
|
|
border-radius: 0 0 0.375rem 0.375rem !important;
|
|
}
|
|
|
|
.ck-word-count {
|
|
background-color: rgb(var(--bs-secondary-bg-rgb));
|
|
border: var(--bs-border-width) solid var(--bs-border-color);
|
|
border-bottom: 0px;
|
|
border-top-left-radius: 0.375rem;
|
|
border-top-right-radius: 0.375rem;
|
|
color: var(--bs-body-color);
|
|
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;
|
|
}
|
|
|
|
.ck-focused {
|
|
border-color: var(--bs-primary-border-subtle) !important;
|
|
box-shadow: 0 0 0 0.25rem rgba(var(--bs-primary-rgb), var(--bs-focus-ring-opacity)) !important;
|
|
outline: 0 !important;
|
|
}
|
|
|
|
.ck-editor__main {
|
|
color: $body-color;
|
|
}
|
|
|
|
.ck.ck-editor__main.ck-invalid .ck-blurred,
|
|
.ck.ck-editor__main.ck-invalid .ck-focused {
|
|
border-color: var(--bs-form-invalid-border-color) !important;
|
|
}
|
|
|
|
.ck.ck-editor__main.ck-invalid .ck-focused {
|
|
box-shadow: 0 0 0 0.25rem rgba(var(--bs-danger-rgb), var(--bs-focus-ring-opacity)) !important;
|
|
}
|
|
}
|
|
|
|
:deep(.ck.ck-powered-by-balloon) {
|
|
display: none !important;
|
|
}
|
|
</style>
|