Prepared improvment of editor code structure (Typescript part)

pull/10/head
Jens Pelzetter 2021-08-26 20:37:12 +02:00
parent 61e6d6945c
commit 3f8f44e2e7
2 changed files with 146 additions and 129 deletions

View File

@ -5,7 +5,7 @@
xmlns:cc="http://xmlns.jcp.org/jsf/composite" xmlns:cc="http://xmlns.jcp.org/jsf/composite"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core" xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
> >
<cc:interface <cc:interface
shortDescription="A editor component for HTML texts using the TipTap editor. To use this component you have also to include the cms-editor.js file into the page." shortDescription="A editor component for HTML texts using the TipTap editor. To use this component you have also to include the cms-editor.js file into the page."
> >
@ -328,7 +328,7 @@
</div> </div>
</template> </template>
</div> </div>
<!-- <c:if test="#{cc.attrs.hasUnusedLocales}"> <!-- <c:if test="#{cc.attrs.hasUnusedLocales}">
<div class="mb-2"> <div class="mb-2">
<div class="text-right"> <div class="text-right">
<button <button
@ -775,9 +775,7 @@ tabindex="-1"
</h6> </h6>
</c:when> </c:when>
<c:otherwise> <c:otherwise>
<div <div id="#{cc.attrs.editorId}-edit-dialog-title">
id="#{cc.attrs.editorId}-edit-dialog-title"
>
#{cc.attrs.editDialogTitle} #{cc.attrs.selectedLocale} #{cc.attrs.editDialogTitle} #{cc.attrs.selectedLocale}
</div> </div>
</c:otherwise> </c:otherwise>
@ -791,9 +789,11 @@ tabindex="-1"
</button>--> </button>-->
</div> </div>
<!--<div class="modal-body">--> <!--<div class="modal-body">-->
<div class="cms-tiptap-editor" <div
class="cms-tiptap-editor"
data-locale="#{cc.attrs.selectedLocale}" data-locale="#{cc.attrs.selectedLocale}"
data-variant-url="#{cc.attrs.variantUrl}/#{cc.attrs.selectedLocale}"> data-variant-url="#{cc.attrs.variantUrl}/#{cc.attrs.selectedLocale}"
>
<div class="cms-tiptap-editor-buttons d-flex mb-1"> <div class="cms-tiptap-editor-buttons d-flex mb-1">
<div class="px-2 cms-tiptap-editor-textformatting"> <div class="px-2 cms-tiptap-editor-textformatting">
<button <button
@ -807,10 +807,7 @@ tabindex="-1"
</span> --> </span> -->
</button> </button>
<button <button
class=" class="btn btn-outline-dark tiptap-strong-emph"
btn btn-outline-dark
tiptap-strong-emph
"
title="#{CmsAdminMessages['cms_editor.buttons.strong_emph']}" title="#{CmsAdminMessages['cms_editor.buttons.strong_emph']}"
type="button" type="button"
> >
@ -830,58 +827,38 @@ tabindex="-1"
</span> --> </span> -->
</button> </button>
<button <button
class=" class="btn btn-outline-dark tiptap-strikethrough"
btn btn-outline-dark
tiptap-strikethrough
"
title="#{CmsAdminMessages['cms_editor.buttons.strikethrough']}" title="#{CmsAdminMessages['cms_editor.buttons.strikethrough']}"
type="button" type="button"
> >
<bootstrap:svgIcon <bootstrap:svgIcon icon="type-strikethrough" />
icon="type-strikethrough"
/>
<!-- <span class="sr-only"> <!-- <span class="sr-only">
#{CmsAdminMessages['cms_editor.buttons.strikethrough']} #{CmsAdminMessages['cms_editor.buttons.strikethrough']}
</span> --> </span> -->
</button> </button>
<button <button
class=" class="btn btn-outline-dark tiptap-subscript"
btn btn-outline-dark
tiptap-subscript
"
title="#{CmsAdminMessages['cms_editor.buttons.subscript']}" title="#{CmsAdminMessages['cms_editor.buttons.subscript']}"
type="button" type="button"
> >
<span aria-hidden="true" <span aria-hidden="true">x<sub>n</sub></span>
>x<sub>n</sub></span
>
</button> </button>
<button <button
class=" class="btn btn-outline-dark tiptap-superscript"
btn btn-outline-dark
tiptap-superscript
"
title="#{CmsAdminMessages['cms_editor.buttons.superscript']}" title="#{CmsAdminMessages['cms_editor.buttons.superscript']}"
type="button" type="button"
> >
<span aria-hidden="true" <span aria-hidden="true">x<sup>n</sup></span>
>x<sup>n</sup></span
>
</button> </button>
</div> </div>
<div class="px-2 cms-tiptap-editor-headings"> <div class="px-2 cms-tiptap-editor-headings">
<ui:repeat begin="1" end="6" var="level"> <ui:repeat begin="1" end="6" var="level">
<button <button
class=" class="btn btn-outline-dark tiptap-h#{level}"
btn btn-outline-dark
tiptap-h#{level}
"
title="#{CmsAdminMessages['cms_editor.buttons.h'.concat(level)]}" title="#{CmsAdminMessages['cms_editor.buttons.h'.concat(level)]}"
type="button" type="button"
> >
<span aria-hidden="true" <span aria-hidden="true">H#{level}</span>
>H#{level}</span
>
</button> </button>
</ui:repeat> </ui:repeat>
<!-- <button class="btn btn-outline-dark tiptap-h2" <!-- <button class="btn btn-outline-dark tiptap-h2"
@ -892,30 +869,21 @@ tabindex="-1"
</div> </div>
<div class="px-2 cms-tiptap-editor-paragraphs"> <div class="px-2 cms-tiptap-editor-paragraphs">
<button <button
class=" class="btn btn-outline-dark tiptap-paragraph"
btn btn-outline-dark
tiptap-paragraph
"
title="#{CmsAdminMessages['cms_editor.buttons.paragraph']}" title="#{CmsAdminMessages['cms_editor.buttons.paragraph']}"
type="button" type="button"
> >
<bootstrap:svgIcon icon="type" /> <bootstrap:svgIcon icon="type" />
</button> </button>
<button <button
class=" class="btn btn-outline-dark tiptap-blockquote"
btn btn-outline-dark
tiptap-blockquote
"
title="#{CmsAdminMessages['cms_editor.buttons.blockquote']}" title="#{CmsAdminMessages['cms_editor.buttons.blockquote']}"
type="button" type="button"
> >
<bootstrap:svgIcon icon="blockquote-left" /> <bootstrap:svgIcon icon="blockquote-left" />
</button> </button>
<button <button
class=" class="btn btn-outline-dark tiptap-codeblock"
btn btn-outline-dark
tiptap-codeblock
"
title="#{CmsAdminMessages['cms_editor.buttons.codeblock']}" title="#{CmsAdminMessages['cms_editor.buttons.codeblock']}"
type="button" type="button"
> >
@ -940,10 +908,7 @@ tabindex="-1"
</div> </div>
<div class="px-2 cms-tiptap-editor-table"> <div class="px-2 cms-tiptap-editor-table">
<button <button
class=" class="btn btn-outline-dark tiptap-insert-table"
btn btn-outline-dark
tiptap-insert-table
"
data-target="#insert-table-dialog" data-target="#insert-table-dialog"
data-toggle="modal" data-toggle="modal"
title="#{CmsAdminMessage['cms_editor.buttons.insert_table']}" title="#{CmsAdminMessage['cms_editor.buttons.insert_table']}"
@ -961,8 +926,10 @@ tabindex="-1"
<div class="modal-dialog"> <div class="modal-dialog">
<form class="modal-content"> <form class="modal-content">
<div class="modal-header"> <div class="modal-header">
<h4 class="modal-title" <h4
id="insert-table-dialog-title"> class="modal-title"
id="insert-table-dialog-title"
>
#{CmsAdminMessages['cms_editor.dialogs.insert_table.title']} #{CmsAdminMessages['cms_editor.dialogs.insert_table.title']}
</h4> </h4>
<button <button
@ -971,9 +938,7 @@ tabindex="-1"
data-dismiss="modal" data-dismiss="modal"
type="button" type="button"
> >
<bootstrap:svgIcon <bootstrap:svgIcon icon="x" />
icon="x"
/>
</button> </button>
</div> </div>
<div class="modal-body"> <div class="modal-body">
@ -1019,17 +984,28 @@ tabindex="-1"
</div> </div>
</form> </form>
</div> </div>
<button
class="btn btn-outline-dark tiptap-insert-table-row"
title="#{CmsAdminMessage['cms_editor.buttons.insert_table_row']}"
type="button"
>
Insert row
</button>
</div> </div>
</div> </div>
</div> </div>
<div class="cms-tiptap-editor-canvas border" <div
class="cms-tiptap-editor-canvas border"
data-locale="#{cc.attrs.selectedLocale}" data-locale="#{cc.attrs.selectedLocale}"
data-variant-url="#{cc.attrs.variantUrl}"></div> data-variant-url="#{cc.attrs.variantUrl}"
></div>
<!--</div>--> <!--</div>-->
<!--<div class="modal-footer">--> <!--<div class="modal-footer">-->
<div class="mt-3"> <div class="mt-3">
<a class="btn btn-warning cms-editor-cancel-button" <a
href="#{backUrl}"> class="btn btn-warning cms-editor-cancel-button"
href="#{backUrl}"
>
#{cc.attrs.editDialogCancelLabel} #{cc.attrs.editDialogCancelLabel}
</a> </a>
<button <button

