asma-core-helpers 0.32.0 → 0.34.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 (48) hide show
  1. package/lib/collaboration/applyOperation.d.ts +5 -13
  2. package/lib/collaboration/applyOperation.d.ts.map +1 -1
  3. package/lib/collaboration/applyOperation.js +450 -20
  4. package/lib/collaboration/applyOperation.js.map +1 -1
  5. package/lib/collaboration/operations.d.ts +113 -1
  6. package/lib/collaboration/operations.d.ts.map +1 -1
  7. package/lib/collaboration/operations.js +7 -0
  8. package/lib/collaboration/operations.js.map +1 -1
  9. package/lib/collaboration/schemas.d.ts +54 -0
  10. package/lib/collaboration/schemas.d.ts.map +1 -1
  11. package/lib/collaboration/schemas.js +74 -1
  12. package/lib/collaboration/schemas.js.map +1 -1
  13. package/lib/collaboration/templateDocument.d.ts +194 -0
  14. package/lib/collaboration/templateDocument.d.ts.map +1 -1
  15. package/lib/collaboration/templateDocument.js +104 -0
  16. package/lib/collaboration/templateDocument.js.map +1 -1
  17. package/lib/collaboration/testHelpers/documentGenerator.d.ts +24 -0
  18. package/lib/collaboration/testHelpers/documentGenerator.d.ts.map +1 -1
  19. package/lib/collaboration/testHelpers/documentGenerator.js +180 -2
  20. package/lib/collaboration/testHelpers/documentGenerator.js.map +1 -1
  21. package/lib/collaboration/typeContracts.d.ts +73 -1
  22. package/lib/collaboration/typeContracts.d.ts.map +1 -1
  23. package/lib/g-definitions.d.ts +17 -0
  24. package/lib/g-definitions.d.ts.map +1 -1
  25. package/lib/g-definitions.js +13 -0
  26. package/lib/g-definitions.js.map +1 -1
  27. package/lib/helpers/sectionPath.helpers.d.ts +64 -0
  28. package/lib/helpers/sectionPath.helpers.d.ts.map +1 -0
  29. package/lib/helpers/sectionPath.helpers.js +68 -0
  30. package/lib/helpers/sectionPath.helpers.js.map +1 -0
  31. package/lib/index.d.ts +1 -0
  32. package/lib/index.d.ts.map +1 -1
  33. package/lib/index.js +1 -0
  34. package/lib/index.js.map +1 -1
  35. package/package.json +1 -1
  36. package/src/collaboration/applyOperation.ts +517 -17
  37. package/src/collaboration/operations.ts +95 -0
  38. package/src/collaboration/property.test.ts +49 -0
  39. package/src/collaboration/schemas.ts +82 -0
  40. package/src/collaboration/templateDocument.ts +238 -1
  41. package/src/collaboration/testHelpers/documentGenerator.ts +201 -2
  42. package/src/collaboration/typeContracts.ts +106 -1
  43. package/src/collaboration/typedAuthoring.test.ts +1091 -0
  44. package/src/collaboration/typedAuthoringHardening.test.ts +344 -1
  45. package/src/g-definitions.ts +29 -0
  46. package/src/helpers/sectionPath.helpers.test.ts +159 -0
  47. package/src/helpers/sectionPath.helpers.ts +129 -0
  48. package/src/index.ts +1 -0
