react-cheminfo 0.128.0 → 0.129.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.
Files changed (59) hide show
  1. package/README.md +79 -43
  2. package/lib/i18n/core/chromeCatalog.d.ts +1 -1
  3. package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
  4. package/lib/locales/de.json +9 -0
  5. package/lib/locales/en.json +9 -0
  6. package/lib/locales/es.json +9 -0
  7. package/lib/locales/fr.json +9 -0
  8. package/lib/locales/it.json +9 -0
  9. package/lib/share/core/exampleMark.d.ts +31 -0
  10. package/lib/share/core/exampleMark.d.ts.map +1 -0
  11. package/lib/share/core/exampleMark.js +31 -0
  12. package/lib/share/core/exampleMark.js.map +1 -0
  13. package/lib/share/core/index.d.ts +5 -0
  14. package/lib/share/core/index.d.ts.map +1 -1
  15. package/lib/share/core/index.js +3 -0
  16. package/lib/share/core/index.js.map +1 -1
  17. package/lib/share/core/linkBytes.d.ts +29 -0
  18. package/lib/share/core/linkBytes.d.ts.map +1 -0
  19. package/lib/share/core/linkBytes.js +70 -0
  20. package/lib/share/core/linkBytes.js.map +1 -0
  21. package/lib/share/core/linkValue.d.ts +34 -0
  22. package/lib/share/core/linkValue.d.ts.map +1 -0
  23. package/lib/share/core/linkValue.js +58 -0
  24. package/lib/share/core/linkValue.js.map +1 -0
  25. package/lib/share/core/opensOn.d.ts +133 -0
  26. package/lib/share/core/opensOn.d.ts.map +1 -0
  27. package/lib/share/core/opensOn.js +82 -0
  28. package/lib/share/core/opensOn.js.map +1 -0
  29. package/lib/share/core/seal.d.ts.map +1 -1
  30. package/lib/share/core/seal.js +3 -32
  31. package/lib/share/core/seal.js.map +1 -1
  32. package/lib/share/ui/ShareOpensOn.d.ts +32 -0
  33. package/lib/share/ui/ShareOpensOn.d.ts.map +1 -0
  34. package/lib/share/ui/ShareOpensOn.js +49 -0
  35. package/lib/share/ui/ShareOpensOn.js.map +1 -0
  36. package/lib/share/ui/index.d.ts +4 -0
  37. package/lib/share/ui/index.d.ts.map +1 -1
  38. package/lib/share/ui/index.js +2 -0
  39. package/lib/share/ui/index.js.map +1 -1
  40. package/lib/share/ui/useOpensOnShare.d.ts +65 -0
  41. package/lib/share/ui/useOpensOnShare.d.ts.map +1 -0
  42. package/lib/share/ui/useOpensOnShare.js +60 -0
  43. package/lib/share/ui/useOpensOnShare.js.map +1 -0
  44. package/package.json +1 -1
  45. package/src/locales/de.json +9 -0
  46. package/src/locales/en.json +9 -0
  47. package/src/locales/es.json +9 -0
  48. package/src/locales/fr.json +9 -0
  49. package/src/locales/it.json +9 -0
  50. package/src/share/core/exampleMark.ts +52 -0
  51. package/src/share/core/index.ts +25 -0
  52. package/src/share/core/linkBytes.ts +75 -0
  53. package/src/share/core/linkValue.ts +66 -0
  54. package/src/share/core/opensOn.ts +203 -0
  55. package/src/share/core/seal.ts +3 -36
  56. package/src/share/ui/ShareOpensOn.tsx +169 -0
  57. package/src/share/ui/index.ts +7 -0
  58. package/src/share/ui/useOpensOnShare.tsx +152 -0
  59. package/styles/chrome.css +18 -0