View File

@ -1,6 +1,6 @@
import "bootstrap"; import "bootstrap";
import * as $ from "jquery"; import * as $ from "jquery";
import { Editor } from "@tiptap/core"; import { ChainedCommands, Editor } from "@tiptap/core";
import Gapcursor from "@tiptap/extension-gapcursor"; import Gapcursor from "@tiptap/extension-gapcursor";
import StarterKit from "@tiptap/starter-kit"; import StarterKit from "@tiptap/starter-kit";
import Subscript from "@tiptap/extension-subscript"; import Subscript from "@tiptap/extension-subscript";
@ -10,6 +10,18 @@ import TableRow from "@tiptap/extension-table-row";
import TableCell from "@tiptap/extension-table-cell"; import TableCell from "@tiptap/extension-table-cell";
import TableHeader from "@tiptap/extension-table-header"; import TableHeader from "@tiptap/extension-table-header";
const BUTTONS: CmsEditorButton[] = [
{
selector: ".tiptap-emph",
command: editor => {
return editor.chain().focus().toggleItalic().run();
},
can: editor => {
return editor.can().chain().focus().toggleItalic().run();
}
}
];
document.addEventListener("DOMContentLoaded", function (event) { document.addEventListener("DOMContentLoaded", function (event) {
// const viewButtons = document.querySelectorAll( // const viewButtons = document.querySelectorAll(
// ".cms-editor .cms-editor-variants .cms-editor-view-button" // ".cms-editor .cms-editor-variants .cms-editor-view-button"
@ -119,19 +131,31 @@ async function initEditor(editorElem: HTMLElement) {
const buttonsElem = editorElem.querySelector(".cms-tiptap-editor-buttons"); const buttonsElem = editorElem.querySelector(".cms-tiptap-editor-buttons");
if (buttonsElem) { if (buttonsElem) {
initEditorButtons(editor, buttonsElem); initEditorButtons(editor, buttonsElem);
for (const button of BUTTONS) {
buttonsElem
.querySelector(button.selector)
?.addEventListener("click", event => {
event.preventDefault();
button.command(editor);
});
}
} else { } else {
console.error("editorButtons are null"); console.error("editorButtons are null");
return; return;
} }
editor.on("selectionUpdate", (editorParam: EditorParam) => {
// ToDo
});
} }
function initEditorButtons(editor: Editor, buttonsElem: Element) { function initEditorButtons(editor: Editor, buttonsElem: Element) {
buttonsElem // buttonsElem
.querySelector(".tiptap-emph") // .querySelector(".tiptap-emph")
?.addEventListener("click", event => { // ?.addEventListener("click", event => {
event.preventDefault(); // event.preventDefault();
editor.chain().focus().toggleItalic().run(); // editor.chain().focus().toggleItalic().run();
}); // });
buttonsElem buttonsElem
.querySelector(".tiptap-strong-emph") .querySelector(".tiptap-strong-emph")
@ -208,18 +232,6 @@ function initEditorButtons(editor: Editor, buttonsElem: Element) {
editor.chain().focus().toggleOrderedList().run(); editor.chain().focus().toggleOrderedList().run();
}); });
editor.on("selectionUpdate", ({ editor }) => {
console.log("Selection updated");
console.log(
`can insertRowBefore: ${editor
.can()
.chain()
.focus()
.addRowBefore()
.run()}`
);
});
buttonsElem buttonsElem
.querySelector(".cms-editor-insert-table-dialog .btn-success") .querySelector(".cms-editor-insert-table-dialog .btn-success")
?.addEventListener("click", event => { ?.addEventListener("click", event => {
@ -252,6 +264,25 @@ function initEditorButtons(editor: Editor, buttonsElem: Element) {
.run(); .run();
$("#insert-table-dialog").modal("hide"); $("#insert-table-dialog").modal("hide");
}); });
buttonsElem
.querySelector(".cms-editor-insert-table-row")
?.addEventListener("click", event => {
event.preventDefault();
editor.chain().focus().addRowAfter().run();
});
editor.on("selectionUpdate", ({ editor }) => {
console.log("Selection updated");
console.log(
`can insertRowAfter: ${editor
.can()
.chain()
.focus()
.addRowAfter()
.run()}`
);
});
} }
async function showEditDialog(event: Event) { async function showEditDialog(event: Event) {
@ -459,3 +490,13 @@ async function showViewDialog(event: Event) {
const viewDialogJquery = $(`#${viewDialogId}`) as any; const viewDialogJquery = $(`#${viewDialogId}`) as any;
viewDialogJquery.modal("toggle"); viewDialogJquery.modal("toggle");
} }
interface CmsEditorButton {
selector: string;
command: (editor: Editor) => boolean;
can: (editor: Editor) => boolean;
}
interface EditorParam {
editor: Editor;
}