forked from lioensky/VCPChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptorium-render-coordinator.js
More file actions
315 lines (289 loc) · 11.1 KB
/
Copy pathscriptorium-render-coordinator.js
File metadata and controls
315 lines (289 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
'use strict';
(() => {
function createRenderCoordinator(context = {}) {
const documentPort = context.documentPort;
if (!documentPort) {
throw new TypeError('Render coordinator requires DocumentPort.');
}
const state = {
adapter: null,
mode: 'edit',
zoom: 100,
editSurface: null,
readSurface: null,
editRevision: -1,
readRevision: -1,
editDocumentId: null,
readDocumentId: null,
invalidationRevision: 0,
disposed: false,
};
const disposers = [];
function assertActive() {
if (state.disposed) {
throw new Error('Render coordinator has been disposed.');
}
}
function currentAdapter() {
assertActive();
if (!state.adapter) throw new Error('No document adapter is active.');
return state.adapter;
}
function setAdapter(adapter) {
assertActive();
if (!adapter
|| typeof adapter.renderEditSurface !== 'function'
|| typeof adapter.renderReadSurface !== 'function') {
throw new TypeError('Render coordinator requires a document adapter.');
}
if (state.adapter === adapter) return adapter;
disposeSurfaces();
state.adapter?.disposeSurface?.();
state.adapter = adapter;
invalidate('adapter-changed');
context.onAdapterChange?.(adapter);
return adapter;
}
function setMode(mode) {
assertActive();
if (!['edit', 'read', 'source-html', 'source-css'].includes(mode)) {
throw new TypeError(`Unsupported surface mode: ${mode}`);
}
state.mode = mode;
return mode;
}
function setZoom(value) {
assertActive();
state.zoom = Math.max(50, Math.min(200, Number(value) || 100));
const editRoot = state.editSurface?.root;
const readRoot = state.readSurface?.root;
context.primitives?.updateZoomLayout?.(editRoot, state.zoom);
context.primitives?.updateZoomLayout?.(readRoot, state.zoom);
context.onZoomChange?.(state.zoom);
return state.zoom;
}
function cacheMatches(surface) {
const status = documentPort.status();
if (surface === 'edit') {
return state.editSurface
&& state.editRevision === status.revision
&& state.editDocumentId === status.documentId;
}
return state.readSurface
&& state.readRevision === status.revision
&& state.readDocumentId === status.documentId;
}
function renderEdit(options = {}) {
const adapter = currentAdapter();
const target = options.target || context.editHost;
if (!target) throw new Error('Edit surface host is unavailable.');
// 渲染态输入会话拥有当前 contenteditable DOM 的所有权。
// IME 组合期间禁止销毁该 DOM;只保留最后一次渲染请求,待
// 编辑器完成“渲染态缓冲 -> 源码”对齐后再执行。
if (context.editorPort?.inputPending?.()) {
context.editorPort.deferRender?.(() =>
renderEdit(options)
);
return state.editSurface;
}
if (!options.force && cacheMatches('edit')) {
activateRuntime('edit');
return state.editSurface;
}
const scrollHost =
options.scrollHost || context.editScrollHost;
const preservedScroll = options.preserveScroll === false
? null
: {
left: Number(scrollHost?.scrollLeft) || 0,
top: Number(scrollHost?.scrollTop) || 0,
};
state.editSurface?.dispose?.();
state.editSurface = adapter.renderEditSurface(target, {
...options,
zoom: state.zoom,
scrollHost,
});
const renderedSurface = state.editSurface;
const status = documentPort.status();
state.editRevision = status.revision;
state.editDocumentId = status.documentId;
state.mode = 'edit';
// 首次脚本激活必须发生在 coordinator 已正式接管 surface 之后。
// renderer 内部的下一帧仍作为布局完成后的幂等兜底,但动画岛
// 不再依赖一个可能在 surface 交接期间被取消的悬空 RAF 才能启动。
context.runtimePort?.activate?.({
kind: adapter.kind,
surface: 'edit',
root: state.editSurface.root,
adapter,
scrollHost,
});
context.onRendered?.({
surface: 'edit',
adapter,
result: state.editSurface,
});
if (preservedScroll && scrollHost) {
window.requestAnimationFrame(() => {
if (state.disposed
|| state.editSurface !== renderedSurface) {
return;
}
scrollHost.scrollTo?.({
left: preservedScroll.left,
top: preservedScroll.top,
behavior: 'auto',
});
});
}
return state.editSurface;
}
function renderRead(options = {}) {
const adapter = currentAdapter();
const target = options.target || context.readHost;
if (!target) throw new Error('Read surface host is unavailable.');
if (!options.force && cacheMatches('read')) {
activateRuntime('read');
return state.readSurface;
}
state.readSurface?.dispose?.();
const scrollHost =
options.scrollHost || context.readScrollHost;
state.readSurface = adapter.renderReadSurface(target, {
...options,
zoom: state.zoom,
scrollHost,
});
const status = documentPort.status();
state.readRevision = status.revision;
state.readDocumentId = status.documentId;
state.mode = 'read';
context.runtimePort?.activate?.({
kind: adapter.kind,
surface: 'read',
root: state.readSurface.root,
adapter,
scrollHost,
});
context.onRendered?.({
surface: 'read',
adapter,
result: state.readSurface,
});
return state.readSurface;
}
function renderCurrent(options = {}) {
return state.mode === 'read'
? renderRead(options)
: renderEdit(options);
}
function activateRuntime(surface = state.mode) {
const normalized = surface === 'read' ? 'read' : 'edit';
const rendered = normalized === 'read'
? state.readSurface
: state.editSurface;
if (!rendered?.root) return false;
context.runtimePort?.activate?.({
kind: currentAdapter().kind,
surface: normalized,
root: rendered.root,
adapter: currentAdapter(),
});
return true;
}
function invalidate(reason = 'manual') {
assertActive();
state.invalidationRevision += 1;
state.editRevision = -1;
state.readRevision = -1;
state.editDocumentId = null;
state.readDocumentId = null;
context.onInvalidate?.({
reason,
invalidationRevision: state.invalidationRevision,
});
}
function disposeSurface(surface) {
if (surface === 'edit') {
state.editSurface?.dispose?.();
state.editSurface = null;
state.editRevision = -1;
state.editDocumentId = null;
return;
}
if (surface === 'read') {
state.readSurface?.dispose?.();
state.readSurface = null;
state.readRevision = -1;
state.readDocumentId = null;
}
}
function disposeSurfaces() {
// Surface 重建、文档替换与 adapter 切换都只是运行时会话边界,
// 不能永久销毁由应用级组合根持有的 RuntimeController。
// 各 renderer surface 的 dispose() 会释放对应运行时;这里再显式
// 清理一次可覆盖 surface 尚未完整建立或已提前丢失的情况。
disposeSurface('edit');
disposeSurface('read');
context.runtimePort?.disposeSurface?.('edit');
context.runtimePort?.disposeSurface?.('read');
}
function status() {
return Object.freeze({
adapterKind: state.adapter?.kind || null,
mode: state.mode,
zoom: state.zoom,
editRevision: state.editRevision,
readRevision: state.readRevision,
invalidationRevision: state.invalidationRevision,
});
}
if (typeof documentPort.subscribe === 'function') {
disposers.push(documentPort.subscribe(
documentPort.EVENTS?.DOCUMENT_REPLACED || 'document-replaced',
() => {
disposeSurfaces();
invalidate('document-replaced');
}
));
disposers.push(documentPort.subscribe(
documentPort.EVENTS?.DOCUMENT_MUTATED || 'document-mutated',
(event) => {
if (!event.derived) invalidate('document-mutated');
}
));
}
function dispose() {
if (state.disposed) return;
disposeSurfaces();
// 只有 RenderCoordinator 自身退出时,才结束应用级运行时控制器。
context.runtimePort?.dispose?.();
state.adapter = null;
disposers.splice(0).forEach((disposeSubscription) => {
try {
disposeSubscription?.();
} catch {}
});
state.disposed = true;
}
return Object.freeze({
setAdapter,
currentAdapter,
setMode,
setZoom,
renderEdit,
renderRead,
renderCurrent,
activateRuntime,
invalidate,
disposeSurface,
disposeSurfaces,
status,
dispose,
});
}
window.ScriptoriumRenderCoordinator = Object.freeze({
createRenderCoordinator,
});
})();