2023-01-13 11:13:09 +01:00
|
|
|
import { Actions } from '@coldwired/actions';
|
|
|
|
import { parseTurboStream } from '@coldwired/turbo-stream';
|
2024-05-06 18:07:29 +02:00
|
|
|
import { createRoot, createReactPlugin, type Root } from '@coldwired/react';
|
2023-01-13 11:13:09 +01:00
|
|
|
import invariant from 'tiny-invariant';
|
|
|
|
import { session as TurboSession, type StreamElement } from '@hotwired/turbo';
|
2024-05-06 18:07:29 +02:00
|
|
|
import type { ComponentType } from 'react';
|
2023-01-11 21:45:41 +01:00
|
|
|
|
|
|
|
import { ApplicationController } from './application_controller';
|
|
|
|
|
2023-01-13 11:13:09 +01:00
|
|
|
type StreamRenderEvent = CustomEvent<{
|
|
|
|
render(streamElement: StreamElement): void;
|
|
|
|
}>;
|
|
|
|
|
2023-02-28 14:05:11 +01:00
|
|
|
type FrameRenderEvent = CustomEvent<{
|
|
|
|
render(currentElement: Element, newElement: Element): void;
|
|
|
|
}>;
|
|
|
|
|
2023-01-11 21:45:41 +01:00
|
|
|
export class TurboController extends ApplicationController {
|
|
|
|
static targets = ['spinner'];
|
|
|
|
|
2023-01-13 11:13:09 +01:00
|
|
|
declare readonly spinnerTargets: HTMLElement[];
|
|
|
|
|
|
|
|
#submitting = false;
|
|
|
|
#actions?: Actions;
|
2024-05-06 18:07:29 +02:00
|
|
|
#root?: Root;
|
2023-01-11 21:45:41 +01:00
|
|
|
|
2023-01-13 11:13:09 +01:00
|
|
|
// `actions` instrface exposes all available actions as methods and also `applyActions` method
|
|
|
|
// wich allows to apply a batch of actions. On top of regular `turbo-stream` actions we also
|
|
|
|
// expose `focus`, `enable`, `disable`, `show` and `hide` actions. Each action take a `targets`
|
|
|
|
// option (wich can be a CSS selector or a list of DOM nodes) and a `fragment` option (wich is a
|
|
|
|
// `DocumentFragment` and only required on "rendering" actions).
|
|
|
|
get actions() {
|
|
|
|
invariant(this.#actions, 'Actions not initialized');
|
|
|
|
return this.#actions;
|
|
|
|
}
|
2023-01-12 17:57:41 +01:00
|
|
|
|
2023-01-11 21:45:41 +01:00
|
|
|
connect() {
|
2024-05-06 18:07:29 +02:00
|
|
|
this.#root = createRoot({
|
|
|
|
layoutComponentName: 'Layout/Layout',
|
|
|
|
loader,
|
|
|
|
schema: {
|
|
|
|
fragmentTagName: 'react-fragment',
|
|
|
|
componentTagName: 'react-component',
|
|
|
|
slotTagName: 'react-slot',
|
|
|
|
loadingClassName: 'loading'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const plugin = createReactPlugin(this.#root);
|
2023-01-13 11:13:09 +01:00
|
|
|
this.#actions = new Actions({
|
2023-02-28 14:05:11 +01:00
|
|
|
element: document.body,
|
2023-04-27 09:39:12 +02:00
|
|
|
schema: {
|
|
|
|
forceAttribute: 'data-turbo-force',
|
|
|
|
focusGroupAttribute: 'data-turbo-focus-group',
|
|
|
|
focusDirectionAttribute: 'data-turbo-focus-direction',
|
|
|
|
hiddenClassName: 'hidden'
|
|
|
|
},
|
2024-05-06 18:07:29 +02:00
|
|
|
plugins: [plugin],
|
2023-01-16 21:31:07 +01:00
|
|
|
debug: false
|
2023-01-13 11:13:09 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// actions#observe() is an interface over specialized mutation observers.
|
|
|
|
// They allow us to preserve certain HTML changes across mutations.
|
|
|
|
this.#actions.observe();
|
|
|
|
|
2024-06-21 17:03:15 +02:00
|
|
|
this.#actions.ready().then(() => {
|
|
|
|
document.body.classList.add('dom-ready');
|
|
|
|
});
|
|
|
|
|
2023-01-13 11:13:09 +01:00
|
|
|
// setup spinner events
|
2023-01-12 17:57:41 +01:00
|
|
|
this.onGlobal('turbo:submit-start', () => this.startSpinner());
|
|
|
|
this.onGlobal('turbo:submit-end', () => this.stopSpinner());
|
|
|
|
this.onGlobal('turbo:fetch-request-error', () => this.stopSpinner());
|
|
|
|
|
|
|
|
// prevent scroll on turbo form submits
|
|
|
|
this.onGlobal('turbo:render', () => this.preventScrollIfNeeded());
|
2023-01-13 11:13:09 +01:00
|
|
|
|
|
|
|
// see: https://turbo.hotwired.dev/handbook/streams#custom-actions
|
|
|
|
this.onGlobal('turbo:before-stream-render', (event: StreamRenderEvent) => {
|
|
|
|
event.detail.render = (streamElement: StreamElement) =>
|
|
|
|
this.actions.applyActions([parseTurboStream(streamElement)]);
|
2023-01-16 21:31:07 +01:00
|
|
|
});
|
2023-02-28 14:05:11 +01:00
|
|
|
|
|
|
|
// see: https://turbo.hotwired.dev/handbook/frames#custom-rendering
|
|
|
|
this.onGlobal('turbo:before-frame-render', (event: FrameRenderEvent) => {
|
|
|
|
event.detail.render = (currentElement, newElement) => {
|
|
|
|
// There is a bug in morphdom when it comes to mutate a custom element. It will miserably
|
|
|
|
// crash. We mutate its content instead.
|
|
|
|
const fragment = document.createDocumentFragment();
|
|
|
|
fragment.append(...newElement.childNodes);
|
|
|
|
this.actions.update({ targets: [currentElement], fragment });
|
|
|
|
};
|
|
|
|
});
|
2023-01-12 17:57:41 +01:00
|
|
|
}
|
|
|
|
|
2024-05-06 18:07:29 +02:00
|
|
|
disconnect(): void {
|
|
|
|
this.#actions?.disconnect();
|
|
|
|
this.#root?.destroy();
|
|
|
|
}
|
|
|
|
|
2023-01-13 11:13:09 +01:00
|
|
|
private startSpinner() {
|
|
|
|
this.#submitting = true;
|
|
|
|
this.actions.show({ targets: this.spinnerTargets });
|
|
|
|
}
|
|
|
|
|
|
|
|
private stopSpinner() {
|
2023-01-12 17:57:41 +01:00
|
|
|
this.#submitting = false;
|
2023-01-13 11:13:09 +01:00
|
|
|
this.actions.hide({ targets: this.spinnerTargets });
|
2023-01-12 17:57:41 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 11:13:09 +01:00
|
|
|
private preventScrollIfNeeded() {
|
2023-01-12 17:57:41 +01:00
|
|
|
if (this.#submitting && TurboSession.navigator.currentVisit) {
|
|
|
|
TurboSession.navigator.currentVisit.scrolled = true;
|
|
|
|
}
|
2023-01-11 21:45:41 +01:00
|
|
|
}
|
|
|
|
}
|
2024-05-06 18:07:29 +02:00
|
|
|
|
|
|
|
type Loader = (exportName: string) => Promise<ComponentType<unknown>>;
|
|
|
|
const componentsRegistry: Record<string, Loader> = {};
|
|
|
|
const components = import.meta.glob('../components/*.tsx');
|
|
|
|
|
|
|
|
const loader: Loader = (name) => {
|
|
|
|
const [moduleName, exportName] = name.split('/');
|
|
|
|
const loader = componentsRegistry[moduleName];
|
|
|
|
invariant(loader, `Cannot find a React component with name "${name}"`);
|
|
|
|
return loader(exportName ?? 'default');
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const [path, loader] of Object.entries(components)) {
|
|
|
|
const [filename] = path.split('/').reverse();
|
|
|
|
const componentClassName = filename.replace(/\.(ts|tsx)$/, '');
|
|
|
|
console.debug(`Registered lazy export for "${componentClassName}" component`);
|
|
|
|
componentsRegistry[componentClassName] = (exportName) =>
|
|
|
|
loader().then(
|
|
|
|
(m) => (m as Record<string, ComponentType<unknown>>)[exportName]
|
|
|
|
);
|
|
|
|
}
|