svedit 0.1.1 → 0.3.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.
@@ -1,6 +1,10 @@
1
1
  <script>
2
+ import { getContext } from 'svelte';
3
+
2
4
  /** @import { CustomPropertyProps } from './types.d.ts'; */
3
5
 
6
+ const svedit = getContext('svedit');
7
+
4
8
  /** @type {CustomPropertyProps} */
5
9
  let {
6
10
  path,
@@ -9,16 +13,23 @@
9
13
  } = $props();
10
14
  </script>
11
15
 
12
- <!-- NOTE: It's super crucial that there is no extra whitespace, to not introduce additional cursor positions in contenteditable -->
13
- <div
14
- class="property {css_class}"
15
- data-type="property"
16
- data-path={path.join('.')}
17
- style="anchor-name: --{path.join('-')};"
18
- >
19
- <div class="cursor-trap svedit-selectable"><br/></div>
20
- <div class="property-content" contenteditable="false">{@render children()}</div>
21
- </div>
16
+ {#if svedit.editable}
17
+ <!-- NOTE: It's super crucial that there is no extra whitespace, to not introduce additional cursor positions in contenteditable -->
18
+ <div
19
+ class="property {css_class}"
20
+ data-type="property"
21
+ data-path={path.join('.')}
22
+ style="anchor-name: --{path.join('-')};"
23
+ >
24
+ <div class="cursor-trap">
25
+ <div class="svedit-selectable"><br/></div>
26
+ </div>
27
+
28
+ <div class="property-content" contenteditable="false">{@render children()}</div>
29
+ </div>
30
+ {:else}
31
+ {@render children()}
32
+ {/if}
22
33
 
23
34
  <style>
24
35
  .property {
@@ -1,4 +1,5 @@
1
1
  import Transaction from './Transaction.svelte.js';
2
+ import { char_slice } from './util.js';
2
3
 
3
4
  /**
4
5
  * @import {
@@ -239,7 +240,9 @@ export default class Document {
239
240
  return node_id ? this.get(node_id) : null;
240
241
  } else {
241
242
  // we are assuming we are either in a text or property (=custom) selection
242
- const owner_node = this.get(this.selection.path.slice(0, -1));
243
+ const node_id = this.selection?.path?.slice(0, -1);
244
+ if (!node_id) return null;
245
+ const owner_node = this.get(node_id);
243
246
  return owner_node;
244
247
  }
245
248
  }
@@ -489,7 +492,7 @@ export default class Document {
489
492
  const start = Math.min(this.selection.anchor_offset, this.selection.focus_offset);
490
493
  const end = Math.max(this.selection.anchor_offset, this.selection.focus_offset);
491
494
  const annotated_string = this.get(this.selection.path);
492
- return annotated_string[0].slice(start, end);
495
+ return char_slice(annotated_string[0], start, end);
493
496
  }
494
497
 
495
498
  get_selected_nodes() {
@@ -18,21 +18,25 @@
18
18
  let is_first_node_array_child = $derived(is_inside_node_array && parseInt(String(path.at(-1)), 10) === 0);
19
19
  </script>
20
20
 
21
- <div
22
- class="node"
23
- data-node-id={node.id}
24
- data-path={path.join('.')}
25
- data-type="node"
26
- style="anchor-name: --{path.join('-')};"
27
- >
28
- {#if is_first_node_array_child}
29
- <NodeCursorTrap {path} type="position-zero-cursor-trap" />
30
- {/if}
21
+ {#if svedit.editable}
22
+ <div
23
+ class="node"
24
+ data-node-id={node.id}
25
+ data-path={path.join('.')}
26
+ data-type="node"
27
+ style="anchor-name: --{path.join('-')};"
28
+ >
29
+ {#if is_first_node_array_child}
30
+ <NodeCursorTrap {path} type="position-zero-cursor-trap" />
31
+ {/if}
32
+ {@render children()}
33
+ {#if is_inside_node_array}
34
+ <NodeCursorTrap {path} type="after-node-cursor-trap" />
35
+ {/if}
36
+ </div>
37
+ {:else}
31
38
  {@render children()}
32
- {#if is_inside_node_array}
33
- <NodeCursorTrap {path} type="after-node-cursor-trap" />
34
- {/if}
35
- </div>
39
+ {/if}
36
40
 
37
41
  <style>
38
42
  div {
@@ -18,7 +18,7 @@
18
18
  </script>
19
19
 
20
20
  <div class={css_class} data-type="node_array" data-path={path.join('.')}>
21
- {#if nodes.length === 0}
21
+ {#if nodes.length === 0 && svedit.editable}
22
22
  <!--
23
23
  Experimental: We'll let .empty-node-array act like a node, so the existing
24
24
  code paths for selection mapping will work as expected.