67 lines
2.0 KiB
JavaScript
67 lines
2.0 KiB
JavaScript
/* canvas-v3.js —— Canvas 静态渲染迁移的特性开关(2026-12-01,Phase 0)
|
||
*
|
||
* 默认关闭;开启后只改变 data-canvas-v3 与后续渲染器选择,不影响数据格式。
|
||
* 目标架构见 docs/plans/canvas-v3-long-term.md。
|
||
*/
|
||
;(function (global) {
|
||
'use strict'
|
||
const KEY = 'iwb_canvas_v3'
|
||
const ATTR = 'canvasV3'
|
||
|
||
// src 内不直调 localStorage:统一走 IWBStorageLocal 门面(桌面端会落到资料目录底座)。
|
||
function store () {
|
||
try {
|
||
const S = global.IWBStorageLocal
|
||
return (S && typeof S.getStore === 'function') ? S.getStore() : null
|
||
} catch (e) { return null }
|
||
}
|
||
|
||
function isEnabled () {
|
||
try {
|
||
const s = store()
|
||
if (s && typeof s.rawGet === 'function') return s.rawGet(KEY, '0') === '1'
|
||
} catch (e) { /* ignore */ }
|
||
return false
|
||
}
|
||
|
||
function syncRoot (on) {
|
||
try {
|
||
if (typeof document !== 'undefined' && document.documentElement) {
|
||
document.documentElement.dataset[ATTR] = on ? 'on' : 'off'
|
||
}
|
||
} catch (e) { /* ignore */ }
|
||
}
|
||
|
||
function setEnabled (on) {
|
||
const next = !!on
|
||
try {
|
||
const s = store()
|
||
if (s && typeof s.rawSet === 'function') s.rawSet(KEY, next ? '1' : '0')
|
||
} catch (e) { /* ignore */ }
|
||
syncRoot(next)
|
||
try {
|
||
if (typeof window !== 'undefined' && typeof window.dispatchEvent === 'function' && typeof CustomEvent === 'function') {
|
||
window.dispatchEvent(new CustomEvent('iwb:canvas-v3', { detail: { enabled: next } }))
|
||
}
|
||
} catch (e) { /* ignore */ }
|
||
return next
|
||
}
|
||
|
||
function init () {
|
||
const on = isEnabled()
|
||
syncRoot(on)
|
||
return on
|
||
}
|
||
|
||
function stats () {
|
||
const on = isEnabled()
|
||
let dom = -1
|
||
try { dom = document.getElementsByTagName('*').length } catch (e) { /* ignore */ }
|
||
return { enabled: on, dom }
|
||
}
|
||
|
||
// P2 第一批:ref 图片节点 + prompt/textout 文本类节点。
|
||
const TYPES = ['ref', 'prompt', 'textout']
|
||
global.IWBCanvasV3 = { KEY, TYPES, isEnabled, setEnabled, init, stats }
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|