a little js

This commit is contained in:
simon lehericey 2024-07-15 23:28:02 +02:00
parent c4403bffeb
commit 393db312c2
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
4 changed files with 55 additions and 4 deletions

View file

@ -0,0 +1,19 @@
import { Controller } from '@hotwired/stimulus';
export class HideTargetController extends Controller {
static targets = ['source', 'toHide'];
declare readonly toHideTargets: HTMLDivElement[];
declare readonly sourceTargets: HTMLInputElement[];
connect() {
this.sourceTargets.forEach((source) => {
source.addEventListener('click', this.handleInput.bind(this));
});
}
handleInput() {
this.toHideTargets.forEach((toHide) => {
toHide.classList.toggle('fr-hidden');
});
}
}

View file

@ -10,7 +10,8 @@ import { createEditor } from '../../shared/tiptap/editor';
export class TiptapController extends ApplicationController { export class TiptapController extends ApplicationController {
static targets = ['editor', 'input', 'button', 'tag']; static targets = ['editor', 'input', 'button', 'tag'];
static values = { static values = {
insertAfterTag: { type: String, default: '' } insertAfterTag: { type: String, default: '' },
attributes: { type: Object, default: {} }
}; };
declare editorTarget: Element; declare editorTarget: Element;
@ -18,6 +19,7 @@ export class TiptapController extends ApplicationController {
declare buttonTargets: HTMLButtonElement[]; declare buttonTargets: HTMLButtonElement[];
declare tagTargets: HTMLElement[]; declare tagTargets: HTMLElement[];
declare insertAfterTagValue: string; declare insertAfterTagValue: string;
declare attributesValue: Record<string, string>;
#initializing = true; #initializing = true;
#editor?: Editor; #editor?: Editor;
@ -28,6 +30,7 @@ export class TiptapController extends ApplicationController {
content: this.content, content: this.content,
tags: this.tags, tags: this.tags,
buttons: this.menuButtons, buttons: this.menuButtons,
attributes: { class: 'fr-input', ...this.attributesValue },
onChange: ({ editor }) => { onChange: ({ editor }) => {
for (const button of this.buttonTargets) { for (const button of this.buttonTargets) {
const action = getAction(editor, button); const action = getAction(editor, button);

View file

@ -0,0 +1,20 @@
import { Controller } from '@hotwired/stimulus';
export class TiptapToTemplateController extends Controller {
static targets = ['output', 'trigger'];
declare readonly outputTarget: HTMLElement;
declare readonly triggerTarget: HTMLButtonElement;
connect() {
this.triggerTarget.addEventListener('click', this.handleClick.bind(this));
}
handleClick() {
const template = this.element.querySelector('.tiptap.ProseMirror p');
if (template) {
this.outputTarget.innerHTML = template.innerHTML;
}
}
}

View file

@ -33,6 +33,7 @@ export function createEditor({
content, content,
tags, tags,
buttons, buttons,
attributes,
onChange onChange
}: { }: {
editorElement: Element; editorElement: Element;
@ -40,8 +41,15 @@ export function createEditor({
tags: TagSchema[]; tags: TagSchema[];
buttons: string[]; buttons: string[];
onChange(change: { editor: Editor }): void; onChange(change: { editor: Editor }): void;
attributes?: Record<string, string>;
}): Editor { }): Editor {
const options = getEditorOptions(editorElement, tags, buttons, content); const options = getEditorOptions(
editorElement,
tags,
buttons,
content,
attributes
);
const editor = new Editor(options); const editor = new Editor(options);
editor.on('transaction', onChange); editor.on('transaction', onChange);
return editor; return editor;
@ -51,7 +59,8 @@ function getEditorOptions(
element: Element, element: Element,
tags: TagSchema[], tags: TagSchema[],
actions: string[], actions: string[],
content?: JSONContent content?: JSONContent,
attributes?: Record<string, string>
): Partial<EditorOptions> { ): Partial<EditorOptions> {
const extensions: Extensions = []; const extensions: Extensions = [];
for (const action of actions) { for (const action of actions) {
@ -123,7 +132,7 @@ function getEditorOptions(
return { return {
element, element,
content, content,
editorProps: { attributes: { class: 'fr-input' } }, editorProps: { attributes },
extensions: [ extensions: [
actions.includes('title') ? DocumentWithHeader : Document, actions.includes('title') ? DocumentWithHeader : Document,
Hystory, Hystory,