@olenbetong/synergi-react 5.6.0 → 5.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/chrome/refresh.d.ts +16 -0
- package/es/chrome/refresh.js +65 -0
- package/es/components/CodeEditor/CodeDiffEditor.d.ts +40 -0
- package/es/components/CodeEditor/CodeDiffEditor.js +138 -0
- package/es/components/CodeEditor/index.d.ts +1 -0
- package/es/components/CodeEditor/index.js +1 -0
- package/es/index.d.ts +1 -0
- package/es/index.js +1 -0
- package/package.json +9 -7
- package/src/chrome/refresh.test.tsx +78 -0
- package/src/chrome/refresh.ts +86 -0
- package/src/components/CodeEditor/CodeDiffEditor.tsx +205 -0
- package/src/components/CodeEditor/index.ts +1 -0
- package/src/index.ts +1 -0
- package/src/test/setup.ts +4 -2
- package/src/vehicles/VehicleInfoDialog/formatting.test.ts +10 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Makes pull-to-refresh run `handler` instead of reloading the page, while the component is
|
|
3
|
+
* mounted. The indicator spins until the returned promise settles. The latest render's handler is
|
|
4
|
+
* used; pass `null` or `false` to leave the page reload in place (for a page that only sometimes
|
|
5
|
+
* can reload its own data). Nested components win: the one mounted last is the one that runs.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* usePullToRefresh(() => Promise.all([dsTimesheet.refreshDataSource(), dsActivities.refreshDataSource()]));
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare function usePullToRefresh(handler: (() => unknown | Promise<unknown>) | null | false | undefined): void;
|
|
13
|
+
/** Turns the pull-to-refresh gesture off while the component is mounted (and `disabled` is true). */
|
|
14
|
+
export declare function useDisablePullToRefresh(disabled?: boolean): void;
|
|
15
|
+
/** Runs the page's refresh, as a pull would: the registered handler, or a reload. */
|
|
16
|
+
export declare function refreshPage(): Promise<void>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useRef } from "react";
|
|
2
|
+
const CHROME_READY_EVENT = "ob:chrome-ready";
|
|
3
|
+
function currentRefresh() {
|
|
4
|
+
return globalThis.ob?.chrome?.refresh;
|
|
5
|
+
}
|
|
6
|
+
function hasChrome() {
|
|
7
|
+
return Boolean(globalThis.ob?.chrome);
|
|
8
|
+
}
|
|
9
|
+
/** Calls `use` once the chrome is on the page and returns a function that undoes it. */
|
|
10
|
+
function withRefresh(use) {
|
|
11
|
+
let cleanup;
|
|
12
|
+
let run = () => {
|
|
13
|
+
let refresh = currentRefresh();
|
|
14
|
+
if (refresh)
|
|
15
|
+
cleanup = use(refresh);
|
|
16
|
+
};
|
|
17
|
+
if (hasChrome() || typeof window === "undefined") {
|
|
18
|
+
run();
|
|
19
|
+
return () => cleanup?.();
|
|
20
|
+
}
|
|
21
|
+
window.addEventListener(CHROME_READY_EVENT, run, { once: true });
|
|
22
|
+
return () => {
|
|
23
|
+
window.removeEventListener(CHROME_READY_EVENT, run);
|
|
24
|
+
cleanup?.();
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Makes pull-to-refresh run `handler` instead of reloading the page, while the component is
|
|
29
|
+
* mounted. The indicator spins until the returned promise settles. The latest render's handler is
|
|
30
|
+
* used; pass `null` or `false` to leave the page reload in place (for a page that only sometimes
|
|
31
|
+
* can reload its own data). Nested components win: the one mounted last is the one that runs.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```tsx
|
|
35
|
+
* usePullToRefresh(() => Promise.all([dsTimesheet.refreshDataSource(), dsActivities.refreshDataSource()]));
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export function usePullToRefresh(handler) {
|
|
39
|
+
let latest = useRef(handler);
|
|
40
|
+
useLayoutEffect(() => {
|
|
41
|
+
latest.current = handler;
|
|
42
|
+
});
|
|
43
|
+
let active = Boolean(handler);
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
if (!active)
|
|
46
|
+
return;
|
|
47
|
+
return withRefresh((refresh) => refresh.register(() => (latest.current ? latest.current() : undefined)));
|
|
48
|
+
}, [active]);
|
|
49
|
+
}
|
|
50
|
+
/** Turns the pull-to-refresh gesture off while the component is mounted (and `disabled` is true). */
|
|
51
|
+
export function useDisablePullToRefresh(disabled = true) {
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (!disabled)
|
|
54
|
+
return;
|
|
55
|
+
return withRefresh((refresh) => refresh.disable());
|
|
56
|
+
}, [disabled]);
|
|
57
|
+
}
|
|
58
|
+
/** Runs the page's refresh, as a pull would: the registered handler, or a reload. */
|
|
59
|
+
export function refreshPage() {
|
|
60
|
+
let refresh = currentRefresh();
|
|
61
|
+
if (refresh)
|
|
62
|
+
return refresh.refresh();
|
|
63
|
+
window.location.reload();
|
|
64
|
+
return Promise.resolve();
|
|
65
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as monaco from "monaco-editor";
|
|
2
|
+
import "./CodeEditor.css";
|
|
3
|
+
export type CodeDiffEditorHandle = {
|
|
4
|
+
/** The Monaco diff editor instance, once mounted. */
|
|
5
|
+
editor: monaco.editor.IStandaloneDiffEditor | null;
|
|
6
|
+
/** Focuses the modified (editable) side. */
|
|
7
|
+
focus(): void;
|
|
8
|
+
};
|
|
9
|
+
export type CodeDiffEditorProps = {
|
|
10
|
+
/** The text compared against (left / removed side). Never editable. */
|
|
11
|
+
original: string;
|
|
12
|
+
/** The current text (right / added side). Controlled: `onChange` reports every edit. */
|
|
13
|
+
value: string;
|
|
14
|
+
onChange?: (value: string) => void;
|
|
15
|
+
/** Monaco language ID for both sides, e.g. `html`, `css`, `json`, `markdown`. */
|
|
16
|
+
language?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Identifies the document. When it changes, both sides get fresh models, so undo history does
|
|
19
|
+
* not leak from one record to the next. Without it, a new `value` is applied as an edit that
|
|
20
|
+
* can be undone.
|
|
21
|
+
*/
|
|
22
|
+
docKey?: string;
|
|
23
|
+
/** Makes the modified side read-only. The original side is always read-only. */
|
|
24
|
+
readOnly?: boolean;
|
|
25
|
+
/** One column with changes inline (default), or two columns side by side. */
|
|
26
|
+
inline?: boolean;
|
|
27
|
+
/** Ctrl/Cmd+S inside the modified side. */
|
|
28
|
+
onSave?: () => void;
|
|
29
|
+
/** Extra Monaco options, applied on mount and whenever the object changes. */
|
|
30
|
+
options?: monaco.editor.IDiffEditorOptions;
|
|
31
|
+
className?: string;
|
|
32
|
+
style?: React.CSSProperties;
|
|
33
|
+
"aria-label"?: string;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Monaco diff editor that follows the site's colour scheme: `original` against an editable
|
|
37
|
+
* `value`. Unchanged regions collapse, so long documents show only what differs. The element fills
|
|
38
|
+
* its parent's size through `className`/`style`; give it a height.
|
|
39
|
+
*/
|
|
40
|
+
export declare const CodeDiffEditor: import("react").ForwardRefExoticComponent<CodeDiffEditorProps & import("react").RefAttributes<CodeDiffEditorHandle>>;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import clsx from "clsx";
|
|
3
|
+
import * as monaco from "monaco-editor";
|
|
4
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
|
|
5
|
+
import { useResolvedColorScheme } from "../../colorScheme.js";
|
|
6
|
+
import "./CodeEditor.css";
|
|
7
|
+
function diffOptions(inline, readOnly, options) {
|
|
8
|
+
return {
|
|
9
|
+
renderSideBySide: !inline,
|
|
10
|
+
// Monaco otherwise drops to the inline view below 900px, which would ignore `inline={false}`.
|
|
11
|
+
useInlineViewWhenSpaceIsLimited: inline ? undefined : false,
|
|
12
|
+
readOnly,
|
|
13
|
+
...options,
|
|
14
|
+
originalEditable: false,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Monaco diff editor that follows the site's colour scheme: `original` against an editable
|
|
19
|
+
* `value`. Unchanged regions collapse, so long documents show only what differs. The element fills
|
|
20
|
+
* its parent's size through `className`/`style`; give it a height.
|
|
21
|
+
*/
|
|
22
|
+
export const CodeDiffEditor = forwardRef(function CodeDiffEditor({ original, value, onChange, language = "plaintext", docKey, readOnly = false, inline = true, onSave, options, className, style, "aria-label": ariaLabel, }, ref) {
|
|
23
|
+
let container = useRef(null);
|
|
24
|
+
let editor = useRef(null);
|
|
25
|
+
// Latest callbacks without re-creating the editor.
|
|
26
|
+
let callbacks = useRef({ onChange, onSave });
|
|
27
|
+
callbacks.current = { onChange, onSave };
|
|
28
|
+
// Set while the editor applies a `value` from props, so that edit is not reported back.
|
|
29
|
+
let applyingValue = useRef(false);
|
|
30
|
+
let shownDoc = useRef(undefined);
|
|
31
|
+
let colorScheme = useResolvedColorScheme();
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
if (!container.current)
|
|
34
|
+
return;
|
|
35
|
+
let instance = monaco.editor.createDiffEditor(container.current, {
|
|
36
|
+
theme: colorScheme === "dark" ? "vs-dark" : "vs",
|
|
37
|
+
automaticLayout: true,
|
|
38
|
+
minimap: { enabled: false },
|
|
39
|
+
scrollBeyondLastLine: false,
|
|
40
|
+
fixedOverflowWidgets: true,
|
|
41
|
+
renderOverviewRuler: true,
|
|
42
|
+
hideUnchangedRegions: { enabled: true },
|
|
43
|
+
modifiedAriaLabel: ariaLabel,
|
|
44
|
+
...diffOptions(inline, readOnly, options),
|
|
45
|
+
});
|
|
46
|
+
instance.setModel({
|
|
47
|
+
original: monaco.editor.createModel(original, language),
|
|
48
|
+
modified: monaco.editor.createModel(value, language),
|
|
49
|
+
});
|
|
50
|
+
editor.current = instance;
|
|
51
|
+
shownDoc.current = docKey;
|
|
52
|
+
let modifiedEditor = instance.getModifiedEditor();
|
|
53
|
+
let changeListener = modifiedEditor.onDidChangeModelContent(() => {
|
|
54
|
+
if (!applyingValue.current) {
|
|
55
|
+
callbacks.current.onChange?.(modifiedEditor.getValue());
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
modifiedEditor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => callbacks.current.onSave?.());
|
|
59
|
+
return () => {
|
|
60
|
+
changeListener.dispose();
|
|
61
|
+
// The diff editor does not own its models; dispose them once nothing shows them.
|
|
62
|
+
let models = instance.getModel();
|
|
63
|
+
instance.dispose();
|
|
64
|
+
models?.original.dispose();
|
|
65
|
+
models?.modified.dispose();
|
|
66
|
+
editor.current = null;
|
|
67
|
+
};
|
|
68
|
+
// The editor is created once; every prop is synced by the effects below.
|
|
69
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
70
|
+
}, []);
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
let instance = editor.current;
|
|
73
|
+
if (!instance)
|
|
74
|
+
return;
|
|
75
|
+
applyingValue.current = true;
|
|
76
|
+
try {
|
|
77
|
+
if (shownDoc.current !== docKey) {
|
|
78
|
+
// New document: new models, so undo history stays with the document it belongs to.
|
|
79
|
+
let old = instance.getModel();
|
|
80
|
+
instance.setModel({
|
|
81
|
+
original: monaco.editor.createModel(original, language),
|
|
82
|
+
modified: monaco.editor.createModel(value, language),
|
|
83
|
+
});
|
|
84
|
+
old?.original.dispose();
|
|
85
|
+
old?.modified.dispose();
|
|
86
|
+
shownDoc.current = docKey;
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
let models = instance.getModel();
|
|
90
|
+
if (!models)
|
|
91
|
+
return;
|
|
92
|
+
// The original side is never edited, so there is no undo history to keep.
|
|
93
|
+
if (models.original.getValue() !== original) {
|
|
94
|
+
models.original.setValue(original);
|
|
95
|
+
}
|
|
96
|
+
if (models.modified.getValue() !== value) {
|
|
97
|
+
// Same document, changed from outside (cancel, reload): one undoable edit.
|
|
98
|
+
instance
|
|
99
|
+
.getModifiedEditor()
|
|
100
|
+
.executeEdits("external", [{ range: models.modified.getFullModelRange(), text: value }]);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
applyingValue.current = false;
|
|
105
|
+
}
|
|
106
|
+
}, [docKey, original, value, language]);
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
let models = editor.current?.getModel();
|
|
109
|
+
if (!models)
|
|
110
|
+
return;
|
|
111
|
+
for (let model of [models.original, models.modified]) {
|
|
112
|
+
if (model.getLanguageId() !== language) {
|
|
113
|
+
monaco.editor.setModelLanguage(model, language);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}, [language]);
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
let instance = editor.current;
|
|
119
|
+
if (!instance)
|
|
120
|
+
return;
|
|
121
|
+
instance.updateOptions(diffOptions(inline, readOnly, options));
|
|
122
|
+
instance.getModifiedEditor().updateOptions({ ariaLabel });
|
|
123
|
+
}, [inline, readOnly, ariaLabel, options]);
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
// The theme is global to every editor on the page.
|
|
126
|
+
monaco.editor.setTheme(colorScheme === "dark" ? "vs-dark" : "vs");
|
|
127
|
+
}, [colorScheme]);
|
|
128
|
+
useImperativeHandle(ref, () => ({
|
|
129
|
+
get editor() {
|
|
130
|
+
return editor.current;
|
|
131
|
+
},
|
|
132
|
+
focus() {
|
|
133
|
+
editor.current?.getModifiedEditor().focus();
|
|
134
|
+
},
|
|
135
|
+
}));
|
|
136
|
+
// `monaco-component`: see CodeEditor - Monaco's context menu needs the theme variables it defines on that class.
|
|
137
|
+
return _jsx("div", { ref: container, className: clsx("ObCodeEditor-root", "monaco-component", className), style: style });
|
|
138
|
+
});
|
package/es/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export { MergeProgressDialogDs, type MergeProgressDialogDsProps, } from "./compo
|
|
|
11
11
|
export { type MergeJobOptions, type MergePhase, useMergeJob } from "./components/MergeProgressDialog/useMergeJob.js";
|
|
12
12
|
export { type AssistantTool, type AssistantToolParameters, isAssistantEnabled, openAssistant, useAssistantContext, useAssistantInstructions, useAssistantTool, } from "./chrome/assistant.js";
|
|
13
13
|
export { type OpenReportOptions, openReport } from "./chrome/openReport.js";
|
|
14
|
+
export { refreshPage, useDisablePullToRefresh, usePullToRefresh } from "./chrome/refresh.js";
|
|
14
15
|
export { BannerImage as PageBannerImage, type BannerImageProps, PageBanner, type PageBannerProps, Tab as PageBannerTab, TabList as PageBannerTabList, type TabListProps as PageBannerTabListProps, type TabProps as PageBannerTabProps, Title as PageBannerTitle, type TitleProps as PageBannerTitleProps, } from "./components/PageBanner/index.js";
|
|
15
16
|
export { Portal } from "./components/Portal/index.js";
|
|
16
17
|
export { ToolbarBreadcrumbs, ToolbarDrawerToggle, ToolbarPortal, type BreadcrumbItem, type ToolbarBreadcrumbsProps, type ToolbarDrawerToggleProps, type ToolbarPortalProps, } from "./components/Toolbar/index.js";
|
package/es/index.js
CHANGED
|
@@ -13,6 +13,7 @@ export { MergeProgressDialogDs, } from "./components/MergeProgressDialog/MergePr
|
|
|
13
13
|
export { useMergeJob } from "./components/MergeProgressDialog/useMergeJob.js";
|
|
14
14
|
export { isAssistantEnabled, openAssistant, useAssistantContext, useAssistantInstructions, useAssistantTool, } from "./chrome/assistant.js";
|
|
15
15
|
export { openReport } from "./chrome/openReport.js";
|
|
16
|
+
export { refreshPage, useDisablePullToRefresh, usePullToRefresh } from "./chrome/refresh.js";
|
|
16
17
|
export { BannerImage as PageBannerImage, PageBanner, Tab as PageBannerTab, TabList as PageBannerTabList, Title as PageBannerTitle, } from "./components/PageBanner/index.js";
|
|
17
18
|
// PdfViewer is intentionally not re-exported here. It references the pdf.js worker
|
|
18
19
|
// with `new URL(..., import.meta.url)`, which makes bundlers emit the 1.2 MB worker
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olenbetong/synergi-react",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.8.0",
|
|
4
4
|
"description": "Standalone React component for SynergiWeb and Partner Portal",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -58,14 +58,17 @@
|
|
|
58
58
|
"@navikt/aksel-icons": ">=8.16.1",
|
|
59
59
|
"react-router": ">=8.3.0"
|
|
60
60
|
},
|
|
61
|
-
"sideEffects":
|
|
61
|
+
"sideEffects": [
|
|
62
|
+
"**/*.css",
|
|
63
|
+
"./es/elements-ds/index.js"
|
|
64
|
+
],
|
|
62
65
|
"dependencies": {
|
|
63
66
|
"@emotion/styled": "^11.14.1",
|
|
64
67
|
"@mui/icons-material": "9.5.0",
|
|
65
68
|
"@mui/material": "9.5.0",
|
|
66
|
-
"@olenbetong/appframe-core": "^2.16.
|
|
69
|
+
"@olenbetong/appframe-core": "^2.16.2",
|
|
67
70
|
"@olenbetong/appframe-data": "^1.10.3",
|
|
68
|
-
"@olenbetong/appframe-ds": "^1.
|
|
71
|
+
"@olenbetong/appframe-ds": "^1.14.2",
|
|
69
72
|
"@olenbetong/appframe-mui": "^7.0.13",
|
|
70
73
|
"@olenbetong/appframe-react": "^2.2.8",
|
|
71
74
|
"clsx": "^2.1.1",
|
|
@@ -119,11 +122,10 @@
|
|
|
119
122
|
},
|
|
120
123
|
"optionalDependencies": {},
|
|
121
124
|
"scripts": {
|
|
122
|
-
"build": "pnpm run build:tsc && pnpm
|
|
125
|
+
"build": "pnpm run build:tsc && pnpm run build:sass:components && pnpm test && pnpm run build:sass",
|
|
123
126
|
"build:sass": "sass ./src/index.scss ./dist/styles/styles.css --source-map-urls=relative --style compressed",
|
|
124
127
|
"build:sass:components": "node ./scripts/buildSassComponents.mjs",
|
|
125
|
-
"build:tsc
|
|
126
|
-
"build:tsc": "node --no-warnings ../../scripts/clean.ts ./es tsconfig.build.tsbuildinfo && tsc -p tsconfig.build.json",
|
|
128
|
+
"build:tsc": "node --no-warnings ../../scripts/clean.ts tsconfig.build.tsbuildinfo && tsc -p tsconfig.build.json && node ./scripts/replaceCss.mjs && node --no-warnings ../../scripts/prune-output.ts ./es ./src",
|
|
127
129
|
"test": "tsc -p tsconfig.test.json && vitest run",
|
|
128
130
|
"test:browser": "vitest run --config vitest.browser.config.ts",
|
|
129
131
|
"test:coverage": "vitest run --coverage"
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { render } from "@testing-library/react";
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { useDisablePullToRefresh, usePullToRefresh } from "./refresh.js";
|
|
5
|
+
|
|
6
|
+
function installChrome() {
|
|
7
|
+
let handlers: Array<() => unknown> = [];
|
|
8
|
+
let disabled = 0;
|
|
9
|
+
let refresh = {
|
|
10
|
+
register: vi.fn((handler: () => unknown) => {
|
|
11
|
+
handlers.push(handler);
|
|
12
|
+
return () => handlers.splice(handlers.indexOf(handler), 1);
|
|
13
|
+
}),
|
|
14
|
+
refresh: async () => {
|
|
15
|
+
await handlers[handlers.length - 1]?.();
|
|
16
|
+
},
|
|
17
|
+
disable: vi.fn(() => {
|
|
18
|
+
disabled++;
|
|
19
|
+
return () => disabled--;
|
|
20
|
+
}),
|
|
21
|
+
get enabled() {
|
|
22
|
+
return disabled === 0;
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
(globalThis as { ob?: unknown }).ob = { chrome: { refresh } };
|
|
26
|
+
return refresh;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function Refreshing({ handler }: { handler: () => unknown }) {
|
|
30
|
+
usePullToRefresh(handler);
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function NoPull() {
|
|
35
|
+
useDisablePullToRefresh();
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe("usePullToRefresh", () => {
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
delete (globalThis as { ob?: unknown }).ob;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("registers the latest handler while mounted", async () => {
|
|
45
|
+
let refresh = installChrome();
|
|
46
|
+
let first = vi.fn();
|
|
47
|
+
let second = vi.fn();
|
|
48
|
+
let { rerender, unmount } = render(<Refreshing handler={first} />);
|
|
49
|
+
rerender(<Refreshing handler={second} />);
|
|
50
|
+
|
|
51
|
+
await refresh.refresh();
|
|
52
|
+
expect(second).toHaveBeenCalledTimes(1);
|
|
53
|
+
expect(first).not.toHaveBeenCalled();
|
|
54
|
+
expect(refresh.register).toHaveBeenCalledTimes(1);
|
|
55
|
+
|
|
56
|
+
unmount();
|
|
57
|
+
await refresh.refresh();
|
|
58
|
+
expect(second).toHaveBeenCalledTimes(1);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("waits for the chrome when the app mounts first", async () => {
|
|
62
|
+
let handler = vi.fn();
|
|
63
|
+
render(<Refreshing handler={handler} />);
|
|
64
|
+
|
|
65
|
+
let refresh = installChrome();
|
|
66
|
+
window.dispatchEvent(new CustomEvent("ob:chrome-ready"));
|
|
67
|
+
await refresh.refresh();
|
|
68
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("turns the gesture off while mounted", () => {
|
|
72
|
+
let refresh = installChrome();
|
|
73
|
+
let { unmount } = render(<NoPull />);
|
|
74
|
+
expect(refresh.enabled).toBe(false);
|
|
75
|
+
unmount();
|
|
76
|
+
expect(refresh.enabled).toBe(true);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Pulling down at the top of a page on a 2026 template reloads the page: the site chrome
|
|
5
|
+
* (`@olenbetong/synergi-chrome`) owns the gesture. An app that can reload just its data offers its
|
|
6
|
+
* own refresh instead, and a page with vertical gestures of its own turns the pull off. These hooks
|
|
7
|
+
* forward to `window.ob.chrome.refresh`, waiting for the chrome to mount (`ob:chrome-ready`) when
|
|
8
|
+
* the app starts first. On a page without the chrome they do nothing.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
type RefreshApi = {
|
|
12
|
+
register: (handler: () => unknown | Promise<unknown>) => () => void;
|
|
13
|
+
refresh: () => Promise<void>;
|
|
14
|
+
disable: () => () => void;
|
|
15
|
+
readonly enabled: boolean;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const CHROME_READY_EVENT = "ob:chrome-ready";
|
|
19
|
+
|
|
20
|
+
function currentRefresh(): RefreshApi | undefined {
|
|
21
|
+
return (globalThis as { ob?: { chrome?: { refresh?: RefreshApi } } }).ob?.chrome?.refresh;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function hasChrome() {
|
|
25
|
+
return Boolean((globalThis as { ob?: { chrome?: unknown } }).ob?.chrome);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Calls `use` once the chrome is on the page and returns a function that undoes it. */
|
|
29
|
+
function withRefresh(use: (refresh: RefreshApi) => void | (() => void)): () => void {
|
|
30
|
+
let cleanup: void | (() => void);
|
|
31
|
+
let run = () => {
|
|
32
|
+
let refresh = currentRefresh();
|
|
33
|
+
if (refresh) cleanup = use(refresh);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
if (hasChrome() || typeof window === "undefined") {
|
|
37
|
+
run();
|
|
38
|
+
return () => cleanup?.();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
window.addEventListener(CHROME_READY_EVENT, run, { once: true });
|
|
42
|
+
return () => {
|
|
43
|
+
window.removeEventListener(CHROME_READY_EVENT, run);
|
|
44
|
+
cleanup?.();
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Makes pull-to-refresh run `handler` instead of reloading the page, while the component is
|
|
50
|
+
* mounted. The indicator spins until the returned promise settles. The latest render's handler is
|
|
51
|
+
* used; pass `null` or `false` to leave the page reload in place (for a page that only sometimes
|
|
52
|
+
* can reload its own data). Nested components win: the one mounted last is the one that runs.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```tsx
|
|
56
|
+
* usePullToRefresh(() => Promise.all([dsTimesheet.refreshDataSource(), dsActivities.refreshDataSource()]));
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export function usePullToRefresh(handler: (() => unknown | Promise<unknown>) | null | false | undefined) {
|
|
60
|
+
let latest = useRef(handler);
|
|
61
|
+
useLayoutEffect(() => {
|
|
62
|
+
latest.current = handler;
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
let active = Boolean(handler);
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
if (!active) return;
|
|
68
|
+
return withRefresh((refresh) => refresh.register(() => (latest.current ? latest.current() : undefined)));
|
|
69
|
+
}, [active]);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Turns the pull-to-refresh gesture off while the component is mounted (and `disabled` is true). */
|
|
73
|
+
export function useDisablePullToRefresh(disabled = true) {
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
if (!disabled) return;
|
|
76
|
+
return withRefresh((refresh) => refresh.disable());
|
|
77
|
+
}, [disabled]);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Runs the page's refresh, as a pull would: the registered handler, or a reload. */
|
|
81
|
+
export function refreshPage(): Promise<void> {
|
|
82
|
+
let refresh = currentRefresh();
|
|
83
|
+
if (refresh) return refresh.refresh();
|
|
84
|
+
window.location.reload();
|
|
85
|
+
return Promise.resolve();
|
|
86
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import clsx from "clsx";
|
|
2
|
+
import * as monaco from "monaco-editor";
|
|
3
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
|
|
4
|
+
|
|
5
|
+
import { useResolvedColorScheme } from "../../colorScheme.js";
|
|
6
|
+
import "./CodeEditor.css";
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
* Where Monaco comes from is described in CodeEditor.tsx. The diff editor needs nothing more: the
|
|
10
|
+
* platform's `monaco-editor` entry is the full build, and the diff is computed in the editor worker
|
|
11
|
+
* its `MonacoEnvironment` already serves.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export type CodeDiffEditorHandle = {
|
|
15
|
+
/** The Monaco diff editor instance, once mounted. */
|
|
16
|
+
editor: monaco.editor.IStandaloneDiffEditor | null;
|
|
17
|
+
/** Focuses the modified (editable) side. */
|
|
18
|
+
focus(): void;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type CodeDiffEditorProps = {
|
|
22
|
+
/** The text compared against (left / removed side). Never editable. */
|
|
23
|
+
original: string;
|
|
24
|
+
/** The current text (right / added side). Controlled: `onChange` reports every edit. */
|
|
25
|
+
value: string;
|
|
26
|
+
onChange?: (value: string) => void;
|
|
27
|
+
/** Monaco language ID for both sides, e.g. `html`, `css`, `json`, `markdown`. */
|
|
28
|
+
language?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Identifies the document. When it changes, both sides get fresh models, so undo history does
|
|
31
|
+
* not leak from one record to the next. Without it, a new `value` is applied as an edit that
|
|
32
|
+
* can be undone.
|
|
33
|
+
*/
|
|
34
|
+
docKey?: string;
|
|
35
|
+
/** Makes the modified side read-only. The original side is always read-only. */
|
|
36
|
+
readOnly?: boolean;
|
|
37
|
+
/** One column with changes inline (default), or two columns side by side. */
|
|
38
|
+
inline?: boolean;
|
|
39
|
+
/** Ctrl/Cmd+S inside the modified side. */
|
|
40
|
+
onSave?: () => void;
|
|
41
|
+
/** Extra Monaco options, applied on mount and whenever the object changes. */
|
|
42
|
+
options?: monaco.editor.IDiffEditorOptions;
|
|
43
|
+
className?: string;
|
|
44
|
+
style?: React.CSSProperties;
|
|
45
|
+
"aria-label"?: string;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
function diffOptions(
|
|
49
|
+
inline: boolean,
|
|
50
|
+
readOnly: boolean,
|
|
51
|
+
options: monaco.editor.IDiffEditorOptions | undefined,
|
|
52
|
+
): monaco.editor.IDiffEditorOptions {
|
|
53
|
+
return {
|
|
54
|
+
renderSideBySide: !inline,
|
|
55
|
+
// Monaco otherwise drops to the inline view below 900px, which would ignore `inline={false}`.
|
|
56
|
+
useInlineViewWhenSpaceIsLimited: inline ? undefined : false,
|
|
57
|
+
readOnly,
|
|
58
|
+
...options,
|
|
59
|
+
originalEditable: false,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Monaco diff editor that follows the site's colour scheme: `original` against an editable
|
|
65
|
+
* `value`. Unchanged regions collapse, so long documents show only what differs. The element fills
|
|
66
|
+
* its parent's size through `className`/`style`; give it a height.
|
|
67
|
+
*/
|
|
68
|
+
export const CodeDiffEditor = forwardRef<CodeDiffEditorHandle, CodeDiffEditorProps>(function CodeDiffEditor(
|
|
69
|
+
{
|
|
70
|
+
original,
|
|
71
|
+
value,
|
|
72
|
+
onChange,
|
|
73
|
+
language = "plaintext",
|
|
74
|
+
docKey,
|
|
75
|
+
readOnly = false,
|
|
76
|
+
inline = true,
|
|
77
|
+
onSave,
|
|
78
|
+
options,
|
|
79
|
+
className,
|
|
80
|
+
style,
|
|
81
|
+
"aria-label": ariaLabel,
|
|
82
|
+
},
|
|
83
|
+
ref,
|
|
84
|
+
) {
|
|
85
|
+
let container = useRef<HTMLDivElement>(null);
|
|
86
|
+
let editor = useRef<monaco.editor.IStandaloneDiffEditor | null>(null);
|
|
87
|
+
// Latest callbacks without re-creating the editor.
|
|
88
|
+
let callbacks = useRef({ onChange, onSave });
|
|
89
|
+
callbacks.current = { onChange, onSave };
|
|
90
|
+
// Set while the editor applies a `value` from props, so that edit is not reported back.
|
|
91
|
+
let applyingValue = useRef(false);
|
|
92
|
+
let shownDoc = useRef<string | undefined>(undefined);
|
|
93
|
+
let colorScheme = useResolvedColorScheme();
|
|
94
|
+
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
if (!container.current) return;
|
|
97
|
+
let instance = monaco.editor.createDiffEditor(container.current, {
|
|
98
|
+
theme: colorScheme === "dark" ? "vs-dark" : "vs",
|
|
99
|
+
automaticLayout: true,
|
|
100
|
+
minimap: { enabled: false },
|
|
101
|
+
scrollBeyondLastLine: false,
|
|
102
|
+
fixedOverflowWidgets: true,
|
|
103
|
+
renderOverviewRuler: true,
|
|
104
|
+
hideUnchangedRegions: { enabled: true },
|
|
105
|
+
modifiedAriaLabel: ariaLabel,
|
|
106
|
+
...diffOptions(inline, readOnly, options),
|
|
107
|
+
});
|
|
108
|
+
instance.setModel({
|
|
109
|
+
original: monaco.editor.createModel(original, language),
|
|
110
|
+
modified: monaco.editor.createModel(value, language),
|
|
111
|
+
});
|
|
112
|
+
editor.current = instance;
|
|
113
|
+
shownDoc.current = docKey;
|
|
114
|
+
|
|
115
|
+
let modifiedEditor = instance.getModifiedEditor();
|
|
116
|
+
let changeListener = modifiedEditor.onDidChangeModelContent(() => {
|
|
117
|
+
if (!applyingValue.current) {
|
|
118
|
+
callbacks.current.onChange?.(modifiedEditor.getValue());
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
modifiedEditor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => callbacks.current.onSave?.());
|
|
122
|
+
|
|
123
|
+
return () => {
|
|
124
|
+
changeListener.dispose();
|
|
125
|
+
// The diff editor does not own its models; dispose them once nothing shows them.
|
|
126
|
+
let models = instance.getModel();
|
|
127
|
+
instance.dispose();
|
|
128
|
+
models?.original.dispose();
|
|
129
|
+
models?.modified.dispose();
|
|
130
|
+
editor.current = null;
|
|
131
|
+
};
|
|
132
|
+
// The editor is created once; every prop is synced by the effects below.
|
|
133
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
134
|
+
}, []);
|
|
135
|
+
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
let instance = editor.current;
|
|
138
|
+
if (!instance) return;
|
|
139
|
+
|
|
140
|
+
applyingValue.current = true;
|
|
141
|
+
try {
|
|
142
|
+
if (shownDoc.current !== docKey) {
|
|
143
|
+
// New document: new models, so undo history stays with the document it belongs to.
|
|
144
|
+
let old = instance.getModel();
|
|
145
|
+
instance.setModel({
|
|
146
|
+
original: monaco.editor.createModel(original, language),
|
|
147
|
+
modified: monaco.editor.createModel(value, language),
|
|
148
|
+
});
|
|
149
|
+
old?.original.dispose();
|
|
150
|
+
old?.modified.dispose();
|
|
151
|
+
shownDoc.current = docKey;
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let models = instance.getModel();
|
|
156
|
+
if (!models) return;
|
|
157
|
+
// The original side is never edited, so there is no undo history to keep.
|
|
158
|
+
if (models.original.getValue() !== original) {
|
|
159
|
+
models.original.setValue(original);
|
|
160
|
+
}
|
|
161
|
+
if (models.modified.getValue() !== value) {
|
|
162
|
+
// Same document, changed from outside (cancel, reload): one undoable edit.
|
|
163
|
+
instance
|
|
164
|
+
.getModifiedEditor()
|
|
165
|
+
.executeEdits("external", [{ range: models.modified.getFullModelRange(), text: value }]);
|
|
166
|
+
}
|
|
167
|
+
} finally {
|
|
168
|
+
applyingValue.current = false;
|
|
169
|
+
}
|
|
170
|
+
}, [docKey, original, value, language]);
|
|
171
|
+
|
|
172
|
+
useEffect(() => {
|
|
173
|
+
let models = editor.current?.getModel();
|
|
174
|
+
if (!models) return;
|
|
175
|
+
for (let model of [models.original, models.modified]) {
|
|
176
|
+
if (model.getLanguageId() !== language) {
|
|
177
|
+
monaco.editor.setModelLanguage(model, language);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}, [language]);
|
|
181
|
+
|
|
182
|
+
useEffect(() => {
|
|
183
|
+
let instance = editor.current;
|
|
184
|
+
if (!instance) return;
|
|
185
|
+
instance.updateOptions(diffOptions(inline, readOnly, options));
|
|
186
|
+
instance.getModifiedEditor().updateOptions({ ariaLabel });
|
|
187
|
+
}, [inline, readOnly, ariaLabel, options]);
|
|
188
|
+
|
|
189
|
+
useEffect(() => {
|
|
190
|
+
// The theme is global to every editor on the page.
|
|
191
|
+
monaco.editor.setTheme(colorScheme === "dark" ? "vs-dark" : "vs");
|
|
192
|
+
}, [colorScheme]);
|
|
193
|
+
|
|
194
|
+
useImperativeHandle(ref, () => ({
|
|
195
|
+
get editor() {
|
|
196
|
+
return editor.current;
|
|
197
|
+
},
|
|
198
|
+
focus() {
|
|
199
|
+
editor.current?.getModifiedEditor().focus();
|
|
200
|
+
},
|
|
201
|
+
}));
|
|
202
|
+
|
|
203
|
+
// `monaco-component`: see CodeEditor - Monaco's context menu needs the theme variables it defines on that class.
|
|
204
|
+
return <div ref={container} className={clsx("ObCodeEditor-root", "monaco-component", className)} style={style} />;
|
|
205
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -58,6 +58,7 @@ export {
|
|
|
58
58
|
useAssistantTool,
|
|
59
59
|
} from "./chrome/assistant.js";
|
|
60
60
|
export { type OpenReportOptions, openReport } from "./chrome/openReport.js";
|
|
61
|
+
export { refreshPage, useDisablePullToRefresh, usePullToRefresh } from "./chrome/refresh.js";
|
|
61
62
|
export {
|
|
62
63
|
BannerImage as PageBannerImage,
|
|
63
64
|
type BannerImageProps,
|
package/src/test/setup.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import "@testing-library/jest-dom/
|
|
1
|
+
import * as matchers from "@testing-library/jest-dom/matchers";
|
|
2
2
|
|
|
3
3
|
import type { TestingLibraryMatchers } from "@testing-library/jest-dom/matchers";
|
|
4
4
|
|
|
5
5
|
import { cleanup } from "@testing-library/react";
|
|
6
|
-
import { afterEach } from "vitest";
|
|
6
|
+
import { afterEach, expect } from "vitest";
|
|
7
|
+
|
|
8
|
+
expect.extend(matchers);
|
|
7
9
|
|
|
8
10
|
afterEach(() => {
|
|
9
11
|
cleanup();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
|
2
2
|
|
|
3
3
|
import { formatMinutes, formatPeriod, fromInputs, getNewPeriod, toDateInput, toTimeInput } from "./formatting.js";
|
|
4
4
|
|
|
@@ -27,6 +27,15 @@ describe("formatMinutes", () => {
|
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
describe("formatPeriod", () => {
|
|
30
|
+
// The format follows the user's culture, and without one the machine's locale (12-hour on en-US).
|
|
31
|
+
beforeAll(() => {
|
|
32
|
+
vi.stubGlobal("af", { userSession: { culture: "nb-NO" } });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
afterAll(() => {
|
|
36
|
+
vi.unstubAllGlobals();
|
|
37
|
+
});
|
|
38
|
+
|
|
30
39
|
it("is empty without both ends of the period", () => {
|
|
31
40
|
expect(formatPeriod(null, new Date())).toBe("");
|
|
32
41
|
});
|