dsh-document-view 0.0.0-stage → 1.1.1
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/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.en.md +134 -0
- package/README.md +149 -2
- package/SECURITY.md +27 -0
- package/client.js +633 -0
- package/cordis.patch.yml +3 -0
- package/docs/COMPATIBILITY.md +69 -0
- package/docs/DETAIL.md +1 -0
- package/docs/DETAIL.zh.md +1 -0
- package/docs/PUBLISHING.md +125 -0
- package/docs/awesome-dsh-catalog-entry.json +12 -0
- package/docs/preview.svg +57 -0
- package/docs/screenshots/01-overview.png +0 -0
- package/docs/screenshots/02-settings.png +0 -0
- package/docs/screenshots/03-narrow.png +0 -0
- package/icon.svg +8 -0
- package/index.js +2 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +63 -4
- package/scripts/build.mjs +35 -0
- package/scripts/publish-npm.mjs +42 -0
- package/scripts/verify.mjs +71 -0
- package/src/collaboration.mjs +542 -0
- package/src/model.mjs +72 -0
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
// Mounted components stay in their original React ownership tree. Only the shell's
|
|
2
|
+
// grid geometry changes; no node is moved, cloned, or remounted by this module.
|
|
3
|
+
|
|
4
|
+
// Fail closed: this plugin depends on internal host services and DOM data
|
|
5
|
+
// attributes that are not a public API. If a future DSH release changes them,
|
|
6
|
+
// disable the plugin silently instead of throwing inside the module loader,
|
|
7
|
+
// so the host page keeps working. See docs/COMPATIBILITY.md for the checklist.
|
|
8
|
+
function hostContractIntact(scoped) {
|
|
9
|
+
const slots = scoped?.slots;
|
|
10
|
+
const sidebar = scoped?.sidebarRight;
|
|
11
|
+
if (typeof scoped?.effect !== 'function') return false;
|
|
12
|
+
if (!slots || typeof slots.inject !== 'function' || typeof slots.register !== 'function') return false;
|
|
13
|
+
if (!sidebar || typeof sidebar.isExpanded !== 'function') return false;
|
|
14
|
+
for (const store of [sidebar.mounted, sidebar.openTabs]) {
|
|
15
|
+
if (!store || typeof store.getSnapshot !== 'function' || typeof store.subscribe !== 'function') return false;
|
|
16
|
+
}
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function installDocumentCollaboration(ctx, require) {
|
|
21
|
+
if (typeof window === 'undefined') return;
|
|
22
|
+
ctx.inject(['slots', 'sidebarRight'], scoped => {
|
|
23
|
+
if (!hostContractIntact(scoped)) {
|
|
24
|
+
console.warn('[dsh-document-view] client host contract changed; plugin disabled safely. See docs/COMPATIBILITY.md');
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
scoped.effect(() => {
|
|
28
|
+
const React = require('react');
|
|
29
|
+
const { createPortal } = require('react-dom');
|
|
30
|
+
const sidebar = scoped.sidebarRight;
|
|
31
|
+
let storage;
|
|
32
|
+
try { storage = window.localStorage; } catch {}
|
|
33
|
+
const model = new CollaborationState(storage);
|
|
34
|
+
const listeners = new Set();
|
|
35
|
+
let revision = 0, fingerprint = '', raf = 0, disposed = false;
|
|
36
|
+
let frame, right, center, observedFrame, observedPanel, drag;
|
|
37
|
+
let pendingIntent, intentTimer;
|
|
38
|
+
const headerMarks = new Map();
|
|
39
|
+
const workbooks = new Map();
|
|
40
|
+
let lastAddress = '', lastSession = '';
|
|
41
|
+
let narrowView = 'document';
|
|
42
|
+
let divider, compact, focusView;
|
|
43
|
+
const focusMarks = new Map();
|
|
44
|
+
const style = document.createElement('style');
|
|
45
|
+
style.dataset.pluginCss = 'dsh-document-view/layout.css';
|
|
46
|
+
style.textContent = `
|
|
47
|
+
[data-fdc-active] { grid-template-columns:var(--fdc-columns) !important; transition:none !important; }
|
|
48
|
+
[data-fdc-active] > [data-fdc-center] { grid-column:3; grid-row:1; min-width:0; border-radius:0 !important; }
|
|
49
|
+
[data-fdc-active] > [data-rightbar-col] { grid-column:2; grid-row:1; position:relative; min-width:0; overflow:hidden; }
|
|
50
|
+
[data-fdc-active] [data-sidebar-right-panel] { left:0 !important; right:auto !important; top:0 !important; bottom:0 !important; width:100% !important; max-width:none !important; --dsh-sidebar-width:100% !important; }
|
|
51
|
+
[data-fdc-active] > [data-side="rightbar"] { display:none !important; }
|
|
52
|
+
[data-fdc-active] [data-fdc-center] { --dsh-composer-side-clearance:12px; --dsh-composer-card-max-width:100%; border-left:1px solid var(--dsw-alias-border-l3,#ddd); }
|
|
53
|
+
[data-fdc-divider] { position:absolute; top:var(--dsh-windows-titlebar-height,0px); bottom:0; width:8px; margin-left:-4px; z-index:30; cursor:col-resize; touch-action:none; }
|
|
54
|
+
[data-fdc-divider]:hover,[data-fdc-divider]:focus-visible { background:color-mix(in srgb,var(--dsw-alias-button-info-fill,#2563eb) 35%,transparent); outline:none; }
|
|
55
|
+
[data-fdc-active][data-fdc-narrow] > [data-fdc-center], [data-fdc-active][data-fdc-narrow] > [data-rightbar-col] { grid-column:2; padding-top:36px; }
|
|
56
|
+
[data-fdc-active][data-fdc-narrow="document"] > [data-fdc-center], [data-fdc-active][data-fdc-narrow="chat"] > [data-rightbar-col] { visibility:hidden; pointer-events:none; }
|
|
57
|
+
[data-fdc-active][data-fdc-narrow] [data-sidebar-right-panel] { top:36px !important; }
|
|
58
|
+
/* Reuse the host's version watcher and tab-local page/zoom state. Never
|
|
59
|
+
remount a document or refresh a dirty editor on our own timer. */
|
|
60
|
+
[data-fdc-active] [data-rightbar-col] span[hidden]:has([data-textpreview-tool="auto-refresh"]) { display:inline-flex !important; }
|
|
61
|
+
[data-fdc-active] [data-rightbar-col] [data-textpreview-tool="auto-refresh"] { width:auto; min-width:24px; padding:3px 6px; gap:4px; white-space:nowrap; }
|
|
62
|
+
[data-fdc-active] [data-rightbar-col] [data-textpreview-tool="auto-refresh"]::after { content:"已暂停"; font-size:11px; }
|
|
63
|
+
[data-fdc-active] [data-rightbar-col] [data-textpreview-tool="auto-refresh"][aria-pressed="true"]::after { content:"自动更新"; }
|
|
64
|
+
.fdc-control { display:inline-flex; flex:none; align-items:center; gap:0; min-width:0; height:28px; font-size:12px; line-height:22px; border-radius:6px; background:transparent; color:var(--dsw-alias-label-tertiary,#666); }
|
|
65
|
+
.fdc-control button { display:inline-flex; flex:none; align-items:center; justify-content:center; gap:5px; height:28px; box-sizing:border-box; color:inherit; background:transparent; border:0; border-radius:inherit; padding:3px 7px; font:inherit; cursor:pointer; white-space:nowrap; }
|
|
66
|
+
.fdc-control:has(button[aria-pressed="true"]) { background:var(--dsw-alias-fill-tsp-secondary,#f0f1f3); }
|
|
67
|
+
.fdc-control button[aria-pressed="true"] { color:var(--dsw-alias-state-business-primary,#2563eb); }
|
|
68
|
+
.fdc-control button:hover { color:var(--dsw-alias-label-primary,#333); }
|
|
69
|
+
.fdc-control button:focus-visible,[data-fdc-compact] button:focus-visible { outline:2px solid var(--dsw-alias-state-business-primary,#2563eb); outline-offset:2px; }
|
|
70
|
+
.fdc-control .fdc-settings { width:20px; padding:0; }
|
|
71
|
+
.fdc-panel { position:fixed; z-index:1000; box-sizing:border-box; width:min(260px,calc(100vw - 16px)); padding:12px; border:1px solid var(--dsw-alias-border-l3,#ddd); border-radius:var(--dsw-radius-md,8px); background:var(--dsw-alias-bg-base,#fff); box-shadow:0 4px 18px #0002; color:var(--dsw-alias-label-primary,#333); font-size:12px; }
|
|
72
|
+
.fdc-panel { max-height:calc(100vh - 16px); overflow-y:auto; }
|
|
73
|
+
.fdc-presets { display:flex; gap:4px; margin-top:10px; flex-wrap:wrap; }
|
|
74
|
+
.fdc-presets button { cursor:pointer; border:1px solid var(--dsw-alias-border-l3,#ddd); border-radius:4px; padding:4px 6px; font:inherit; color:inherit; background:transparent; }
|
|
75
|
+
.fdc-presets button[aria-pressed="true"] { color:var(--dsw-alias-state-business-primary,#2563eb); background:var(--dsw-alias-fill-tsp-secondary,#f0f1f3); }
|
|
76
|
+
.fdc-panel label { display:flex; align-items:flex-start; gap:6px; line-height:1.6; cursor:pointer; }
|
|
77
|
+
.fdc-panel p { color:var(--dsw-alias-label-secondary,#666); margin:8px 0 0; line-height:1.6; }
|
|
78
|
+
[data-fdc-active] [data-fdc-header] { padding-inline:12px; }
|
|
79
|
+
[data-fdc-active] [data-fdc-title-row] { display:grid; grid-template-columns:minmax(0,1fr) auto auto; gap:6px 8px; padding-inline-start:0; }
|
|
80
|
+
[data-fdc-active] [data-fdc-title-cluster] { display:contents; }
|
|
81
|
+
[data-fdc-active] [data-fdc-title-cluster] > nav { grid-column:1; grid-row:1; min-width:0; }
|
|
82
|
+
[data-fdc-active] [data-conversation-header-corner] { grid-column:3; grid-row:1; margin:0; }
|
|
83
|
+
[data-fdc-active] [data-fdc-header-actions] { grid-column:1/-1; grid-row:2; min-width:0; flex-wrap:wrap; gap:6px; }
|
|
84
|
+
[data-fdc-active] [data-fdc-header-utilities] { grid-column:2; grid-row:1; min-width:0; margin:0; gap:6px; }
|
|
85
|
+
[data-fdc-active] [data-conversation-tabs] { gap:18px; padding-left:0; overflow-x:auto; overscroll-behavior-x:contain; scrollbar-width:thin; }
|
|
86
|
+
[data-fdc-active] [data-conversation-tabs] > button { flex:none; white-space:nowrap; }
|
|
87
|
+
[data-fdc-compact] button { color:var(--dsw-alias-label-primary,#333); background:transparent; border:0; border-radius:var(--dsw-radius-xs,4px); padding:4px 7px; font:inherit; cursor:pointer; white-space:nowrap; }
|
|
88
|
+
[data-fdc-compact] button[aria-pressed="true"] { color:var(--dsw-alias-state-business-primary,#2563eb); background:var(--dsw-alias-fill-tsp-secondary,#f0f1f3); }
|
|
89
|
+
[data-fdc-compact] { position:absolute; top:var(--dsh-windows-titlebar-height,0px); right:8px; height:32px; z-index:35; display:flex; align-items:center; gap:4px; font-size:12px; background:var(--dsw-alias-bg-base,#fff); }
|
|
90
|
+
[data-fdc-compact][hidden],[data-fdc-divider][hidden] { display:none; }
|
|
91
|
+
`;
|
|
92
|
+
document.head.appendChild(style);
|
|
93
|
+
const notify = value => {
|
|
94
|
+
if (fingerprint === value) return;
|
|
95
|
+
fingerprint = value;
|
|
96
|
+
revision++;
|
|
97
|
+
listeners.forEach(listener => listener());
|
|
98
|
+
};
|
|
99
|
+
const schedule = () => {
|
|
100
|
+
if (!disposed && !raf) raf = requestAnimationFrame(() => { raf = 0; sync(); });
|
|
101
|
+
};
|
|
102
|
+
const currentId = () => sidebar.mounted.getSnapshot();
|
|
103
|
+
const active = () => { try { return sidebar.active(); } catch { return undefined; } };
|
|
104
|
+
function clearIntent() {
|
|
105
|
+
pendingIntent = undefined;
|
|
106
|
+
clearTimeout(intentTimer);
|
|
107
|
+
}
|
|
108
|
+
function commitDrag() {
|
|
109
|
+
if (drag?.changed) model.save();
|
|
110
|
+
drag = undefined;
|
|
111
|
+
}
|
|
112
|
+
function consumeIntent(id, address) {
|
|
113
|
+
if (!pendingIntent) return;
|
|
114
|
+
const intent = pendingIntent;
|
|
115
|
+
if (intent.id !== id || Date.now() >= intent.expires || !model.auto) { clearIntent(); return; }
|
|
116
|
+
if (!sidebar.isExpanded() || !isDocument(address)) return;
|
|
117
|
+
let matches = intent.requested === address;
|
|
118
|
+
const prefix = `dsh-resource://file/session/${id}/`;
|
|
119
|
+
if (!matches && intent.path && address.startsWith(prefix)) {
|
|
120
|
+
try {
|
|
121
|
+
const viewed = decodeURIComponent(address.slice(prefix.length)).replace(/\\/g,'/');
|
|
122
|
+
const clicked = intent.path.replace(/\\/g,'/');
|
|
123
|
+
matches = clicked === viewed || (!viewed.startsWith('/') && !/^[A-Za-z]:/.test(viewed) && clicked.endsWith(`/${viewed}`));
|
|
124
|
+
} catch {}
|
|
125
|
+
}
|
|
126
|
+
if (!matches) return;
|
|
127
|
+
clearIntent();
|
|
128
|
+
model.observe(id, address, true);
|
|
129
|
+
}
|
|
130
|
+
function setLayout(id, ratio) {
|
|
131
|
+
if (!id || currentId() !== id) return;
|
|
132
|
+
commitDrag();
|
|
133
|
+
model.setRatio(id, ratio);
|
|
134
|
+
schedule();
|
|
135
|
+
}
|
|
136
|
+
function focusNarrowPanel(panel) {
|
|
137
|
+
const chat = focusView === 'chat';
|
|
138
|
+
const region = chat ? center : panel;
|
|
139
|
+
focusView = undefined;
|
|
140
|
+
if (!region) return;
|
|
141
|
+
const target = [...region.querySelectorAll(chat ? 'textarea,[contenteditable="true"]' : 'iframe,[role="document"],[tabindex="0"]')].find(node =>
|
|
142
|
+
!node.disabled && node.getBoundingClientRect().width > 0 && getComputedStyle(node).visibility !== 'hidden');
|
|
143
|
+
if (target) target.focus({preventScroll:true});
|
|
144
|
+
else {
|
|
145
|
+
if (!focusMarks.has(region)) focusMarks.set(region, region.getAttribute('tabindex'));
|
|
146
|
+
region.setAttribute('tabindex','-1'); region.focus({preventScroll:true});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function toggle(id) {
|
|
150
|
+
clearIntent();
|
|
151
|
+
if (!id || id !== currentId()) return;
|
|
152
|
+
const tab = active();
|
|
153
|
+
model.toggle(id, tab?.contentId);
|
|
154
|
+
if (!model.session(id).enabled) {
|
|
155
|
+
right?.querySelector('[data-sidebar-right-mode="push"]')?.click();
|
|
156
|
+
}
|
|
157
|
+
if (model.session(id).enabled && !sidebar.isExpanded()) {
|
|
158
|
+
try {
|
|
159
|
+
if (isDocument(tab?.contentId) || tab?.kind === 'files') sidebar.toggleExpanded();
|
|
160
|
+
else sidebar.openTab('files');
|
|
161
|
+
} catch { /* The arriving session may not have adopted its sidebar yet. */ }
|
|
162
|
+
}
|
|
163
|
+
narrowView = 'document';
|
|
164
|
+
schedule();
|
|
165
|
+
}
|
|
166
|
+
function Controls({ collaborationSessionId: id, documentSeat }) {
|
|
167
|
+
React.useSyncExternalStore(listener => { listeners.add(listener); return () => listeners.delete(listener); }, () => revision);
|
|
168
|
+
const [settingsOpen, setSettingsOpen] = React.useState(false);
|
|
169
|
+
const anchor = React.useRef(null), popover = React.useRef(null);
|
|
170
|
+
React.useLayoutEffect(() => {
|
|
171
|
+
if (!settingsOpen) return;
|
|
172
|
+
const position = () => {
|
|
173
|
+
const bounds = anchor.current?.getBoundingClientRect(), panel = popover.current;
|
|
174
|
+
if (!bounds || !panel) return;
|
|
175
|
+
panel.style.left = `${Math.max(8, Math.min(window.innerWidth - panel.offsetWidth - 8, bounds.right - panel.offsetWidth))}px`;
|
|
176
|
+
panel.style.top = `${Math.max(8, Math.min(window.innerHeight - panel.offsetHeight - 8, bounds.bottom + 6))}px`;
|
|
177
|
+
};
|
|
178
|
+
const outside = event => {
|
|
179
|
+
if (!anchor.current?.contains(event.target) && !popover.current?.contains(event.target)) setSettingsOpen(false);
|
|
180
|
+
};
|
|
181
|
+
const escape = event => {
|
|
182
|
+
if (event.key === 'Escape') { event.stopPropagation(); setSettingsOpen(false); anchor.current?.focus(); }
|
|
183
|
+
};
|
|
184
|
+
position();
|
|
185
|
+
popover.current?.querySelector('input,button')?.focus({preventScroll:true});
|
|
186
|
+
window.addEventListener('resize',position); document.addEventListener('scroll',position,true);
|
|
187
|
+
document.addEventListener('pointerdown',outside,true); document.addEventListener('keydown',escape,true);
|
|
188
|
+
return () => {
|
|
189
|
+
window.removeEventListener('resize',position); document.removeEventListener('scroll',position,true);
|
|
190
|
+
document.removeEventListener('pointerdown',outside,true); document.removeEventListener('keydown',escape,true);
|
|
191
|
+
};
|
|
192
|
+
}, [settingsOpen]);
|
|
193
|
+
React.useEffect(() => { setSettingsOpen(false); }, [id]);
|
|
194
|
+
if (!id) return null;
|
|
195
|
+
const state = model.session(id);
|
|
196
|
+
const h = React.createElement;
|
|
197
|
+
if (documentSeat) return state.enabled ? h('div', {className:'fdc-control'}, h('button', {
|
|
198
|
+
type:'button', 'aria-label':'退出文档协作', onClick:() => toggle(id)
|
|
199
|
+
}, '退出协作')) : null;
|
|
200
|
+
const icon = h('svg', {width:14,height:14,viewBox:'0 0 16 16',fill:'none','aria-hidden':true},
|
|
201
|
+
h('rect',{x:2,y:2,width:12,height:12,rx:2,stroke:'currentColor'}),h('path',{d:'M9 2v12',stroke:'currentColor'}));
|
|
202
|
+
return h('div', { className:'fdc-control' },
|
|
203
|
+
h('button', { type:'button', 'aria-label':'文档协作模式', 'aria-pressed':state.enabled,
|
|
204
|
+
title:state.enabled ? '退出文档协作,恢复普通会话布局' : '文档放在中间,对话放在右侧', onClick:() => toggle(id) }, icon, '文档协作'),
|
|
205
|
+
h('button', { type:'button', className:'fdc-settings', ref:anchor, 'aria-label':'文档协作设置', 'aria-haspopup':'dialog', 'aria-expanded':settingsOpen,
|
|
206
|
+
onClick:() => setSettingsOpen(value => !value) }, h('svg',{width:12,height:12,viewBox:'0 0 16 16',fill:'none','aria-hidden':true},h('path',{d:'m4 6 4 4 4-4',stroke:'currentColor',strokeWidth:1.5}))),
|
|
207
|
+
settingsOpen && createPortal(h('div', {className:'fdc-panel',ref:popover,role:'dialog','aria-label':'文档协作选项'}, h('label', null, h('input', { type:'checkbox', checked:model.auto,
|
|
208
|
+
onChange:event => { model.setAuto(event.target.checked); notify(`${fingerprint}:auto:${model.auto}`); schedule(); } }), '查看文档时自动进入协作模式'),
|
|
209
|
+
h('div', {className:'fdc-presets',role:'group','aria-label':'分栏布局'},
|
|
210
|
+
...[['文档优先',0.35],['均衡',0.5],['对话优先',0.65]].map(([label,ratio]) => h('button', {
|
|
211
|
+
key:label,type:'button','aria-pressed':Math.abs(state.ratio-ratio)<0.001,
|
|
212
|
+
onClick:() => setLayout(id,ratio)
|
|
213
|
+
},label))),
|
|
214
|
+
h('p', null, '拖动分隔线调整宽度,双击或按 Home 恢复默认。'),
|
|
215
|
+
h('p', null, '默认关闭。点击文件或切换文件标签时生效;AI 后台读写文件不会切换布局。手动退出后,同一文件不会反复开启。')),document.body));
|
|
216
|
+
}
|
|
217
|
+
function restoreFocusMarks() {
|
|
218
|
+
for (const [node,value] of focusMarks) {
|
|
219
|
+
if (value === null) node.removeAttribute('tabindex');
|
|
220
|
+
else node.setAttribute('tabindex',value);
|
|
221
|
+
}
|
|
222
|
+
focusMarks.clear();
|
|
223
|
+
}
|
|
224
|
+
function removeGeometry() {
|
|
225
|
+
restoreFocusMarks();
|
|
226
|
+
if (frame) {
|
|
227
|
+
frame.removeAttribute('data-fdc-active');
|
|
228
|
+
frame.removeAttribute('data-fdc-narrow');
|
|
229
|
+
frame.style.removeProperty('--fdc-columns');
|
|
230
|
+
}
|
|
231
|
+
if (divider) divider.hidden = true;
|
|
232
|
+
if (compact) compact.hidden = true;
|
|
233
|
+
commitDrag(); focusView = undefined;
|
|
234
|
+
}
|
|
235
|
+
function markHeader() {
|
|
236
|
+
const actions = center?.querySelector('[data-slot="conversation.session.header.actions"]')?.parentElement;
|
|
237
|
+
const cluster = actions?.parentElement, row = cluster?.parentElement;
|
|
238
|
+
if (!cluster?.querySelector('nav') || !row?.querySelector('[data-conversation-header-corner]')) return;
|
|
239
|
+
const elements = [[actions,'data-fdc-header-actions'],[cluster,'data-fdc-title-cluster'],[row,'data-fdc-title-row'],[row.parentElement,'data-fdc-header']];
|
|
240
|
+
const utilities = row.querySelector('[data-slot="conversation.session.header.utilities"]')?.parentElement;
|
|
241
|
+
if (utilities) elements.push([utilities,'data-fdc-header-utilities']);
|
|
242
|
+
for (const [node, attribute] of elements) { node.setAttribute(attribute,''); headerMarks.set(node,attribute); }
|
|
243
|
+
for (const [node, attribute] of headerMarks) {
|
|
244
|
+
if (!node.isConnected) { node.removeAttribute(attribute); headerMarks.delete(node); }
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
const workbookZoom = node => parseFloat(node.querySelector('.fortune-zoom-ratio-current')?.textContent || '') / 100;
|
|
248
|
+
function zoomStep(node, goal) {
|
|
249
|
+
const current = workbookZoom(node);
|
|
250
|
+
if (!Number.isFinite(current) || Math.abs(current - goal) < 0.05) return false;
|
|
251
|
+
// Native controls update both the canvas and the displayed percentage.
|
|
252
|
+
// One click per animation frame avoids batching repeated stale ratios.
|
|
253
|
+
const buttons = node.querySelectorAll('.fortune-zoom-container .fortune-zoom-button[role="button"]');
|
|
254
|
+
const button = current < goal ? buttons[1] : buttons[0];
|
|
255
|
+
if (!button) return false;
|
|
256
|
+
button.click();
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
function syncWorkbook(panel, enabled, fullscreen, projectedWidth) {
|
|
260
|
+
for (const [node] of workbooks) if (!node.isConnected) workbooks.delete(node);
|
|
261
|
+
const node = Array.from(panel?.querySelectorAll('[data-excel-preview]') || []).find(element =>
|
|
262
|
+
!element.closest('[hidden]') && element.getBoundingClientRect().width > 0 && getComputedStyle(element).visibility !== 'hidden');
|
|
263
|
+
if (!node) return;
|
|
264
|
+
const width = node.getBoundingClientRect().width, zoom = workbookZoom(node);
|
|
265
|
+
if (!Number.isFinite(zoom) || width < 1) return;
|
|
266
|
+
let record = workbooks.get(node);
|
|
267
|
+
if (!record) {
|
|
268
|
+
record = { mode:'push', width:fullscreen ? projectedWidth : width, zoom, manual:false };
|
|
269
|
+
workbooks.set(node,record);
|
|
270
|
+
}
|
|
271
|
+
if (record.switching) return;
|
|
272
|
+
if (enabled && fullscreen) {
|
|
273
|
+
if (record.mode !== 'fullscreen') { record.mode = 'fullscreen'; record.manual = false; }
|
|
274
|
+
if (!record.manual) {
|
|
275
|
+
const goal = Math.max(0.1, Math.min(4, Math.round(record.zoom * width / Math.max(1,record.width) * 10) / 10));
|
|
276
|
+
if (zoomStep(node,goal)) schedule();
|
|
277
|
+
}
|
|
278
|
+
} else {
|
|
279
|
+
if (record.mode === 'fullscreen') record.mode = record.manual ? 'push' : 'restoring';
|
|
280
|
+
if (record.mode === 'restoring' && zoomStep(node,record.zoom)) { schedule(); return; }
|
|
281
|
+
record.mode = 'push'; record.width = width; record.zoom = workbookZoom(node); record.manual = false;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
function manualZoom(event) {
|
|
285
|
+
if (!event.isTrusted || !(event.target instanceof Element)) return;
|
|
286
|
+
const keyboard = event.type === 'keydown' && (event.ctrlKey || event.metaKey) && ['+','=','-','0'].includes(event.key);
|
|
287
|
+
const control = keyboard ? event.target : event.target.closest('.fortune-zoom-container');
|
|
288
|
+
const node = control?.closest('[data-excel-preview]');
|
|
289
|
+
const record = node && workbooks.get(node);
|
|
290
|
+
if (record) { record.manual = true; if (record.mode === 'restoring') record.mode = 'push'; }
|
|
291
|
+
}
|
|
292
|
+
function sheetGesture(event) {
|
|
293
|
+
if (!event.isTrusted || !(event.target instanceof Element)) return;
|
|
294
|
+
const target = event.target.closest('.fortune-sheettab-container-c > *, .fortune-sheet-list-item');
|
|
295
|
+
const node = target?.closest('[data-excel-preview]'), record = node && workbooks.get(node);
|
|
296
|
+
if (!record || record.mode !== 'fullscreen') return;
|
|
297
|
+
if (record.manual) { workbooks.delete(node); return; }
|
|
298
|
+
event.preventDefault(); event.stopImmediatePropagation();
|
|
299
|
+
if (record.switching) return;
|
|
300
|
+
record.switching = true;
|
|
301
|
+
let remaining = 40;
|
|
302
|
+
const restore = () => {
|
|
303
|
+
if (disposed || !node.isConnected || !target.isConnected) return;
|
|
304
|
+
if (!record.manual && remaining-- && zoomStep(node,record.zoom)) { requestAnimationFrame(restore); return; }
|
|
305
|
+
workbooks.delete(node);
|
|
306
|
+
if (event.type === 'contextmenu') target.dispatchEvent(new MouseEvent('contextmenu', {bubbles:true,cancelable:true,clientX:event.clientX,clientY:event.clientY,button:2}));
|
|
307
|
+
else target.click();
|
|
308
|
+
schedule();
|
|
309
|
+
};
|
|
310
|
+
restore();
|
|
311
|
+
}
|
|
312
|
+
// Read the host's inline track, never our own overridden computed grid.
|
|
313
|
+
// Measuring the sidebar here feeds the previous collaboration width back
|
|
314
|
+
// into the next layout, preventing both collapse and native resizing.
|
|
315
|
+
function sidebarWidth() {
|
|
316
|
+
const track = frame?.style.gridTemplateColumns.match(/^\s*(\d+(?:\.\d+)?)px(?:\s|$)/);
|
|
317
|
+
if (track) return Number(track[1]);
|
|
318
|
+
if (frame?.hasAttribute('data-sidebar-collapsed')) return 0;
|
|
319
|
+
return center?.previousElementSibling?.getBoundingClientRect().width || 0;
|
|
320
|
+
}
|
|
321
|
+
function markShell() {
|
|
322
|
+
const column = document.querySelector('[data-rightbar-col]');
|
|
323
|
+
const next = column?.parentElement;
|
|
324
|
+
// The collapsed marker disappears when the panel opens. The owning
|
|
325
|
+
// frame's explicit grid tracks remain present in both presentations.
|
|
326
|
+
if (!next || !next.style.gridTemplateColumns) return false;
|
|
327
|
+
if (frame !== next) {
|
|
328
|
+
removeGeometry();
|
|
329
|
+
center?.removeAttribute('data-fdc-center');
|
|
330
|
+
divider?.remove(); compact?.remove();
|
|
331
|
+
frame = next;
|
|
332
|
+
frameObserver.disconnect(); nativeColumns = frame.style.gridTemplateColumns;
|
|
333
|
+
frameObserver.observe(frame, {attributes:true,attributeFilter:['style']});
|
|
334
|
+
right = column; center = right.previousElementSibling;
|
|
335
|
+
if (!center) return false;
|
|
336
|
+
center.setAttribute('data-fdc-center', '');
|
|
337
|
+
divider = document.createElement('div');
|
|
338
|
+
divider.dataset.fdcDivider = '';
|
|
339
|
+
divider.setAttribute('role','separator'); divider.setAttribute('aria-label','调整文档与对话宽度');
|
|
340
|
+
divider.title = '拖动调整宽度;双击或 Home 恢复默认';
|
|
341
|
+
divider.addEventListener('dblclick', () => setLayout(currentId(),0.35));
|
|
342
|
+
divider.setAttribute('aria-orientation','vertical'); divider.tabIndex = 0; divider.hidden = true;
|
|
343
|
+
divider.addEventListener('pointerdown', event => {
|
|
344
|
+
if (event.button !== 0 || !currentId()) return;
|
|
345
|
+
event.preventDefault();
|
|
346
|
+
commitDrag();
|
|
347
|
+
drag = { id:currentId(), pointer:event.pointerId, changed:false };
|
|
348
|
+
divider.setPointerCapture(event.pointerId);
|
|
349
|
+
});
|
|
350
|
+
divider.addEventListener('pointermove', event => {
|
|
351
|
+
if (!drag || drag.pointer !== event.pointerId || drag.id !== currentId()) return;
|
|
352
|
+
const rect = frame.getBoundingClientRect();
|
|
353
|
+
const left = sidebarWidth();
|
|
354
|
+
const previous = model.session(drag.id).ratio;
|
|
355
|
+
model.setRatio(drag.id, (rect.right - event.clientX) / Math.max(1, rect.width - left), false);
|
|
356
|
+
drag.changed ||= previous !== model.session(drag.id).ratio;
|
|
357
|
+
schedule();
|
|
358
|
+
});
|
|
359
|
+
const release = commitDrag;
|
|
360
|
+
divider.addEventListener('pointerup', release); divider.addEventListener('pointercancel', release); divider.addEventListener('lostpointercapture', release);
|
|
361
|
+
divider.addEventListener('keydown', event => {
|
|
362
|
+
const id = currentId();
|
|
363
|
+
if (!id || !['ArrowLeft','ArrowRight','Home'].includes(event.key)) return;
|
|
364
|
+
event.preventDefault();
|
|
365
|
+
const width = frame.getBoundingClientRect().width, left = sidebarWidth();
|
|
366
|
+
const geometry = solveColumns(width,left,model.session(id).ratio);
|
|
367
|
+
const available = width-left;
|
|
368
|
+
if (geometry.narrow || available <= 0) return;
|
|
369
|
+
const ratio = geometry.chat/available;
|
|
370
|
+
const minimum = Math.max(0.2,400/available), maximum = Math.min(0.65,1-360/available);
|
|
371
|
+
setLayout(id,event.key === 'Home' ? 0.35 : Math.max(minimum,Math.min(maximum,ratio+(event.key === 'ArrowLeft' ? 0.02 : -0.02))));
|
|
372
|
+
schedule();
|
|
373
|
+
});
|
|
374
|
+
compact = document.createElement('div'); compact.dataset.fdcCompact = ''; compact.hidden = true;
|
|
375
|
+
for (const [label, view] of [['文档','document'],['对话','chat'],['退出协作','exit']]) {
|
|
376
|
+
const button = document.createElement('button'); button.type = 'button'; button.textContent = label;
|
|
377
|
+
button.dataset.view = view;
|
|
378
|
+
button.addEventListener('click', () => {
|
|
379
|
+
if (view === 'exit') toggle(currentId());
|
|
380
|
+
else { narrowView = view; focusView = view; schedule(); }
|
|
381
|
+
});
|
|
382
|
+
compact.appendChild(button);
|
|
383
|
+
}
|
|
384
|
+
frame.append(divider, compact);
|
|
385
|
+
}
|
|
386
|
+
if (observedFrame !== frame) { resize.disconnect(); resize.observe(frame); observedFrame = frame; observedPanel = undefined; }
|
|
387
|
+
markHeader();
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
function sync() {
|
|
391
|
+
if (disposed) return;
|
|
392
|
+
const focused = document.activeElement;
|
|
393
|
+
const id = currentId();
|
|
394
|
+
if (drag && drag.id !== id) commitDrag();
|
|
395
|
+
if (!id) { clearIntent(); removeGeometry(); notify('none'); return; }
|
|
396
|
+
const tab = active();
|
|
397
|
+
const address = tab?.contentId || '';
|
|
398
|
+
const state = model.session(id);
|
|
399
|
+
const inventory = sidebar.openTabs.getSnapshot();
|
|
400
|
+
const docs = inventory.filter(entry => entry.sessionId === id && isDocument(entry.contentId)).length;
|
|
401
|
+
model.observeCount(id, docs);
|
|
402
|
+
consumeIntent(id, address);
|
|
403
|
+
const expanded = sidebar.isExpanded();
|
|
404
|
+
const changed = id !== lastSession || address !== lastAddress;
|
|
405
|
+
if (changed) narrowView = 'document';
|
|
406
|
+
lastSession = id; lastAddress = address;
|
|
407
|
+
const shellReady = markShell();
|
|
408
|
+
const panel = right?.querySelector(`[data-sidebar-right-session="${CSS.escape(id)}"][data-sidebar-right-panel]`);
|
|
409
|
+
if (observedPanel !== panel) {
|
|
410
|
+
if (observedPanel) resize.unobserve(observedPanel);
|
|
411
|
+
if (panel) resize.observe(panel);
|
|
412
|
+
observedPanel = panel;
|
|
413
|
+
}
|
|
414
|
+
const width = frame?.getBoundingClientRect().width || 0;
|
|
415
|
+
const left = sidebarWidth();
|
|
416
|
+
const geometry = solveColumns(width, left, state.ratio);
|
|
417
|
+
// On wide windows, a native fullscreen request is respected. On narrow
|
|
418
|
+
// windows, native auto-fullscreen is adapted to the document/chat switch.
|
|
419
|
+
const fullscreen = panel?.getAttribute('data-sidebar-right-panel') === 'fullscreen' && !geometry.narrow;
|
|
420
|
+
const visible = shellReady && state.enabled && expanded && !fullscreen && (isDocument(address) || tab?.kind === 'files');
|
|
421
|
+
if (visible) {
|
|
422
|
+
frame.setAttribute('data-fdc-active','');
|
|
423
|
+
const columns = geometry.narrow ? `${left}px minmax(0,1fr) 0px` : `${left}px minmax(0,1fr) ${geometry.chat}px`;
|
|
424
|
+
if (frame.style.getPropertyValue('--fdc-columns') !== columns) frame.style.setProperty('--fdc-columns', columns);
|
|
425
|
+
if (geometry.narrow) frame.setAttribute('data-fdc-narrow', narrowView);
|
|
426
|
+
else frame.removeAttribute('data-fdc-narrow');
|
|
427
|
+
divider.hidden = geometry.narrow;
|
|
428
|
+
divider.style.left = `${left + geometry.document}px`;
|
|
429
|
+
const available = Math.max(1,width-left);
|
|
430
|
+
const actual = Math.round(geometry.chat/available*100);
|
|
431
|
+
divider.setAttribute('aria-valuenow',String(actual));
|
|
432
|
+
divider.setAttribute('aria-valuemin',String(Math.round(Math.max(0.2,400/available)*100)));
|
|
433
|
+
divider.setAttribute('aria-valuemax',String(Math.round(Math.min(0.65,1-360/available)*100)));
|
|
434
|
+
divider.setAttribute('aria-valuetext',`对话 ${actual}%,文档 ${100-actual}%`);
|
|
435
|
+
compact.hidden = !geometry.narrow;
|
|
436
|
+
if (geometry.narrow && !focusView) {
|
|
437
|
+
const hiddenRegion = narrowView === 'document' ? center : right;
|
|
438
|
+
if (hiddenRegion?.contains(focused)) compact.querySelector(`[data-view="${narrowView}"]`)?.focus({preventScroll:true});
|
|
439
|
+
}
|
|
440
|
+
for (const button of compact.children) {
|
|
441
|
+
const value=String(button.dataset.view === narrowView);
|
|
442
|
+
if(button.getAttribute('aria-pressed')!==value) button.setAttribute('aria-pressed',value);
|
|
443
|
+
}
|
|
444
|
+
} else removeGeometry();
|
|
445
|
+
if (visible && geometry.narrow && focusView) focusNarrowPanel(panel);
|
|
446
|
+
else if (!geometry.narrow) focusView = undefined;
|
|
447
|
+
syncWorkbook(panel, state.enabled, fullscreen, geometry.document);
|
|
448
|
+
notify(JSON.stringify([id, state.enabled, model.auto, address, geometry.narrow, narrowView, visible, state.ratio]));
|
|
449
|
+
}
|
|
450
|
+
function viewGesture(event) {
|
|
451
|
+
if (!event.isTrusted) return;
|
|
452
|
+
const target = event.target instanceof Element ? event.target : document.activeElement;
|
|
453
|
+
if (!target || target.closest('.fdc-control,[data-fdc-compact],[data-fdc-divider]')) return;
|
|
454
|
+
if (event.type === 'keydown' && !['Enter',' '].includes(event.key)) return;
|
|
455
|
+
if (target.closest('[data-dockkit-tab-close]')) return;
|
|
456
|
+
const tabNode = target.closest('[data-dockkit-tab]');
|
|
457
|
+
const fileAction = target.closest('[data-rightbar-col]') && target.closest('button,[role="button"],[data-files-entry="file"]');
|
|
458
|
+
const resourceLink = target.closest('a[href^="dsh-resource://file/"],[data-resource-address^="dsh-resource://file/"]');
|
|
459
|
+
// Native deliverable cards and Markdown file mentions are buttons whose
|
|
460
|
+
// title carries the path, rather than links carrying a resource URL.
|
|
461
|
+
const presentedPreview = target.closest('[data-presented-file] > button[title]');
|
|
462
|
+
const mentionPreview = target.closest('code > button[title][aria-label]');
|
|
463
|
+
if (!tabNode && !fileAction && !resourceLink && !presentedPreview && !mentionPreview) return;
|
|
464
|
+
const id = currentId();
|
|
465
|
+
const intendedTab = tabNode && sidebar.openTabs.getSnapshot().find(entry =>
|
|
466
|
+
entry.sessionId === id && entry.tabId === tabNode.dataset.dockkitTab);
|
|
467
|
+
const requested = intendedTab?.contentId || resourceLink?.getAttribute('href') || resourceLink?.getAttribute('data-resource-address');
|
|
468
|
+
const row = presentedPreview || mentionPreview || target.closest('[data-files-entry="file"],[role="button"][title]');
|
|
469
|
+
const requestedPath = row?.getAttribute('data-files-path') || row?.getAttribute('title');
|
|
470
|
+
if (!isDocument(requested) && !requestedPath) return;
|
|
471
|
+
clearIntent();
|
|
472
|
+
if (!model.auto || !id) return;
|
|
473
|
+
pendingIntent = {id, requested, path:requestedPath, expires:Date.now()+5000};
|
|
474
|
+
intentTimer = setTimeout(clearIntent, 5000);
|
|
475
|
+
// The next frame handles synchronous opens; subscriptions handle delayed ones.
|
|
476
|
+
schedule();
|
|
477
|
+
}
|
|
478
|
+
const resize = new ResizeObserver(schedule);
|
|
479
|
+
const frameObserver = new MutationObserver(() => {
|
|
480
|
+
const columns = frame?.style.gridTemplateColumns;
|
|
481
|
+
if (columns !== nativeColumns) { nativeColumns = columns; schedule(); }
|
|
482
|
+
});
|
|
483
|
+
let nativeColumns = '';
|
|
484
|
+
const structuralSelector = '[data-rightbar-col],[data-sidebar-right-panel],[data-excel-preview],[data-slot="conversation.session.header.actions"],.fortune-zoom-container';
|
|
485
|
+
const observer = new MutationObserver(records => {
|
|
486
|
+
if (records.some(record => {
|
|
487
|
+
if (record.type === 'attributes') {
|
|
488
|
+
return record.target === frame || record.target.matches('[data-sidebar-right-panel]') ||
|
|
489
|
+
(record.target.closest('[data-rightbar-col]') && record.attributeName === 'hidden');
|
|
490
|
+
}
|
|
491
|
+
return [...record.addedNodes,...record.removedNodes].some(node => node instanceof Element &&
|
|
492
|
+
(node.matches(structuralSelector) || node.querySelector(structuralSelector)));
|
|
493
|
+
})) schedule();
|
|
494
|
+
});
|
|
495
|
+
observer.observe(document.body, { childList:true, subtree:true, attributes:true,
|
|
496
|
+
attributeFilter:['data-sidebar-right-open','data-sidebar-right-panel','hidden','aria-selected','data-sidebar-collapsed','data-rightbar-collapsed'] });
|
|
497
|
+
document.addEventListener('click', viewGesture, true);
|
|
498
|
+
document.addEventListener('keydown', viewGesture, true);
|
|
499
|
+
document.addEventListener('click', manualZoom, true);
|
|
500
|
+
document.addEventListener('keydown', manualZoom, true);
|
|
501
|
+
document.addEventListener('click', sheetGesture, true);
|
|
502
|
+
document.addEventListener('contextmenu', sheetGesture, true);
|
|
503
|
+
const unsubMounted = sidebar.mounted.subscribe(schedule);
|
|
504
|
+
const unsubTabs = sidebar.openTabs.subscribe(schedule);
|
|
505
|
+
const disposers = [];
|
|
506
|
+
for (const seat of ['conversation.session.header.actions','sidebar.right.tab.document.actions']) {
|
|
507
|
+
disposers.push(scoped.slots.inject(seat, () => scoped.slots.register({
|
|
508
|
+
name:seat, id:`dsh-document-view:${seat}`, order:5,
|
|
509
|
+
inject:sessionId => ({ collaborationSessionId:sessionId, documentSeat:seat.startsWith('sidebar.') })
|
|
510
|
+
}, Controls)));
|
|
511
|
+
}
|
|
512
|
+
schedule();
|
|
513
|
+
return () => {
|
|
514
|
+
disposed = true; clearIntent(); commitDrag(); cancelAnimationFrame(raf); resize.disconnect(); observer.disconnect(); frameObserver.disconnect();
|
|
515
|
+
document.removeEventListener('click',viewGesture,true); document.removeEventListener('keydown',viewGesture,true);
|
|
516
|
+
document.removeEventListener('click',manualZoom,true);
|
|
517
|
+
document.removeEventListener('keydown',manualZoom,true);
|
|
518
|
+
document.removeEventListener('click',sheetGesture,true);
|
|
519
|
+
document.removeEventListener('contextmenu',sheetGesture,true);
|
|
520
|
+
unsubMounted(); unsubTabs(); disposers.forEach(dispose => dispose());
|
|
521
|
+
removeGeometry(); center?.removeAttribute('data-fdc-center'); divider?.remove(); compact?.remove(); style.remove(); listeners.clear();
|
|
522
|
+
for (const [node,attribute] of headerMarks) node.removeAttribute(attribute);
|
|
523
|
+
headerMarks.clear();
|
|
524
|
+
// Restore temporary native zoom even if the plugin is disabled while a
|
|
525
|
+
// workbook stays mounted. Stop immediately on a subsequent user change.
|
|
526
|
+
for (const [node,record] of workbooks) {
|
|
527
|
+
if (record.mode === 'push' || record.manual) continue;
|
|
528
|
+
let expected = workbookZoom(node), remaining = 40;
|
|
529
|
+
const restore = () => {
|
|
530
|
+
if (!node.isConnected || !remaining-- || Math.abs(workbookZoom(node) - expected) > 0.05) return;
|
|
531
|
+
if (!zoomStep(node,record.zoom)) return;
|
|
532
|
+
expected += expected < record.zoom ? 0.1 : -0.1;
|
|
533
|
+
requestAnimationFrame(restore);
|
|
534
|
+
};
|
|
535
|
+
restore();
|
|
536
|
+
}
|
|
537
|
+
workbooks.clear();
|
|
538
|
+
restoreFocusMarks();
|
|
539
|
+
};
|
|
540
|
+
}, 'dsh-document-view');
|
|
541
|
+
});
|
|
542
|
+
}
|
package/src/model.mjs
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const STORAGE_KEY = 'dsh.document-view.v1';
|
|
2
|
+
const clampRatio = value => Number.isFinite(value) ? Math.max(0.2, Math.min(0.65, value)) : 0.35;
|
|
3
|
+
|
|
4
|
+
export function isDocument(address) {
|
|
5
|
+
return typeof address === 'string' && address.startsWith('dsh-resource://file/');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function solveColumns(width, sidebar, ratio) {
|
|
9
|
+
width = Math.max(0, Number.isFinite(width) ? width : 0);
|
|
10
|
+
sidebar = Math.max(0, Math.min(width, Number.isFinite(sidebar) ? sidebar : 0));
|
|
11
|
+
const available = width - sidebar;
|
|
12
|
+
if (available < 760) return { sidebar, document: available, chat: 0, narrow: true };
|
|
13
|
+
const chat = Math.round(Math.max(400, Math.min(available - 360, available * clampRatio(ratio))));
|
|
14
|
+
return { sidebar, document: available - chat, chat, narrow: false };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class CollaborationState {
|
|
18
|
+
constructor(storage) {
|
|
19
|
+
this.storage = storage;
|
|
20
|
+
this.auto = false;
|
|
21
|
+
this.sessions = Object.create(null);
|
|
22
|
+
this.counts = new Map();
|
|
23
|
+
try {
|
|
24
|
+
const saved = JSON.parse(storage?.getItem(STORAGE_KEY) || '{}');
|
|
25
|
+
this.auto = saved.auto === true;
|
|
26
|
+
for (const [id, value] of Object.entries(saved.sessions || {})) {
|
|
27
|
+
if (!value || typeof value !== 'object') continue;
|
|
28
|
+
this.sessions[id] = {
|
|
29
|
+
enabled: value.enabled === true,
|
|
30
|
+
ratio: clampRatio(value.ratio),
|
|
31
|
+
suppressed: typeof value.suppressed === 'string' ? value.suppressed : ''
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
} catch { /* Storage corruption must never block the shell. */ }
|
|
35
|
+
}
|
|
36
|
+
session(id) {
|
|
37
|
+
return this.sessions[id] ||= { enabled: false, ratio: 0.35, suppressed: '' };
|
|
38
|
+
}
|
|
39
|
+
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. */ }
|
|
42
|
+
}
|
|
43
|
+
toggle(id, address) {
|
|
44
|
+
const session = this.session(id);
|
|
45
|
+
session.enabled = !session.enabled;
|
|
46
|
+
session.suppressed = session.enabled ? '' : address || '';
|
|
47
|
+
this.save();
|
|
48
|
+
}
|
|
49
|
+
observe(id, address, userGesture) {
|
|
50
|
+
const session = this.session(id);
|
|
51
|
+
if (this.auto && userGesture && isDocument(address) && session.suppressed !== address && !session.enabled) {
|
|
52
|
+
session.enabled = true;
|
|
53
|
+
session.suppressed = '';
|
|
54
|
+
this.save();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
observeCount(id, count) {
|
|
58
|
+
const previous = this.counts.get(id);
|
|
59
|
+
this.counts.set(id, count);
|
|
60
|
+
if (previous > 0 && count === 0) {
|
|
61
|
+
this.session(id).enabled = false;
|
|
62
|
+
this.save();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
setAuto(value) { this.auto = value === true; this.save(); }
|
|
66
|
+
setRatio(id, value, persist = true) {
|
|
67
|
+
const session = this.session(id), ratio = clampRatio(value);
|
|
68
|
+
if (session.ratio === ratio) return;
|
|
69
|
+
session.ratio = ratio;
|
|
70
|
+
if (persist) this.save();
|
|
71
|
+
}
|
|
72
|
+
}
|