@@ -0,0 +1,32 @@
1
+ import type { ReactElement, ReactNode } from 'react';
2
+ import type { OpensOnChoice, OpensOnLinks, ShareExample } from '../core/opensOn.ts';
3
+ /** Props of {@link ShareOpensOn}. */
4
+ export interface ShareOpensOnProps {
5
+ /** What the dialog can hand out for the page on screen. */
6
+ links: OpensOnLinks;
7
+ /** What the link opens on. */
8
+ choice: OpensOnChoice;
9
+ /** Called with the choice the reader makes. */
10
+ onChange: (choice: OpensOnChoice) => void;
11
+ /** The examples a link can open on, in the order they are listed. */
12
+ examples: readonly ShareExample[];
13
+ /**
14
+ * What the site calls what is on screen: "This circuit", "This drawing".
15
+ * @default the chrome's own words
16
+ */
17
+ screenLabel?: string;
18
+ /**
19
+ * What a site adds under the choice: the style a link sets, say.
20
+ * @default undefined — nothing
21
+ */
22
+ children?: ReactNode;
23
+ }
24
+ /**
25
+ * The section of the Share dialog saying what the link opens on: what is on
26
+ * screen, when a link can carry it; any of the examples, whatever is on
27
+ * screen; or the page alone.
28
+ * @param props - See {@link ShareOpensOnProps}.
29
+ * @returns The section's content.
30
+ */
31
+ export declare function ShareOpensOn(props: ShareOpensOnProps): ReactElement;
32
+ //# sourceMappingURL=ShareOpensOn.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ShareOpensOn.d.ts","sourceRoot":"","sources":["../../../src/share/ui/ShareOpensOn.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAIrD,OAAO,KAAK,EACV,aAAa,EACb,YAAY,EAEZ,YAAY,EACb,MAAM,oBAAoB,CAAC;AAE5B,qCAAqC;AACrC,MAAM,WAAW,iBAAiB;IAChC,2DAA2D;IAC3D,KAAK,EAAE,YAAY,CAAC;IACpB,8BAA8B;IAC9B,MAAM,EAAE,aAAa,CAAC;IACtB,+CAA+C;IAC/C,QAAQ,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,IAAI,CAAC;IAC1C,qEAAqE;IACrE,QAAQ,EAAE,SAAS,YAAY,EAAE,CAAC;IAClC;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,iBAAiB,GAAG,YAAY,CAkFnE"}
@@ -0,0 +1,49 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { H6, HTMLSelect, Radio } from '@blueprintjs/core';
3
+ import { useId } from 'react';
4
+ import { useChromeT } from "../../i18n/ui/useT.js";
5
+ /**
6
+ * The section of the Share dialog saying what the link opens on: what is on
7
+ * screen, when a link can carry it; any of the examples, whatever is on
8
+ * screen; or the page alone.
9
+ * @param props - See {@link ShareOpensOnProps}.
10
+ * @returns The section's content.
11
+ */
12
+ export function ShareOpensOn(props) {
13
+ const { links, choice, onChange, examples, screenLabel, children } = props;
14
+ const t = useChromeT();
15
+ const group = useId();
16
+ const select = (opensOn) => () => {
17
+ onChange({ ...choice, opensOn });
18
+ };
19
+ const tooLarge = links.screen === 'too-large';
20
+ const shown = examples.find((example) => example.id === choice.example);
21
+ const changed = links.changed && choice.example === links.example;
22
+ return (_jsxs("div", { role: "radiogroup", "aria-label": t('share.opensOn'), className: "share-opens-on", children: [_jsx(H6, { children: t('share.opensOn') }), links.screen === undefined ? null : (_jsx(Radio, { name: group, value: "screen", checked: choice.opensOn === 'screen', disabled: tooLarge, onChange: select('screen'), labelElement: _jsx(Option, { title: screenLabel ?? t('share.opensOnScreen'), note: tooLarge
23
+ ? t('share.opensOnTooLarge')
24
+ : t('share.opensOnScreenNote') }) })), examples.length === 0 ? null : (_jsxs(_Fragment, { children: [_jsx(Radio, { name: group, value: "example", checked: choice.opensOn === 'example', onChange: select('example'), label: t('share.opensOnExample') }), _jsxs("div", { className: "share-opens-on__under", children: [_jsx(HTMLSelect, { fill: true, "aria-label": t('share.opensOnExampleLabel'), value: choice.example, onChange: (event) => {
25
+ onChange({ opensOn: 'example', example: event.target.value });
26
+ }, children: _jsx(ExampleOptions, { examples: examples }) }), shown?.description ? (_jsx("span", { className: "share-opens-on__note", children: shown.description })) : null, changed ? (_jsx("span", { className: "share-opens-on__note", children: t('share.opensOnChanged') })) : null] })] })), _jsx(Radio, { name: group, value: "none", checked: choice.opensOn === 'none', onChange: select('none'), labelElement: _jsx(Option, { title: t('share.opensOnNone'), note: t('share.opensOnNoneNote') }) }), children] }));
27
+ }
28
+ // The title in a span of its own, so it is what a click on the words finds.
29
+ function Option(props) {
30
+ return (_jsxs(_Fragment, { children: [_jsx("span", { className: "share-opens-on__title", children: props.title }), _jsx("span", { className: "share-opens-on__note", children: props.note })] }));
31
+ }
32
+ // Listed under their groups when they have any, in the order they come.
33
+ function ExampleOptions(props) {
34
+ const groups = new Map();
35
+ const loose = [];
36
+ for (const example of props.examples) {
37
+ if (example.group === undefined) {
38
+ loose.push(example);
39
+ continue;
40
+ }
41
+ const group = groups.get(example.group);
42
+ if (group)
43
+ group.push(example);
44
+ else
45
+ groups.set(example.group, [example]);
46
+ }
47
+ return (_jsxs(_Fragment, { children: [loose.map((example) => (_jsx("option", { value: example.id, children: example.name }, example.id))), [...groups].map(([label, members]) => (_jsx("optgroup", { label: label, children: members.map((example) => (_jsx("option", { value: example.id, children: example.name }, example.id))) }, label)))] }));
48
+ }
49
+ //# sourceMappingURL=ShareOpensOn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ShareOpensOn.js","sourceRoot":"","sources":["../../../src/share/ui/ShareOpensOn.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE1D,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAE9B,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AA8BnD;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,KAAwB;IACnD,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,WAAW,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC;IAC3E,MAAM,CAAC,GAAG,UAAU,EAAE,CAAC;IACvB,MAAM,KAAK,GAAG,KAAK,EAAE,CAAC;IACtB,MAAM,MAAM,GAAG,CAAC,OAAsB,EAAE,EAAE,CAAC,GAAG,EAAE;QAC9C,QAAQ,CAAC,EAAE,GAAG,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC;IACnC,CAAC,CAAC;IACF,MAAM,QAAQ,GAAG,KAAK,CAAC,MAAM,KAAK,WAAW,CAAC;IAC9C,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,KAAK,MAAM,CAAC,OAAO,CAAC,CAAC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AK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACpB,SAAS;QACX,CAAC;QACD,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACxC,IAAI,KAAK;YAAE,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;;YAC1B,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAC5C,CAAC;IACD,OAAO,CACL,8BACG,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CACtB,iBAAyB,KAAK,EAAE,OAAO,CAAC,EAAE,YACvC,OAAO,CAAC,IAAI,IADF,OAAO,CAAC,EAAE,CAEd,CACV,CAAC,EACD,CAAC,GAAG,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,CACrC,mBAAsB,KAAK,EAAE,KAAK,YAC/B,OAAO,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CACxB,iBAAyB,KAAK,EAAE,OAAO,CAAC,EAAE,YACvC,OAAO,CAAC,IAAI,IADF,OAAO,CAAC,EAAE,CAEd,CACV,CAAC,IALW,KAAK,CAMT,CACZ,CAAC,IACD,CACJ,CAAC;AACJ,CAAC"}
@@ -9,7 +9,11 @@ export type { ShareButtonProps, ShareButtonVariant } from './ShareButton.tsx';
9
9
  export { ShareButton } from './ShareButton.tsx';