@@ -0,0 +1,129 @@
1
+ export type HistoryLocation = { pathname: string; search: string }
2
+ export type HistoryTo = { pathname: string; search?: string }
3
+ export type HistoryLike = {
4
+ location: HistoryLocation
5
+ push: (to: HistoryTo, state?: unknown) => void
6
+ replace: (to: HistoryTo, state?: unknown) => void
7
+ }
8
+
9
+ export type SectionPathConfig<TTab extends string> = {
10
+ /** Section root without a leading slash — pass a `ProtectedRoutesPaths` value. */
11
+ base: string
12
+ /** Every tab slug the section can render. */
13
+ tabs: readonly TTab[]
14
+ /** Used as the tab for `build`/`goToTab`/`replaceTab` when none is given. */
15
+ defaultTab: TTab
16
+ /** Search params that address a DETAIL view inside this section; stripped by every
17
+ * navigation this module performs, so returning to a tab never re-opens a detail. */
18
+ detailParams?: readonly string[]
19
+ /** The shared shell history singleton in real usage (`history` from `asma-core-helpers`'s
20
+ * `g-definitions`), or a fake in tests. Required and explicit so this module stays free of
21
+ * any browser-global dependency and is testable under plain Node. */
22
+ history: HistoryLike
23
+ }
24
+
25
+ export type SectionPath<TTab extends string> = {
26
+ readonly root: string
27
+ build: (tab?: TTab) => string
28
+ /** `undefined` when the pathname is outside the section (so a widget mounted elsewhere
29
+ * never canonicalises the URL). `{ tab: undefined }` at the bare section root. */
30
+ parse: (pathname: string) => { tab?: TTab } | undefined
31
+ isTab: (value: unknown) => value is TTab
32
+ goToTab: (tab?: TTab, state?: unknown) => void
33
+ replaceTab: (tab?: TTab, state?: unknown) => void
34
+ /** Current search minus `detailParams`; '' or '?a=b'. */
35
+ searchWithoutDetail: () => string
36
+ }
37
+
38
+ export function createSectionPath<TTab extends string>(config: SectionPathConfig<TTab>): SectionPath<TTab> {
39
+ const { base, tabs, defaultTab, detailParams = [], history } = config
40
+ const root = `/${base}`
41
+ const prefix = `${root}/`
42
+
43
+ const isTab = (value: unknown): value is TTab => typeof value === 'string' && (tabs as readonly string[]).includes(value)
44
+
45
+ const parse = (pathname: string): { tab?: TTab } | undefined => {
46
+ if (pathname === root) return {}
47
+ if (!pathname.startsWith(prefix)) return undefined
48
+ const [tabSegment] = pathname.slice(prefix.length).split('/')
49
+ return { tab: isTab(tabSegment) ? tabSegment : undefined }
50
+ }
51
+
52
+ const build = (tab: TTab = defaultTab): string => `${root}/${tab}`
53
+
54
+ const searchWithoutDetail = (): string => stripSearchParams(history.location.search, detailParams)
55
+
56
+ const goToTab = (tab: TTab = defaultTab, state?: unknown): void => {
57
+ history.push({ pathname: build(tab), search: searchWithoutDetail() }, state)
58
+ }
59
+ const replaceTab = (tab: TTab = defaultTab, state?: unknown): void => {
60
+ history.replace({ pathname: build(tab), search: searchWithoutDetail() }, state)
61
+ }
62
+
63
+ return { root, build, parse, isTab, goToTab, replaceTab, searchWithoutDetail }
64
+ }
65
+
66
+ /** `/base/:detailId/:tab?` — a list at the root, tabs under a detail id. */
67
+ export type DetailPathConfig<TTab extends string> = SectionPathConfig<TTab> & {
68
+ isDetailId: (segment: string) => boolean
69
+ }
70
+
71
+ export type DetailPath<TTab extends string> = {
72
+ readonly listPath: string
73
+ build: (detailId: string, tab?: TTab) => string
74
+ /** `undefined` when the pathname is outside the section. `{}` at the bare list path or an
75
+ * unrecognised id segment (both render the list). */
76
+ parse: (pathname: string) => { detailId?: string; tab?: TTab } | undefined
77
+ isTab: (value: unknown) => value is TTab
78
+ goToList: (state?: unknown) => void
79
+ goToDetail: (detailId: string, tab?: TTab, state?: unknown) => void
80
+ replaceDetail: (detailId: string, tab?: TTab, state?: unknown) => void
81
+ searchWithoutDetail: () => string
82
+ }
83
+
84
+ export function createDetailPath<TTab extends string>(config: DetailPathConfig<TTab>): DetailPath<TTab> {
85
+ const { base, tabs, defaultTab, detailParams = [], isDetailId, history } = config
86
+ const root = `/${base}`
87
+ const prefix = `${root}/`
88
+
89
+ const isTab = (value: unknown): value is TTab => typeof value === 'string' && (tabs as readonly string[]).includes(value)
90
+
91
+ const parse = (pathname: string): { detailId?: string; tab?: TTab } | undefined => {
92
+ if (pathname === root) return {}
93
+ if (!pathname.startsWith(prefix)) return undefined
94
+ const [idSegment, tabSegment] = pathname.slice(prefix.length).split('/')
95
+ if (!idSegment || !isDetailId(idSegment)) return {}
96
+ return { detailId: idSegment, tab: isTab(tabSegment) ? tabSegment : undefined }
97
+ }
98
+
99
+ const build = (detailId: string, tab: TTab = defaultTab): string => `${root}/${detailId}/${tab}`
100
+
101
+ const searchWithoutDetail = (): string => stripSearchParams(history.location.search, detailParams)
102
+
103
+ const goToList = (state?: unknown): void => {
104
+ history.push({ pathname: root, search: searchWithoutDetail() }, state)
105
+ }
106
+ const goToDetail = (detailId: string, tab: TTab = defaultTab, state?: unknown): void => {
107
+ history.push({ pathname: build(detailId, tab), search: searchWithoutDetail() }, state)
108
+ }
109
+ const replaceDetail = (detailId: string, tab: TTab = defaultTab, state?: unknown): void => {
110
+ history.replace({ pathname: build(detailId, tab), search: searchWithoutDetail() }, state)
111
+ }
112
+
113
+ return { listPath: root, build, parse, isTab, goToList, goToDetail, replaceDetail, searchWithoutDetail }
114
+ }
115
+
116
+ function stripSearchParams(search: string, names: readonly string[]): string {
117
+ if (!names.length) return search
118
+ const params = new URLSearchParams(search)
119
+ let changed = false
120
+ for (const name of names) {
121
+ if (params.has(name)) {
122
+ params.delete(name)
123
+ changed = true
124
+ }
125
+ }
126
+ if (!changed) return search
127
+ const next = params.toString()
128
+ return next ? `?${next}` : ''
129
+ }
package/src/index.ts CHANGED
@@ -27,6 +27,7 @@ export * from './helpers/attachUserJournalCredentials.js'
27
27
  export * from './helpers/initASMAAppVitals.js'
28
28
  export * from './helpers/registerOpenReplay.js'
29
29
  export * from './helpers/urlParams.helpers.js'
30
+ export * from './helpers/sectionPath.helpers.js'
30
31
  export * from './helpers/openReplayObject.js'
31
32
  //export * from './helpers/openReplayConfigs.js'
32
33
  export * from './helpers/sha256.js'