From fcf70dd4ee0716f6fd078a5046044ada0f0862d7 Mon Sep 17 00:00:00 2001 From: YUCLing Date: Tue, 17 Dec 2024 19:39:54 +0800 Subject: [PATCH] chore: code format --- framework/core/js/src/common/Application.tsx | 66 +++++----- framework/core/js/src/common/Component.ts | 12 +- .../js/src/common/components/LinkButton.js | 2 +- .../core/js/src/common/helpers/listItems.tsx | 22 ++-- .../src/common/resolvers/DefaultResolver.tsx | 5 +- .../core/js/src/common/utils/cloneVnode.ts | 24 ++-- .../core/js/src/common/utils/patchMithril.js | 122 +++++++++--------- .../core/js/src/forum/ForumApplication.tsx | 10 +- .../js/src/forum/components/PostStream.js | 5 +- .../forum/components/PostStreamScrubber.js | 7 +- 10 files changed, 131 insertions(+), 144 deletions(-) diff --git a/framework/core/js/src/common/Application.tsx b/framework/core/js/src/common/Application.tsx index ed5a5787f5..dbb4bd60dc 100644 --- a/framework/core/js/src/common/Application.tsx +++ b/framework/core/js/src/common/Application.tsx @@ -357,41 +357,37 @@ export default class Application { let lastRoute: string; let comp: Component | null = null; function RoutedApp() { - return () => m.route(routePrefix, ({route}) => { - for (const k in routes) { - let match; - if (match = route.match(k)) { - const r = routes[k]; - const { current, path, params } = route; - const routeData = { - current, - path, - params: match, - searchParams: params - }; - appRef.routing = { - ...route, - params: match, - searchParams: params - }; - if (lastRoute != current) { - lastRoute = current; - r.onmatch(match, routeData).then((c: any) => { - comp = c; - this.redraw(); - }); + return () => + m.route(routePrefix, ({ route }) => { + for (const k in routes) { + let match; + if ((match = route.match(k))) { + const r = routes[k]; + const { current, path, params } = route; + const routeData = { + current, + path, + params: match, + searchParams: params, + }; + appRef.routing = { + ...route, + params: match, + searchParams: params, + }; + if (lastRoute != current) { + lastRoute = current; + r.onmatch(match, routeData).then((c: any) => { + comp = c; + this.redraw(); + }); + } + return comp ? m.set({ appRoute: routeData }, r.render ? r.render(comp, match) : m(comp, match)) : m('div'); } - return comp ? - m.set({appRoute: routeData}, r.render ? - r.render(comp, match) : - m(comp, match) - ) : - m('div'); } - } - }); + }); } - this.redraw.app = m.mount(document.getElementById('content')!, () => ); + this.redraw.app = m.mount(document.getElementById('content')!, () => ); const appEl = document.getElementById('app')!; const appHeaderEl = document.querySelector('.App-header')!; @@ -551,10 +547,8 @@ export default class Application { redrawAll(sync = false) { for (const k in app.redraw) { const redraw = app.redraw[k]; - if (sync) - redraw.sync(); - else - redraw(); + if (sync) redraw.sync(); + else redraw(); } } diff --git a/framework/core/js/src/common/Component.ts b/framework/core/js/src/common/Component.ts index 8c7d8596ac..30fe92e561 100644 --- a/framework/core/js/src/common/Component.ts +++ b/framework/core/js/src/common/Component.ts @@ -3,21 +3,21 @@ import type Mithril from 'mithril'; export interface ComponentAttrs extends Mithril.Attributes {} interface Redraw { - (): typeof m.redraw, - sync: typeof m.redraw.sync + (): typeof m.redraw; + sync: typeof m.redraw.sync; } interface Context { - redraw: Redraw, - [x: string]: any, + redraw: Redraw; + [x: string]: any; } export interface RenderAttrs { - attrs: Attrs + attrs: Attrs; } export interface RenderAttrsDOM extends RenderAttrs { - dom: HTMLElement + dom: HTMLElement; } /** diff --git a/framework/core/js/src/common/components/LinkButton.js b/framework/core/js/src/common/components/LinkButton.js index 0ce31965c5..14320d4a59 100644 --- a/framework/core/js/src/common/components/LinkButton.js +++ b/framework/core/js/src/common/components/LinkButton.js @@ -27,7 +27,7 @@ export default class LinkButton extends Button { view(vnode) { const vdom = super.view(vnode); - vdom.m = (vdom.m & ~m.TYPE_MASK) | (m.TYPE_COMPONENT & m.TYPE_MASK);; // make it component + vdom.m = (vdom.m & ~m.TYPE_MASK) | (m.TYPE_COMPONENT & m.TYPE_MASK); // make it component vdom.t = m.component(Link); vdom.a.children = vdom.c; vdom.a.active = String(vdom.a.active); diff --git a/framework/core/js/src/common/helpers/listItems.tsx b/framework/core/js/src/common/helpers/listItems.tsx index a657e85a84..8cdb9e6585 100644 --- a/framework/core/js/src/common/helpers/listItems.tsx +++ b/framework/core/js/src/common/helpers/listItems.tsx @@ -71,10 +71,7 @@ export default function listItems( item.a.key = item.a.key || item.itemName; const key = item.a.key; - return m.keyed([key], (key) => [ - key, - item - ]); + return m.keyed([key], (key) => [key, item]); } if (isVnode(item)) { @@ -86,15 +83,12 @@ export default function listItems( } const key = (isVnode(item) && item?.a?.key) || item.itemName; - const child = - {cloneVnode(item)} - ; - - return key ? m.keyed([key], (key) => - [ - key, - child - ]) : child; + const child = ( + + {cloneVnode(item)} + + ); + + return key ? m.keyed([key], (key) => [key, child]) : child; }); } - \ No newline at end of file diff --git a/framework/core/js/src/common/resolvers/DefaultResolver.tsx b/framework/core/js/src/common/resolvers/DefaultResolver.tsx index adde55e71e..cf3e0359bb 100644 --- a/framework/core/js/src/common/resolvers/DefaultResolver.tsx +++ b/framework/core/js/src/common/resolvers/DefaultResolver.tsx @@ -52,9 +52,6 @@ export default class DefaultResolver< } render(comp: Comp, attrs: Attrs) { - return m.keyed([this.makeKey()], (key) => [ - key, - m(comp, this.makeAttrs(attrs)) - ]); + return m.keyed([this.makeKey()], (key) => [key, m(comp, this.makeAttrs(attrs))]); } } diff --git a/framework/core/js/src/common/utils/cloneVnode.ts b/framework/core/js/src/common/utils/cloneVnode.ts index 20d4109b47..41c33f405c 100644 --- a/framework/core/js/src/common/utils/cloneVnode.ts +++ b/framework/core/js/src/common/utils/cloneVnode.ts @@ -1,14 +1,14 @@ export default function cloneVnode(vnode: any): any { - if (Array.isArray(vnode)) return vnode.map((v) => cloneVnode(v)); - if (typeof vnode !== 'object' || vnode === null || !('m' in vnode)) return vnode; - let children: any[] = []; - if (Array.isArray(vnode.c)) { - for (const i in (vnode.c as any[])) { - children[i] = cloneVnode(vnode.c[i]); - } + if (Array.isArray(vnode)) return vnode.map((v) => cloneVnode(v)); + if (typeof vnode !== 'object' || vnode === null || !('m' in vnode)) return vnode; + let children: any[] = []; + if (Array.isArray(vnode.c)) { + for (const i in vnode.c as any[]) { + children[i] = cloneVnode(vnode.c[i]); } - return { - ...vnode, - c: vnode.c ? children : vnode.c - }; -} \ No newline at end of file + } + return { + ...vnode, + c: vnode.c ? children : vnode.c, + }; +} diff --git a/framework/core/js/src/common/utils/patchMithril.js b/framework/core/js/src/common/utils/patchMithril.js index 84ebb02352..92fe1d0414 100644 --- a/framework/core/js/src/common/utils/patchMithril.js +++ b/framework/core/js/src/common/utils/patchMithril.js @@ -4,19 +4,19 @@ import bidi from './bidi'; /** * Create a function component from Component class. - * @param {Component} comp - * @returns + * @param {Component} comp + * @returns */ function createComponentFunc(comp) { return function (attrs, old) { /** @type {Component} */ - const inst = attrs.__component = old?.__component ?? new comp(); + const inst = (attrs.__component = old?.__component ?? new comp()); const { children: mChildren, __component, ...compAttrs } = attrs; const vnodeAttrs = { - attrs: compAttrs + attrs: compAttrs, }; - + const isInit = !old?.__component; let beforeUpdateResult; @@ -26,7 +26,7 @@ function createComponentFunc(comp) { } else { beforeUpdateResult = inst.onbeforeupdate({ ...vnodeAttrs, - dom: inst.element // the dom must be existing since this is not the first time it got rendered. + dom: inst.element, // the dom must be existing since this is not the first time it got rendered. }); } @@ -34,7 +34,7 @@ function createComponentFunc(comp) { m.layout((dom) => { const vnodeDOMAttrs = { ...vnodeAttrs, - dom + dom, }; if (isInit) { inst.oncreate(vnodeDOMAttrs); @@ -49,66 +49,72 @@ function createComponentFunc(comp) { m.remove((dom) => { const vnodeDOMAttrs = { ...vnodeAttrs, - dom + dom, }; inst.onbeforeremove(vnodeDOMAttrs); // todo: support delayed removal inst.onremove(vnodeDOMAttrs); - }) + }), ]; - return (function () { - const viewArgs = [{ - ...vnodeAttrs, - children: mChildren - }, old]; + return function () { + const viewArgs = [ + { + ...vnodeAttrs, + children: mChildren, + }, + old, + ]; const view = this.view(...viewArgs); return [...children, ...(Array.isArray(view) ? view : [view])]; - }).apply(inst); - } + }.apply(inst); + }; } function Trust(attrs) { return [ m.layout((dom) => { - dom.innerHTML = attrs.html - }) + dom.innerHTML = attrs.html; + }), ]; } // from mithril, modified const Link = () => { - var href, opts, setRoute - var listener = (ev) => { - if ( - !ev.defaultPrevented && - (ev.button === 0 || ev.which === 0 || ev.which === 1) && - (!ev.currentTarget.target || ev.currentTarget.target === "_self") && - !ev.ctrlKey && !ev.metaKey && !ev.shiftKey && !ev.altKey - ) { - setRoute(href, opts) - return m.capture(ev) - } - } - - return function (attrs, old) { - setRoute = app.routing.set - href = attrs.h - opts = attrs.o - return [ - m.layout((dom) => { - dom.href = app.routing.prefix + href - if (!old) dom.addEventListener("click", listener) - }), - m.remove((dom) => { - dom.removeEventListener("click", listener) - }), - ] - } -} + var href, opts, setRoute; + var listener = (ev) => { + if ( + !ev.defaultPrevented && + (ev.button === 0 || ev.which === 0 || ev.which === 1) && + (!ev.currentTarget.target || ev.currentTarget.target === '_self') && + !ev.ctrlKey && + !ev.metaKey && + !ev.shiftKey && + !ev.altKey + ) { + setRoute(href, opts); + return m.capture(ev); + } + }; + + return function (attrs, old) { + setRoute = app.routing.set; + href = attrs.h; + opts = attrs.o; + return [ + m.layout((dom) => { + dom.href = app.routing.prefix + href; + if (!old) dom.addEventListener('click', listener); + }), + m.remove((dom) => { + dom.removeEventListener('click', listener); + }), + ]; + }; +}; function getOrCreateComponentFunc(comp) { let func = componentFuncs.get(comp); if (!func) { - componentFuncs.set(comp, func = createComponentFunc(comp)); + componentFuncs.set(comp, (func = createComponentFunc(comp))); func.Component = comp; } return func; @@ -128,9 +134,9 @@ export default function patchMithril(global) { 'a', { className: attrs.className, - class: attrs.class + class: attrs.class, }, - m(Link, {h: `${attrs.href}`, o: attrs.options}), + m(Link, { h: `${attrs.href}`, o: attrs.options }), args.slice(1) ); } @@ -158,36 +164,36 @@ export default function patchMithril(global) { throw 'Not a component'; } return getOrCreateComponentFunc(comp); - } + }; modifiedMithril.route.param = function (k) { console.warn('m.route.param is not available in v3, falling back to managed routing'); return app.routing.params[k]; - } + }; modifiedMithril.route.get = function () { console.warn('m.route.get is not available in v3, falling back to managed routing'); return app.routing.current; - } + }; - modifiedMithril.route.set = function(link) { + modifiedMithril.route.set = function (link) { console.warn('m.route.set is not available in v3, falling back to managed routing'); app.routing.set(link); - } + }; modifiedMithril.redraw = function () { console.warn('global redraw is not available in v3, using managed global redraw for now'); app.redrawAll(); - } + }; - modifiedMithril.redraw.sync = function() { + modifiedMithril.redraw.sync = function () { console.warn('global redraw is not available in v3, using managed global redraw for now'); app.redrawAll(true); - } + }; modifiedMithril.trust = function (html) { return m(Trust, { html }); - } + }; modifiedMithril.route.Link = '__LINK__'; diff --git a/framework/core/js/src/forum/ForumApplication.tsx b/framework/core/js/src/forum/ForumApplication.tsx index e8cb5a50bf..7f57614e57 100644 --- a/framework/core/js/src/forum/ForumApplication.tsx +++ b/framework/core/js/src/forum/ForumApplication.tsx @@ -115,11 +115,11 @@ export default class ForumApplication extends Application { // We mount navigation and header components after the page, so components // like the back button can access the updated state when rendering. m.mount(document.getElementById('app-navigation')!, () => ); - m.mount(document.getElementById('header-navigation')!, () => ); - m.mount(document.getElementById('header-primary')!, () => ); - m.mount(document.getElementById('header-secondary')!, () => ); - m.mount(document.getElementById('notices')!, () => ); - m.mount(document.getElementById('footer')!, () =>