10
10
  export type { ShareDialogProps } from './ShareDialog.tsx';
11
11
  export type { SharePartDescriptions } from './SharePartRow.tsx';
12
+ export type { ShareOpensOnProps } from './ShareOpensOn.tsx';
13
+ export { ShareOpensOn } from './ShareOpensOn.tsx';
12
14
  export type { ShareQrCodeProps } from './ShareQrCode.tsx';
13
15
  export { ShareQrCode } from './ShareQrCode.tsx';
14
16
  export { ShareDialog } from './ShareDialog.tsx';
17
+ export type { OpensOnShareOptions, OpensOnShareProps, } from './useOpensOnShare.tsx';
18
+ export { useOpensOnShare } from './useOpensOnShare.tsx';
15
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/share/ui/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AAC1E,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,YAAY,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,YAAY,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AACtE,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,YAAY,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,YAAY,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,YAAY,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAChE,YAAY,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/share/ui/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AAC1E,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,YAAY,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,YAAY,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AACtE,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,YAAY,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,YAAY,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,YAAY,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAChE,YAAY,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,YAAY,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,YAAY,EACV,mBAAmB,EACnB,iBAAiB,GAClB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC"}
@@ -3,6 +3,8 @@ export { useIsHidden } from "./hiddenParts.js";
3
3
  export { PagePart } from "./PagePart.js";
4
4
  export { SealedLinkRefused } from "./SealedLinkRefused.js";
5
5
  export { ShareButton } from "./ShareButton.js";
6
+ export { ShareOpensOn } from "./ShareOpensOn.js";
6
7
  export { ShareQrCode } from "./ShareQrCode.js";
7
8
  export { ShareDialog } from "./ShareDialog.js";
