dsh-document-view 1.1.0 → 1.2.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/src/model.mjs CHANGED
@@ -2,7 +2,13 @@ const STORAGE_KEY = 'dsh.document-view.v1';
2
2
  const clampRatio = value => Number.isFinite(value) ? Math.max(0.2, Math.min(0.65, value)) : 0.35;
3
3
 
4
4
  export function isDocument(address) {
5
- return typeof address === 'string' && address.startsWith('dsh-resource://file/');
5
+ if (typeof address !== 'string' || !address) return false;
6
+ if (address.startsWith('dsh-resource://file/')) return true;
7
+ // HTML previews may open under a viewer address that is not a session file
8
+ // address (absolute path, http URL, blob URL, ...). Recognize them by the
9
+ // .html/.htm extension anywhere in the address so the split layout applies.
10
+ const clean = address.split(/[?#]/, 1)[0];
11
+ return /\.(?:html|htm)$/i.test(clean) || /\.(?:html|htm)[\\/]/i.test(clean);
6
12
  }
7
13
 
8
14
  export function solveColumns(width, sidebar, ratio) {
@@ -20,6 +26,7 @@ export class CollaborationState {
20
26
  this.auto = false;
21
27
  this.sessions = Object.create(null);
22
28
  this.counts = new Map();
29
+ this.dirty = new Set();
23
30
  try {
24
31
  const saved = JSON.parse(storage?.getItem(STORAGE_KEY) || '{}');
25
32
  this.auto = saved.auto === true;
@@ -36,14 +43,61 @@ export class CollaborationState {
36
43
  session(id) {
37
44
  return this.sessions[id] ||= { enabled: false, ratio: 0.35, suppressed: '' };
38
45
  }
46
+ // Merge with what other windows already saved: only the flags this window
47
+ // changed override the stored value, so two windows on different sessions
48
+ // no longer erase each other.
39
49
  save() {
40
- try { this.storage?.setItem(STORAGE_KEY, JSON.stringify({ auto: this.auto, sessions: this.sessions })); }
41
- catch { /* Private browsing and quota errors keep in-memory behavior working. */ }
50
+ if (!this.storage) return false;
51
+ try {
52
+ let remote = {};
53
+ try { remote = JSON.parse(this.storage.getItem(STORAGE_KEY) || '{}'); } catch { /* treat as empty */ }
54
+ const sessions = { ...(remote.sessions || {}) };
55
+ let auto = remote.auto === true;
56
+ if (this.dirty.has('auto')) auto = this.auto;
57
+ for (const id of this.dirty) {
58
+ if (id === 'auto') continue;
59
+ sessions[id] = this.session(id);
60
+ }
61
+ for (const [id, value] of Object.entries(this.sessions)) {
62
+ if (!(id in sessions)) sessions[id] = value;
63
+ }
64
+ for (const [id, value] of Object.entries(remote.sessions || {})) {
65
+ if (this.dirty.has(id) || !value || typeof value !== 'object') continue;
66
+ this.sessions[id] = {
67
+ enabled: value.enabled === true,
68
+ ratio: clampRatio(value.ratio),
69
+ suppressed: typeof value.suppressed === 'string' ? value.suppressed : '',
70
+ };
71
+ }
72
+ this.storage.setItem(STORAGE_KEY, JSON.stringify({ auto, sessions }));
73
+ this.dirty.clear();
74
+ return true;
75
+ } catch { return false; }
76
+ }
77
+ // Adopt another window's write without dropping this window's pending edits.
78
+ reload() {
79
+ try {
80
+ const remote = JSON.parse(this.storage?.getItem(STORAGE_KEY) || '{}');
81
+ if (!this.dirty.has('auto')) this.auto = remote.auto === true;
82
+ const sessions = {};
83
+ for (const [id, value] of Object.entries(remote.sessions || {})) {
84
+ if (!value || typeof value !== 'object') continue;
85
+ sessions[id] = {
86
+ enabled: value.enabled === true,
87
+ ratio: clampRatio(value.ratio),
88
+ suppressed: typeof value.suppressed === 'string' ? value.suppressed : '',
89
+ };
90
+ }
91
+ for (const id of this.dirty) if (id !== 'auto') sessions[id] = this.session(id);
92
+ for (const [id, value] of Object.entries(this.sessions)) if (!(id in sessions)) sessions[id] = value;
93
+ this.sessions = sessions;
94
+ } catch { /* keep the current in-memory state */ }
42
95
  }
43
96
  toggle(id, address) {
44
97
  const session = this.session(id);
45
98
  session.enabled = !session.enabled;
46
99
  session.suppressed = session.enabled ? '' : address || '';
100
+ this.dirty.add(id);
47
101
  this.save();
48
102
  }
49
103
  observe(id, address, userGesture) {
@@ -51,6 +105,7 @@ export class CollaborationState {
51
105
  if (this.auto && userGesture && isDocument(address) && session.suppressed !== address && !session.enabled) {
52
106
  session.enabled = true;
53
107
  session.suppressed = '';
108
+ this.dirty.add(id);
54
109
  this.save();
55
110
  }
56
111
  }
@@ -59,14 +114,17 @@ export class CollaborationState {
59
114
  this.counts.set(id, count);
60
115
  if (previous > 0 && count === 0) {
61
116
  this.session(id).enabled = false;
117
+ this.dirty.add(id);
62
118
  this.save();
63
119
  }
64
120
  }
65
- setAuto(value) { this.auto = value === true; this.save(); }
121
+ setAuto(value) { this.auto = value === true; this.dirty.add('auto'); this.save(); }
66
122
  setRatio(id, value, persist = true) {
67
123
  const session = this.session(id), ratio = clampRatio(value);
68
124
  if (session.ratio === ratio) return;
69
125
  session.ratio = ratio;
126
+ // Dragging commits on pointer-up, so the id must stay dirty until then.
127
+ this.dirty.add(id);
70
128
  if (persist) this.save();
71
129
  }
72
130
  }