@rokkit/states 1.0.0-next.113 → 1.0.0-next.114

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rokkit/states",
3
- "version": "1.0.0-next.113",
3
+ "version": "1.0.0-next.114",
4
4
  "description": "Contains generic data manipulation functions that can be used in various components.",
5
5
  "author": "Jerry Thomas <me@jerrythomas.name>",
6
6
  "license": "MIT",
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "@lukeed/uuid": "^2.0.1",
34
- "@rokkit/core": "1.0.0-next.112",
34
+ "@rokkit/core": "1.0.0-next.114",
35
35
  "d3-array": "^3.2.4",
36
36
  "d3-collection": "^1.0.7",
37
37
  "ramda": "^0.30.1",
@@ -45,10 +45,10 @@ export function deriveLookupWithProxy(items, fields = defaultFields, path = [])
45
45
  const proxy = new Proxy(item, fields)
46
46
 
47
47
  lookup.set(key, proxy)
48
- // console.log(key, proxy.value)
49
- if (proxy.hasChildren) {
48
+ const children = proxy.value[proxy.fields.children] ?? []
49
+ if (children.length > 0) {
50
50
  const childFields = getNestedFields(fields)
51
- const childLookup = deriveLookupWithProxy(proxy.get('children'), childFields, itemPath)
51
+ const childLookup = deriveLookupWithProxy(children, childFields, itemPath)
52
52
  for (const [childKey, childValue] of childLookup.entries()) {
53
53
  lookup.set(childKey, childValue)
54
54
  }
@@ -8,38 +8,11 @@ export class NestedController extends ListController {
8
8
  * @param {Object} [value]
9
9
  */
10
10
  init(value) {
11
- // this.createLookup(items)
12
11
  if (value) {
13
12
  this.ensureVisible(value)
14
13
  this.moveToValue(value)
15
14
  }
16
15
  }
17
- /**
18
- * @private
19
- * @param {Object[]} items
20
- * @param {number[]} [path]=[]
21
- */
22
- createLookup(items, path = []) {
23
- const depth = path.length
24
- if (depth >= this.mappers.length) {
25
- this.mappers.push(this.mappers[depth - 1].getChildMapper())
26
- }
27
- const fm = this.mappers[depth]
28
-
29
- items.forEach((item, index) => {
30
- const itemPath = [...path, index]
31
- const key = getKeyFromPath(itemPath)
32
-
33
- this.lookup.set(key, item)
34
- if (fm.get('selected', item)) {
35
- this.selectedKeys.add(key)
36
- }
37
-
38
- if (fm.hasChildren(item)) {
39
- this.createLookup(fm.get('children', item), itemPath)
40
- }
41
- })
42
- }
43
16
 
44
17
  /**
45
18
  * Mark parents as expanded so that item is visible
@@ -48,7 +21,6 @@ export class NestedController extends ListController {
48
21
  */
49
22
  ensureVisible(value) {
50
23
  const result = this.lookup.entries().find((entry) => equals(entry[1].value, value))
51
- // console.log(result)
52
24
  const path = getPathFromKey(result[0])
53
25
 
54
26
  for (let i = 1; i < path.length; i++) {
@@ -67,9 +39,6 @@ export class NestedController extends ListController {
67
39
  if (!this.lookup.has(key)) return false
68
40
  const proxy = this.lookup.get(key)
69
41
  proxy.expanded = !proxy.expanded
70
- // const item = this.lookup.get(key)
71
- // const fields = this.fieldsFor(key)
72
- // item[fields.expanded] = !item[this.fields.expanded]
73
42
  return true
74
43
  }
75
44
 
@@ -83,9 +52,6 @@ export class NestedController extends ListController {
83
52
  if (!this.lookup.has(actualKey)) return false
84
53
  const proxy = this.lookup.get(actualKey)
85
54
  proxy.expanded = true
86
- // const item = this.lookup.get(actualKey)
87
- // const fields = this.fieldsFor(actualKey)
88
- // item[fields.expanded] = true
89
55
 
90
56
  return true
91
57
  }
@@ -98,25 +64,8 @@ export class NestedController extends ListController {
98
64
  collapse(key) {
99
65
  const actualKey = key ?? this.focusedKey
100
66
  if (!this.lookup.has(actualKey)) return false
101
- // const item = this.lookup.get(actualKey)
102
- // const fields = this.fieldsFor(actualKey)
103
- // item[fields.expanded] = false
104
67
  const proxy = this.lookup.get(actualKey)
105
68
  proxy.expanded = false
106
69
  return true
107
70
  }
108
-
109
- /**
110
- *
111
- * @param {*} key
112
- * @returns
113
- */
114
- fieldsFor(key) {
115
- const path = getPathFromKey(key)
116
- let fields = this.fields
117
- for (let i = 1; i < path.length; i++) {
118
- fields = getNestedFields(fields)
119
- }
120
- return fields
121
- }
122
71
  }
@@ -1,17 +1,39 @@
1
- import { defaultFields } from '@rokkit/core'
1
+ import { defaultFields, id, toString, getNestedFields } from '@rokkit/core'
2
2
  import { isNil, has } from 'ramda'
3
3
 
4
4
  export class Proxy {
5
5
  #original = null
6
- #value = null
6
+ #value = $state(null)
7
7
  #fields = null
8
+ #id = null
9
+
10
+ #children = $derived(this.#processChildren())
8
11
 
9
12
  constructor(value, fields) {
10
13
  this.fields = fields
11
14
  this.#original = value
12
15
  this.#value = typeof value === 'object' ? value : { [this.fields.text]: value }
16
+ this.id = typeof value === 'object' ? (this.get('id') ?? id()) : value
17
+ }
18
+
19
+ #processChildren() {
20
+ const children = this.#value[this.fields.children] ?? []
21
+ if (Array.isArray(children)) {
22
+ const fields = getNestedFields(this.fields)
23
+ return children.map((child) => new Proxy(child, fields))
24
+ }
25
+ return []
13
26
  }
14
27
 
28
+ get id() {
29
+ return this.#id
30
+ }
31
+ set id(new_id) {
32
+ this.#id = typeof new_id === 'string' ? new_id : toString(new_id)
33
+ }
34
+ get children() {
35
+ return this.#children
36
+ }
15
37
  get fields() {
16
38
  return this.#fields
17
39
  }
@@ -45,9 +67,9 @@ export class Proxy {
45
67
  *
46
68
  * @param {string} fieldName - Name of the field to get
47
69
  * @param {any} defaultValue - Default value to return if not found
48
- * @returns {any|null} - The attribute value or null if not found
70
+ * @returns {any|undefined} - The attribute value or null if not found
49
71
  */
50
- get(fieldName, defaultValue = null) {
72
+ get(fieldName, defaultValue) {
51
73
  return this.has(fieldName) ? this.#value[this.fields[fieldName]] : defaultValue
52
74
  }
53
75
 
@@ -61,12 +83,30 @@ export class Proxy {
61
83
  return !isNil(mappedField) && has(mappedField, this.#value)
62
84
  }
63
85
 
86
+ /**
87
+ * Gets the appropriate snippet for rendering this item:
88
+ * - Uses the 'snippet' field from the current item to find the snippet key
89
+ * - Finds a matching snippet in the provided collection using this key
90
+ * - Falls back to the defaultSnippet if:
91
+ * - No snippet key is configured for this item
92
+ * - The configured snippet key doesn't exist in the snippets collection
93
+ * @param {Object} snippets
94
+ * @param {import('svelte').Snippet|undefined} defaultSnippet
95
+ * @returns {import('svelte').Snippet|undefined}
96
+ */
97
+ getSnippet(snippets, defaultSnippet) {
98
+ const snippetKey = this.get('snippet')
99
+ const snippet = has(snippetKey, snippets) ? snippets[snippetKey] : undefined
100
+ return snippet ?? defaultSnippet
101
+ }
102
+
64
103
  /**
65
104
  * Identifies if the item has children
66
105
  */
67
106
  get hasChildren() {
68
107
  return (
69
108
  typeof this.#original === 'object' &&
109
+ has(this.fields.children, this.#value) &&
70
110
  Array.isArray(this.#value[this.fields.children]) &&
71
111
  this.#value[this.fields.children].length > 0
72
112
  )