9
+ export { useOpensOnShare } from "./useOpensOnShare.js";
8
10
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/share/ui/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA2B,CAAC;AAChE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAgB,CAAC;AAE1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAyB,CAAC;AAE5D,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAmB,CAAC;AAIhD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAmB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAmB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/share/ui/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA2B,CAAC;AAChE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAgB,CAAC;AAE1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAyB,CAAC;AAE5D,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAmB,CAAC;AAIhD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAoB,CAAC;AAElD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAmB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAmB,CAAC;AAKhD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAuB,CAAC"}
@@ -0,0 +1,65 @@
1
+ import type { ReactNode } from 'react';
2
+ import type { ShareAddress, ShareExample, ShareOnScreen } from '../core/opensOn.ts';
3
+ /** What {@link useOpensOnShare} is told of the page. */
4
+ export interface OpensOnShareOptions {
5
+ /**
6
+ * Whether the page holding the document is the page on show. Any other page
7
+ * hands out its own address.
8
+ */
9
+ active: boolean;
10
+ /** The examples a link can open on, in the order the folder lists them. */
11
+ examples: readonly ShareExample[];
12
+ /**
13
+ * What is on screen, read as the dialog opens. A promise when writing it
14
+ * into a link takes a moment: the dialog then opens once it is written.
15
+ */
16
+ onScreen: () => ShareOnScreen | Promise<ShareOnScreen>;
17
+ /**
18
+ * The parameters naming what a page opens on, besides `example`, which a
19
+ * link replaces: the one carrying a document, say.
20
+ * @default [] — `example` alone
21
+ */
22
+ openedOn?: readonly string[];
23
+ /**
24
+ * The link to an example.
25
+ * @default `?example=<id>` on the page on screen
26
+ */
27
+ exampleAddress?: (id: string) => ShareAddress;
28
+ /**
29
+ * The link to the page alone.
30
+ * @default the page on screen, with nothing added
31
+ */
32
+ aloneAddress?: ShareAddress;
33
+ /**
34
+ * What the site calls what is on screen: "This circuit", "This drawing".
35
+ * @default the chrome's own words
36
+ */
37
+ screenLabel?: string;
38
+ /**
39
+ * What a site adds under the choice: the style a link sets, say.
40
+ * @default undefined — nothing
41
+ */
42
+ children?: ReactNode;
43
+ }
44
+ /** What the header's Share control is told, beyond what every page shares. */
45
+ export interface OpensOnShareProps {
46
+ /** Whether the dialog is open. */
47
+ open: boolean;
48
+ /** Called when the dialog opens or closes. */
49
+ onOpenChange: (open: boolean) => void;
50
+ /** The query string the link is built from. */
51
+ search: string | undefined;
52
+ /** The page the link opens, when it is not the page on screen. */
53
+ baseUrl: string | undefined;
54
+ /** The section saying what the link opens on. */
55
+ children: ReactNode;
56
+ }
57
+ /**
58
+ * The Share dialog of a page holding a document says what the link opens on:
59
+ * what is on screen, any of the examples the site comes with, or the page
60
+ * alone. Spread it into the header's `share` prop.
61
+ * @param options - See {@link OpensOnShareOptions}.
62
+ * @returns What the header's Share control is told.
63
+ */
64
+ export declare function useOpensOnShare(options: OpensOnShareOptions): OpensOnShareProps;
65
+ //# sourceMappingURL=useOpensOnShare.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useOpensOnShare.d.ts","sourceRoot":"","sources":["../../../src/share/ui/useOpensOnShare.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAGvC,OAAO,KAAK,EAGV,YAAY,EACZ,YAAY,EACZ,aAAa,EACd,MAAM,oBAAoB,CAAC;AAU5B,wDAAwD;AACxD,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,MAAM,EAAE,OAAO,CAAC;IAChB,2EAA2E;IAC3E,QAAQ,EAAE,SAAS,YAAY,EAAE,CAAC;IAClC;;;OAGG;IACH,QAAQ,EAAE,MAAM,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACvD;;;;OAIG;IACH,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC7B;;;OAGG;IACH,cAAc,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,YAAY,CAAC;IAC9C;;;OAGG;IACH,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB;AAED,8EAA8E;AAC9E,MAAM,WAAW,iBAAiB;IAChC,kCAAkC;IAClC,IAAI,EAAE,OAAO,CAAC;IACd,8CAA8C;IAC9C,YAAY,EAAE,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;IACtC,+CAA+C;IAC/C,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,kEAAkE;IAClE,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,iDAAiD;IACjD,QAAQ,EAAE,SAAS,CAAC;CACrB;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC7B,OAAO,EAAE,mBAAmB,GAC3B,iBAAiB,CAmEnB"}
@@ -0,0 +1,60 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useRef, useState } from 'react';
3
+ import { EXAMPLE_PARAM, defaultOpensOn, opensOnAddress, opensOnLinks, } from "../core/opensOn.js";
4
+ import { ShareOpensOn } from "./ShareOpensOn.js";
5
+ /**
6
+ * The Share dialog of a page holding a document says what the link opens on:
7
+ * what is on screen, any of the examples the site comes with, or the page
8
+ * alone. Spread it into the header's `share` prop.
9
+ * @param options - See {@link OpensOnShareOptions}.
10
+ * @returns What the header's Share control is told.
11
+ */
12
+ export function useOpensOnShare(options) {
13
+ const { active, examples, onScreen, exampleAddress, aloneAddress, screenLabel, children, } = options;
14
+ const openedOn = [EXAMPLE_PARAM, ...(options.openedOn ?? [])];
15
+ const [shared, setShared] = useState(null);
16
+ const [choice, setChoice] = useState(null);
17
+ // A dialog closed before what is on screen was written stays closed.
18
+ const asked = useRef(0);
19
+ function onOpenChange(open) {
20
+ const request = ++asked.current;
21
+ if (!open) {
22
+ setShared(null);
23
+ return;
24
+ }
25
+ const { search } = globalThis.location;
26
+ if (!active) {
27
+ setShared(search);
28
+ return;
29
+ }
30
+ const show = (screen) => {
31
+ if (request !== asked.current)
32
+ return;
33
+ const links = opensOnLinks(search, openedOn, screen);
34
+ setChoice(defaultOpensOn(links, examples));
35
+ setShared(links);
36
+ };
37
+ const read = onScreen();
38
+ if (read instanceof Promise)
39
+ void read.then(show);
40
+ else
41
+ show(read);
42
+ }
43
+ const links = typeof shared === 'string' ? null : shared;
44
+ const address = links === null || choice === null
45
+ ? undefined
46
+ : opensOnAddress(links, choice, {
47
+ example: exampleAddress,
48
+ alone: aloneAddress,
49
+ });
50
+ return {
51
+ open: shared !== null,
52
+ onOpenChange,
53
+ search: typeof shared === 'string' ? shared : address?.search,
54
+ baseUrl: address?.path === undefined
55
+ ? undefined
56
+ : new URL(address.path, globalThis.location.href).href,
57
+ children: links === null || choice === null ? undefined : (_jsx(ShareOpensOn, { links: links, choice: choice, onChange: setChoice, examples: examples, screenLabel: screenLabel, children: children })),
58
+ };
59
+ }
60
+ //# sourceMappingURL=useOpensOnShare.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useOpensOnShare.js","sourceRoot":"","sources":["../../../src/share/ui/useOpensOnShare.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AASzC,OAAO,EACL,aAAa,EACb,cAAc,EACd,cAAc,EACd,YAAY,GACb,MAAM,oBA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"React components, hooks and framework-free helpers shared by the cheminfo sites: site chrome, citations, sharing, routing, pedagogy, charts, and the structure, orbital and 3D molecule viewers",
6
6
  "author": "Luc Patiny",
