a little js
This commit is contained in:
parent
c4403bffeb
commit
393db312c2
4 changed files with 55 additions and 4 deletions
19
app/javascript/controllers/hide_target_controller.ts
Normal file
19
app/javascript/controllers/hide_target_controller.ts
Normal 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');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
|
20
app/javascript/controllers/tiptap_to_template_controller.ts
Normal file
20
app/javascript/controllers/tiptap_to_template_controller.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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,
|
||||||
|
|
Loading…
Reference in a new issue