@@ -179,6 +179,15 @@
179
179
  "share.copyQrSvg": "Als SVG kopieren",
180
180
  "share.qrCodeHint": "Das PNG lässt sich überall einfügen; das SVG bleibt in jeder Größe scharf.",
181
181
  "share.qrCodeTooLong": "Dieser Link ist zu lang für einen QR-Code. Kopieren Sie stattdessen den Link.",
182
+ "share.opensOn": "Der Link öffnet",
183
+ "share.opensOnScreen": "Was auf dem Bildschirm ist",
184
+ "share.opensOnScreenNote": "Der Link enthält es vollständig, es ist also keine Datei zu senden.",
185
+ "share.opensOnTooLarge": "Zu groß für einen Link: Speichern Sie es als Datei und senden Sie die Datei.",
186
+ "share.opensOnExample": "Ein Beispiel",
187
+ "share.opensOnExampleLabel": "Beispiel",
188
+ "share.opensOnChanged": "Das Beispiel im Original: Die hier gemachten Änderungen bleiben zurück.",
189
+ "share.opensOnNone": "Nur die Seite",
190
+ "share.opensOnNoneNote": "Jeder Besucher findet dort, was er zurückgelassen hat, oder eine leere Seite.",
182
191
  "seal.refusedTitle": "Dieser Link lässt sich nicht öffnen",
183
192
  "seal.refusedBody": "Er wurde nach dem Erstellen verändert oder ist unvollständig, daher wurde nichts geöffnet. Bitten Sie erneut um den Link.",
184
193
  "seal.openWithout": "Das Werkzeug ohne den Link öffnen",
@@ -179,6 +179,15 @@
179
179
  "share.copyQrSvg": "Copy as SVG",
180
180
  "share.qrCodeHint": "The PNG pastes anywhere; the SVG stays sharp at any size.",
181
181
  "share.qrCodeTooLong": "This link is too long for a QR code. Copy the link instead.",
182
+ "share.opensOn": "Opens on",
183
+ "share.opensOnScreen": "What is on screen",
184
+ "share.opensOnScreenNote": "The link carries it whole, so there is no file to send.",
185
+ "share.opensOnTooLarge": "Too large for a link: save it as a file and send the file.",
186
+ "share.opensOnExample": "An example",
187
+ "share.opensOnExampleLabel": "Example",
188
+ "share.opensOnChanged": "The example as it comes: the changes made here stay behind.",
189
+ "share.opensOnNone": "The page alone",
190
+ "share.opensOnNoneNote": "Each visitor finds what they left there, or an empty page.",
182
191
  "seal.refusedTitle": "This link cannot be opened",
183
192
  "seal.refusedBody": "It was changed after it was made, or it is incomplete, so nothing was opened. Ask for the link again.",
184
193
  "seal.openWithout": "Open the tool without it",
@@ -179,6 +179,15 @@
179
179
  "share.copyQrSvg": "Copiar como SVG",
180
180
  "share.qrCodeHint": "El PNG se pega en cualquier sitio; el SVG se ve nítido a cualquier tamaño.",
181
181
  "share.qrCodeTooLong": "Este enlace es demasiado largo para un código QR. Copia el enlace en su lugar.",
182
+ "share.opensOn": "El enlace abre",
183
+ "share.opensOnScreen": "Lo que hay en pantalla",
184
+ "share.opensOnScreenNote": "El enlace lo lleva entero, así que no hay ningún archivo que enviar.",
185
+ "share.opensOnTooLarge": "Demasiado grande para un enlace: guárdelo en un archivo y envíe el archivo.",
186
+ "share.opensOnExample": "Un ejemplo",
187
+ "share.opensOnExampleLabel": "Ejemplo",
188
+ "share.opensOnChanged": "El ejemplo tal como viene: los cambios hechos aquí no se incluyen.",
189
+ "share.opensOnNone": "Solo la página",
190
+ "share.opensOnNoneNote": "Cada visitante encuentra lo que dejó allí, o una página vacía.",
182
191
  "seal.refusedTitle": "Este enlace no se puede abrir",
183
192
  "seal.refusedBody": "Se modificó después de crearse, o está incompleto, así que no se abrió nada. Pide el enlace de nuevo.",
184
193
  "seal.openWithout": "Abrir la herramienta sin el enlace",
@@ -179,6 +179,15 @@
179
179
  "share.copyQrSvg": "Copier en SVG",
180
180
  "share.qrCodeHint": "Le PNG se colle partout ; le SVG reste net à toute taille.",
181
181
  "share.qrCodeTooLong": "Ce lien est trop long pour un code QR. Copiez plutôt le lien.",
182
+ "share.opensOn": "Le lien ouvre",
183
+ "share.opensOnScreen": "Ce qui est à l’écran",
184
+ "share.opensOnScreenNote": "Le lien le contient en entier : aucun fichier à envoyer.",
185
+ "share.opensOnTooLarge": "Trop grand pour un lien : enregistrez-le dans un fichier et envoyez le fichier.",
186
+ "share.opensOnExample": "Un exemple",
187
+ "share.opensOnExampleLabel": "Exemple",
188
+ "share.opensOnChanged": "L’exemple tel qu’il est fourni : les modifications faites ici n’en font pas partie.",
189
+ "share.opensOnNone": "La page seule",
190
+ "share.opensOnNoneNote": "Chaque visiteur y retrouve ce qu’il avait laissé, ou une page vide.",
182
191
  "seal.refusedTitle": "Ce lien ne peut pas être ouvert",
183
192
  "seal.refusedBody": "Il a été modifié après sa création, ou il est incomplet : rien n'a été ouvert. Demandez à nouveau le lien.",
184
193
  "seal.openWithout": "Ouvrir l'outil sans ce lien",
@@ -179,6 +179,15 @@
179
179
  "share.copyQrSvg": "Copia come SVG",
180
180
  "share.qrCodeHint": "Il PNG si incolla ovunque; l'SVG resta nitido a ogni dimensione.",
181
181
  "share.qrCodeTooLong": "Questo link è troppo lungo per un codice QR. Copia invece il link.",
182
+ "share.opensOn": "Il link apre",
183
+ "share.opensOnScreen": "Ciò che è sullo schermo",
184
+ "share.opensOnScreenNote": "Il link lo contiene per intero, quindi non c’è nessun file da inviare.",
185
+ "share.opensOnTooLarge": "Troppo grande per un link: salvalo in un file e invia il file.",
186
+ "share.opensOnExample": "Un esempio",
187
+ "share.opensOnExampleLabel": "Esempio",
188
+ "share.opensOnChanged": "L’esempio così come viene fornito: le modifiche fatte qui non sono incluse.",
189
+ "share.opensOnNone": "Solo la pagina",
190
+ "share.opensOnNoneNote": "Ogni visitatore ritrova ciò che vi aveva lasciato, o una pagina vuota.",
182
191
  "seal.refusedTitle": "Questo link non può essere aperto",
183
192
  "seal.refusedBody": "È stato modificato dopo la creazione, o è incompleto, quindi non si è aperto nulla. Chiedi di nuovo il link.",
184
193
  "seal.openWithout": "Apri lo strumento senza il link",
@@ -0,0 +1,52 @@
1
+ /**
2
+ * The examples a page opened, and the documents they opened as, so a link can
3
+ * tell an example still as it opened from one changed since.
4
+ */
5
+
6
+ import type { ShareOnScreen } from './opensOn.ts';
7
+
8
+ /** The examples a page opened, as {@link createExampleMark} keeps them. */
9
+ export interface ExampleMark<TDocument extends object> {
10
+ /**
11
+ * Say the document on screen is an example, as it opened.
12
+ * @param id - The example.
13
+ * @param document - The document it opened as.
14
+ */
15
+ mark: (id: string, document: TDocument) => void;
16
+ /** Say what is on screen came from no example: a file, a draft, a clear page. */
17
+ clear: () => void;
18
+ /**
19
+ * What the Share dialog is told of the document on screen.
20
+ * @param document - The document on screen.
21
+ * @returns The example it opened on, and whether it has changed since.
22
+ */
23
+ read: (document: TDocument) => Pick<ShareOnScreen, 'example' | 'changed'>;
24
+ }
25
+
26
+ /**
27
+ * Keep the examples a page opened. Documents are compared by identity: every
28
+ * change replaces the document, so the one an example opened as is the one on
29
+ * screen until something is changed, and an undo back to it — even past a file
30
+ * opened since — makes it that example again.
31
+ * @returns The mark.
32
+ */
33
+ export function createExampleMark<
34
+ TDocument extends object,
35
+ >(): ExampleMark<TDocument> {
36
+ const opened = new WeakMap<TDocument, string>();
37
+ let last: string | null = null;
38
+ return {
39
+ mark: (id, document) => {
40
+ opened.set(document, id);
41
+ last = id;
42
+ },
43
+ clear: () => {
44
+ last = null;
45
+ },
46
+ read: (document) => {
47
+ const example = opened.get(document);
48
+ if (example !== undefined) return { example, changed: false };
49
+ return { example: last, changed: last !== null };
50
+ },
51
+ };
52
+ }
@@ -19,8 +19,33 @@ export {
19
19
  sharePreviewSize,
20
20
  } from './devices.ts';
21
21
  export { escapeAttribute, escapeText } from './escape.ts';
22
+ export type { ExampleMark } from './exampleMark.ts';
23
+ export { createExampleMark } from './exampleMark.ts';
22
24
  export type { KeySetParamOptions } from './keySetParam.ts';
23
25
  export { keySetParam } from './keySetParam.ts';
26
+ export {
27
+ MAX_LINK_TOKEN,
28
+ packLinkValue,
29
+ packLinkValueIfFits,
30
+ unpackLinkValue,
31
+ } from './linkValue.ts';
32
+ export type {
33
+ OpensOnAddresses,
34
+ OpensOnChoice,
35
+ OpensOnLinks,
36
+ OpensOnTarget,
37
+ ScreenLink,
38
+ ShareAddress,
39
+ ShareExample,
40
+ ShareOnScreen,
41
+ } from './opensOn.ts';
42
+ export {
43
+ EXAMPLE_PARAM,
44
+ carriedScreen,
45
+ defaultOpensOn,
46
+ opensOnAddress,
47
+ opensOnLinks,
48
+ } from './opensOn.ts';
24
49
  export type { PageShareConfig } from './pageShareConfig.ts';
25
50
  export { createPageShareConfig } from './pageShareConfig.ts';
26
51
  export type { ShareRegion } from './regions.ts';
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Bytes written into a link: in the URL-safe base64 alphabet, and deflated or
3
+ * inflated through the browser's own streams.
4
+ */
5
+
6
+ /**
7
+ * Write bytes in the URL-safe base64 alphabet, without padding.
8
+ * @param bytes - The bytes.
9
+ * @returns The text.
10
+ */
11
+ export function toBase64Url(bytes: Uint8Array): string {
12
+ let binary = '';
13
+ for (const byte of bytes) binary += String.fromCodePoint(byte);
14
+ return btoa(binary)
15
+ .replaceAll('+', '-')
16
+ .replaceAll('/', '_')
17
+ .replace(/[=]+$/, '');
18
+ }
19
+
20
+ /**
21
+ * Read bytes written by {@link toBase64Url}.
22
+ * @param text - The text, without padding.
23
+ * @returns The bytes, or `null` when the text is not URL-safe base64.
24
+ */
25
+ export function fromBase64Url(text: string): Uint8Array<ArrayBuffer> | null {
26
+ if (!/^[\w-]*$/.test(text) || text.length % 4 === 1) return null;
27
+ const base64 = text.replaceAll('-', '+').replaceAll('_', '/');
28
+ const binary = atob(base64.padEnd(Math.ceil(base64.length / 4) * 4, '='));
29
+ const bytes = new Uint8Array(binary.length);
30
+ for (let index = 0; index < binary.length; index++) {
31
+ bytes[index] = binary.codePointAt(index) ?? 0;
32
+ }
33
+ return bytes;
34
+ }
35
+
36
+ /**
37
+ * Pass bytes through a compression or a decompression stream.
38
+ * @param bytes - The bytes in.
39
+ * @param stream - The stream.
40
+ * @param limit - The most the bytes out may weigh, so a link made by hand
41
+ * cannot make the page inflate without end.
42
+ * @default Infinity
43
+ * @returns The bytes out.
44
+ * @throws {Error} When the bytes out weigh more than `limit`, or the stream
45
+ * cannot read the bytes in.
46
+ */
47
+ export async function pipeBytes(
48
+ bytes: Uint8Array<ArrayBuffer>,
49
+ stream: CompressionStream | DecompressionStream,
50
+ limit = Infinity,
51
+ ): Promise<Uint8Array<ArrayBuffer>> {
52
+ // A stream of its own, not a Blob's: jsdom's Blob has no `stream()`.
53
+ const source = new ReadableStream<Uint8Array<ArrayBuffer>>({
54
+ start(controller) {
55
+ controller.enqueue(bytes);
56
+ controller.close();
57
+ },
58
+ });
59
+ const output = source.pipeThrough(stream).pipeThrough(capped(limit));
60
+ return new Uint8Array(await new Response(output).arrayBuffer());
61
+ }
62
+
63
+ function capped(limit: number): TransformStream<Uint8Array, Uint8Array> {
64
+ let length = 0;
65
+ return new TransformStream({
66
+ transform(chunk, controller) {
67
+ length += chunk.byteLength;
68
+ if (length > limit) {
69
+ controller.error(new Error('The value unpacks larger than allowed.'));
70
+ return;
71
+ }
72
+ controller.enqueue(chunk);
73
+ },
74
+ });
75
+ }
@@ -0,0 +1,66 @@
1
+ /**
2
+ * A value written into a link: its JSON, deflated, in the URL-safe base64
3
+ * alphabet, so the link opens what was on screen with no file beside it and
4
+ * no server behind it. A circuit of twenty parts takes about a thousand
5
+ * characters.
6
+ */
7
+
8
+ import { fromBase64Url, pipeBytes, toBase64Url } from './linkBytes.ts';
9
+
10
+ /**
11
+ * The longest token a link is given: with the rest of the address it stays
12
+ * under the 8 KB request line nginx and Apache accept by default. A page of
13
+ * text fits; a pasted picture rarely does.
14
+ */
15
+ export const MAX_LINK_TOKEN = 7000;
16
+
17
+ // Bound what a link made by hand can make the page read and inflate.
18
+ const MAX_READ_TOKEN = 32 * 1024;
19
+ const MAX_UNPACKED_BYTES = 4 * 1024 * 1024;
20
+
21
+ /**
22
+ * Write a value as the token a link carries.
23
+ * @param value - The value, as JSON writes it.
24
+ * @returns The token, in the URL-safe base64 alphabet without padding.
25
+ */
26
+ export async function packLinkValue(value: unknown): Promise<string> {
27
+ const json = new TextEncoder().encode(JSON.stringify(value));
28
+ const packed = await pipeBytes(json, new CompressionStream('deflate-raw'));
29
+ return toBase64Url(packed);
30
+ }
31
+
32
+ /**
33
+ * Read the value a link carries. The link may come from anywhere, so what it
34
+ * holds is trusted for nothing: the caller checks it as it would a file.
35
+ * @param token - The token, as {@link packLinkValue} writes it.
36
+ * @returns The value, or `undefined` when the token holds none.
37
+ */
38
+ export async function unpackLinkValue(token: string): Promise<unknown> {
39
+ if (token.length > MAX_READ_TOKEN) return undefined;
40
+ const bytes = fromBase64Url(token);
41
+ if (!bytes) return undefined;
42
+ try {
43
+ const json = await pipeBytes(
44
+ bytes,
45
+ new DecompressionStream('deflate-raw'),
46
+ MAX_UNPACKED_BYTES,
47
+ );
48
+ return JSON.parse(new TextDecoder().decode(json));
49
+ } catch {
50
+ return undefined;
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Write a value as the token a link carries, unless the token would be too
56
+ * long for one.
57
+ * @param value - The value, as JSON writes it.
58
+ * @returns The token, or `undefined` when it is longer than
59
+ * {@link MAX_LINK_TOKEN}.
60
+ */
61
+ export async function packLinkValueIfFits(
62
+ value: unknown,
63
+ ): Promise<string | undefined> {
64
+ const token = await packLinkValue(value);
65
+ return token.length > MAX_LINK_TOKEN ? undefined : token;
66
+ }