cortico 0.1.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/LICENSE +21 -0
- package/README.md +47 -0
- package/package.json +43 -0
- package/src/boot.ts +46 -0
- package/src/bot.ts +1342 -0
- package/src/config-file.ts +41 -0
- package/src/core/README.md +92 -0
- package/src/core/billing.ts +26 -0
- package/src/core/blobs.ts +106 -0
- package/src/core/bus.ts +311 -0
- package/src/core/config-schema.ts +201 -0
- package/src/core/config.ts +262 -0
- package/src/core/core.ts +699 -0
- package/src/core/cost.ts +244 -0
- package/src/core/event-store.ts +304 -0
- package/src/core/fork.ts +113 -0
- package/src/core/generation.ts +118 -0
- package/src/core/instance-lock.ts +215 -0
- package/src/core/ipc-logger.ts +105 -0
- package/src/core/language.ts +62 -0
- package/src/core/log-context.ts +38 -0
- package/src/core/loop.ts +1858 -0
- package/src/core/markers.ts +48 -0
- package/src/core/prefix.ts +121 -0
- package/src/core/run.ts +95 -0
- package/src/core/secrets.ts +18 -0
- package/src/core/session.ts +56 -0
- package/src/core/sessions.ts +261 -0
- package/src/core/state.ts +104 -0
- package/src/core/template.ts +72 -0
- package/src/core/timers.ts +129 -0
- package/src/core/tool-log.ts +135 -0
- package/src/core/transcript.ts +92 -0
- package/src/core/truncate.ts +121 -0
- package/src/core/types.ts +1140 -0
- package/src/core/usage-log.ts +79 -0
- package/src/core/util.ts +386 -0
- package/src/deploy-listing.ts +160 -0
- package/src/deploy.ts +162 -0
- package/src/extensions/README.md +72 -0
- package/src/extensions/dry-mount.ts +390 -0
- package/src/extensions/manifest.ts +153 -0
- package/src/extensions/runtime.ts +42 -0
- package/src/extensions.ts +519 -0
- package/src/launcher.ts +285 -0
- package/src/paths.ts +155 -0
- package/src/protocol/open-responses/LICENSE +201 -0
- package/src/protocol/open-responses/README.md +14 -0
- package/src/protocol/open-responses/context-helpers.ts +27 -0
- package/src/protocol/open-responses/context-log.ts +59 -0
- package/src/protocol/open-responses/context.ts +89 -0
- package/src/protocol/open-responses/generated.ts +115 -0
- package/src/protocol/open-responses/index.ts +33 -0
- package/src/protocol/open-responses/openapi.json +4230 -0
- package/src/protocol/open-responses/stream.ts +146 -0
- package/src/protocol/open-responses/tokens.ts +31 -0
- package/src/providers/README.md +86 -0
- package/src/providers/base.ts +111 -0
- package/src/providers/configuration.ts +97 -0
- package/src/providers/console/config.ts +21 -0
- package/src/providers/console/settings.ts +443 -0
- package/src/providers/console/strings.ts +74 -0
- package/src/providers/console/types.ts +11 -0
- package/src/providers/llamacpp/archive.ts +148 -0
- package/src/providers/llamacpp/catalog.ts +247 -0
- package/src/providers/llamacpp/config.ts +36 -0
- package/src/providers/llamacpp/console/client.ts +6 -0
- package/src/providers/llamacpp/console/models-panel.ts +120 -0
- package/src/providers/llamacpp/console/runtime-panel.ts +173 -0
- package/src/providers/llamacpp/console/server.ts +153 -0
- package/src/providers/llamacpp/index.ts +95 -0
- package/src/providers/llamacpp/native.ts +51 -0
- package/src/providers/llamacpp/options.ts +117 -0
- package/src/providers/llamacpp/runtime-store.ts +139 -0
- package/src/providers/llamacpp/runtime.ts +214 -0
- package/src/providers/llamacpp/server.ts +286 -0
- package/src/providers/llamacpp/strings.ts +230 -0
- package/src/providers/openai-responses-compat/config.ts +20 -0
- package/src/providers/openai-responses-compat/index.ts +89 -0
- package/src/providers/openai-responses-compat/native.ts +127 -0
- package/src/providers/openai-responses-compat/strings.ts +19 -0
- package/src/providers/pricebook.ts +75 -0
- package/src/providers/registry.ts +137 -0
- package/src/providers/strings.ts +71 -0
- package/src/providers/transport/chat.ts +156 -0
- package/src/providers/transport/errors.ts +48 -0
- package/src/providers/transport/history.ts +68 -0
- package/src/providers/transport/native-input.ts +83 -0
- package/src/providers/transport/native-types.ts +13 -0
- package/src/providers/transport/response-assembly.ts +191 -0
- package/src/providers/transport/response-http.ts +196 -0
- package/src/providers/transport/response-meters.ts +27 -0
- package/src/providers/transport/responses-input.ts +56 -0
- package/src/web/README.md +84 -0
- package/src/web/client/console-pages/builtins/llm-settings/panel.ts +625 -0
- package/src/web/client/console-pages/builtins/llm-settings/pricing-panel.ts +168 -0
- package/src/web/client/console-pages/builtins/llm-settings/strings.ts +213 -0
- package/src/web/client/console-pages/builtins.ts +10 -0
- package/src/web/client/console-pages/context.ts +135 -0
- package/src/web/client/console-pages/host.ts +633 -0
- package/src/web/client/console-pages/loader.ts +132 -0
- package/src/web/client/console-pages/strings.ts +90 -0
- package/src/web/client/console-pages/tools/strings.ts +49 -0
- package/src/web/client/console-pages/tools/view.ts +215 -0
- package/src/web/client/core/api.ts +200 -0
- package/src/web/client/core/language.ts +56 -0
- package/src/web/client/core/lifecycle.ts +132 -0
- package/src/web/client/core/router.ts +199 -0
- package/src/web/client/core/stream.ts +157 -0
- package/src/web/client/core/websocket.ts +82 -0
- package/src/web/client/features/appearance/index.ts +333 -0
- package/src/web/client/features/appearance/strings.ts +87 -0
- package/src/web/client/features/config/index.ts +11 -0
- package/src/web/client/features/config/strings.ts +41 -0
- package/src/web/client/features/config/view.ts +428 -0
- package/src/web/client/features/core/events.ts +197 -0
- package/src/web/client/features/core/index.ts +254 -0
- package/src/web/client/features/core/run.ts +56 -0
- package/src/web/client/features/core/runlog.ts +214 -0
- package/src/web/client/features/core/sessions.ts +53 -0
- package/src/web/client/features/core/strings.ts +157 -0
- package/src/web/client/features/extensions/index.ts +422 -0
- package/src/web/client/features/extensions/strings.ts +164 -0
- package/src/web/client/features/feature.ts +93 -0
- package/src/web/client/features/live/context.ts +280 -0
- package/src/web/client/features/live/fork.ts +156 -0
- package/src/web/client/features/live/index.ts +392 -0
- package/src/web/client/features/live/onboarding.ts +113 -0
- package/src/web/client/features/live/protocol.ts +122 -0
- package/src/web/client/features/live/sessions.ts +71 -0
- package/src/web/client/features/live/status.ts +68 -0
- package/src/web/client/features/live/strings.ts +252 -0
- package/src/web/client/features/live/timeline.ts +590 -0
- package/src/web/client/features/prompts/editor.ts +292 -0
- package/src/web/client/features/prompts/index.ts +346 -0
- package/src/web/client/features/prompts/strings.ts +136 -0
- package/src/web/client/features/prompts/view.ts +256 -0
- package/src/web/client/features/providers/index.ts +100 -0
- package/src/web/client/features/providers/strings.ts +23 -0
- package/src/web/client/features/settings/general.ts +28 -0
- package/src/web/client/features/settings/index.ts +82 -0
- package/src/web/client/features/settings/strings.ts +33 -0
- package/src/web/client/features/storage/index.ts +1 -0
- package/src/web/client/features/storage/strings.ts +45 -0
- package/src/web/client/features/storage/view.ts +154 -0
- package/src/web/client/features/usage/chart.ts +486 -0
- package/src/web/client/features/usage/index.ts +407 -0
- package/src/web/client/features/usage/labels.ts +46 -0
- package/src/web/client/features/usage/range.ts +36 -0
- package/src/web/client/features/usage/state.ts +37 -0
- package/src/web/client/features/usage/strings.ts +191 -0
- package/src/web/client/features/usage/tooltip.ts +86 -0
- package/src/web/client/features/usage/types.ts +96 -0
- package/src/web/client/features/worlds/index.ts +424 -0
- package/src/web/client/features/worlds/strings.ts +121 -0
- package/src/web/client/main.ts +273 -0
- package/src/web/client/shell/avatar.ts +233 -0
- package/src/web/client/shell/index.ts +549 -0
- package/src/web/client/shell/strings.ts +89 -0
- package/src/web/client/theme/handoff.ts +43 -0
- package/src/web/client/theme/palette.ts +109 -0
- package/src/web/client/theme/registry.ts +130 -0
- package/src/web/client/theme/storage.ts +72 -0
- package/src/web/client/theme/strings.ts +123 -0
- package/src/web/client/theme/studio.ts +366 -0
- package/src/web/client/ui/actions.ts +109 -0
- package/src/web/client/ui/data.ts +185 -0
- package/src/web/client/ui/dom.ts +23 -0
- package/src/web/client/ui/fields.ts +191 -0
- package/src/web/client/ui/format.ts +75 -0
- package/src/web/client/ui/icons.ts +183 -0
- package/src/web/client/ui/images.ts +91 -0
- package/src/web/client/ui/index.ts +89 -0
- package/src/web/client/ui/lamp.ts +125 -0
- package/src/web/client/ui/log.ts +98 -0
- package/src/web/client/ui/overlay.ts +184 -0
- package/src/web/client/ui/page.ts +8 -0
- package/src/web/client/ui/prompt-input.tsx +311 -0
- package/src/web/client/ui/sheet.ts +131 -0
- package/src/web/client/ui/strings.ts +71 -0
- package/src/web/console-pages.ts +367 -0
- package/src/web/files.ts +107 -0
- package/src/web/path-picker.ts +296 -0
- package/src/web/public/index.html +17 -0
- package/src/web/public/styles.css +1715 -0
- package/src/web/server.ts +1883 -0
- package/src/web/shared/client-panel.ts +603 -0
- package/src/web/shared/console-protocol.ts +560 -0
- package/src/web/shared/css.d.ts +1 -0
- package/src/web/shared/path-picker.ts +23 -0
- package/src/web/shared/theme.ts +256 -0
- package/src/web/theme-store.ts +37 -0
- package/src/world.ts +449 -0
- package/src/worlds/bilibili/ENV_PROMPT.md +5 -0
- package/src/worlds/bilibili/README.md +176 -0
- package/src/worlds/bilibili/audience-admission.ts +995 -0
- package/src/worlds/bilibili/client.ts +528 -0
- package/src/worlds/bilibili/coalescing-buffer.ts +125 -0
- package/src/worlds/bilibili/config.ts +185 -0
- package/src/worlds/bilibili/console/client.ts +202 -0
- package/src/worlds/bilibili/definition.ts +35 -0
- package/src/worlds/bilibili/gift-frame.ts +131 -0
- package/src/worlds/bilibili/normalize.ts +452 -0
- package/src/worlds/bilibili/overlay/announcement.ts +76 -0
- package/src/worlds/bilibili/overlay/assets.ts +82 -0
- package/src/worlds/bilibili/overlay/model.ts +494 -0
- package/src/worlds/bilibili/overlay/project.ts +202 -0
- package/src/worlds/bilibili/overlay/server.ts +286 -0
- package/src/worlds/bilibili/overlay/types.ts +217 -0
- package/src/worlds/bilibili/overlay/web/app.js +690 -0
- package/src/worlds/bilibili/overlay/web/editor.css +297 -0
- package/src/worlds/bilibili/overlay/web/editor.html +102 -0
- package/src/worlds/bilibili/overlay/web/editor.js +2116 -0
- package/src/worlds/bilibili/overlay/web/overlay.html +13 -0
- package/src/worlds/bilibili/overlay/web/styles.css +258 -0
- package/src/worlds/bilibili/protobuf.ts +123 -0
- package/src/worlds/bilibili/wire.ts +94 -0
- package/src/worlds/bilibili/world.ts +1389 -0
- package/src/worlds/console-fixture/ENV_PROMPT.md +1 -0
- package/src/worlds/console-fixture/console/client.ts +72 -0
- package/src/worlds/console-fixture/world.ts +73 -0
- package/src/worlds/index.ts +15 -0
- package/src/worlds/minecraft/ENV_PROMPT.md +32 -0
- package/src/worlds/minecraft/ENV_PROMPT_CAMERA.md +2 -0
- package/src/worlds/minecraft/LICENSE-mineflayer-pathfinder.txt +21 -0
- package/src/worlds/minecraft/README.md +917 -0
- package/src/worlds/minecraft/blueprint-plan.ts +915 -0
- package/src/worlds/minecraft/blueprint-registry.ts +566 -0
- package/src/worlds/minecraft/blueprint-repair.ts +236 -0
- package/src/worlds/minecraft/blueprint-resource.ts +272 -0
- package/src/worlds/minecraft/blueprint.ts +492 -0
- package/src/worlds/minecraft/body-lease.ts +340 -0
- package/src/worlds/minecraft/bridge.ts +749 -0
- package/src/worlds/minecraft/check.ts +651 -0
- package/src/worlds/minecraft/chests.ts +292 -0
- package/src/worlds/minecraft/client-launch.ts +272 -0
- package/src/worlds/minecraft/client-options.ts +85 -0
- package/src/worlds/minecraft/client-skins.ts +164 -0
- package/src/worlds/minecraft/client-window.ps1 +91 -0
- package/src/worlds/minecraft/client.ts +423 -0
- package/src/worlds/minecraft/combat-context.ts +109 -0
- package/src/worlds/minecraft/combat.ts +1552 -0
- package/src/worlds/minecraft/config.ts +499 -0
- package/src/worlds/minecraft/console/access.ts +218 -0
- package/src/worlds/minecraft/console/client.ts +244 -0
- package/src/worlds/minecraft/console/log.ts +110 -0
- package/src/worlds/minecraft/console/mount.ts +420 -0
- package/src/worlds/minecraft/console/skin.ts +257 -0
- package/src/worlds/minecraft/console/style.css +154 -0
- package/src/worlds/minecraft/console/world.ts +449 -0
- package/src/worlds/minecraft/deaths.ts +135 -0
- package/src/worlds/minecraft/definition.ts +18 -0
- package/src/worlds/minecraft/engine-child.ts +274 -0
- package/src/worlds/minecraft/engine-ipc.ts +98 -0
- package/src/worlds/minecraft/entity-facts.ts +132 -0
- package/src/worlds/minecraft/escape.ts +407 -0
- package/src/worlds/minecraft/executor.ts +14494 -0
- package/src/worlds/minecraft/explored.ts +156 -0
- package/src/worlds/minecraft/geometry.ts +205 -0
- package/src/worlds/minecraft/goal-plan.ts +647 -0
- package/src/worlds/minecraft/item-break.ts +126 -0
- package/src/worlds/minecraft/item-facts.ts +146 -0
- package/src/worlds/minecraft/item-pick.ts +67 -0
- package/src/worlds/minecraft/level-dat.ts +176 -0
- package/src/worlds/minecraft/log.ts +132 -0
- package/src/worlds/minecraft/mineflayer-fixes.ts +1180 -0
- package/src/worlds/minecraft/names.ts +377 -0
- package/src/worlds/minecraft/pathfinder-lib.d.ts +13 -0
- package/src/worlds/minecraft/pathfinder-perf.ts +796 -0
- package/src/worlds/minecraft/piglin.ts +49 -0
- package/src/worlds/minecraft/policy.ts +547 -0
- package/src/worlds/minecraft/precheck.ts +655 -0
- package/src/worlds/minecraft/proxy.ts +445 -0
- package/src/worlds/minecraft/ranged.ts +635 -0
- package/src/worlds/minecraft/readouts.ts +102 -0
- package/src/worlds/minecraft/round.ts +26 -0
- package/src/worlds/minecraft/search-observation.ts +92 -0
- package/src/worlds/minecraft/server-config.ts +467 -0
- package/src/worlds/minecraft/server.ts +658 -0
- package/src/worlds/minecraft/show.ts +84 -0
- package/src/worlds/minecraft/skills.ts +1763 -0
- package/src/worlds/minecraft/terrain.ts +1735 -0
- package/src/worlds/minecraft/window.ts +96 -0
- package/src/worlds/minecraft/works.ts +179 -0
- package/src/worlds/minecraft/world.ts +6438 -0
- package/src/worlds/qq/ENV_PROMPT.md +5 -0
- package/src/worlds/qq/config.ts +108 -0
- package/src/worlds/qq/console/client.ts +126 -0
- package/src/worlds/qq/console/events.ts +155 -0
- package/src/worlds/qq/console/gate.ts +232 -0
- package/src/worlds/qq/console/roster.ts +203 -0
- package/src/worlds/qq/console/style.css +65 -0
- package/src/worlds/qq/conversation.ts +28 -0
- package/src/worlds/qq/definition.ts +62 -0
- package/src/worlds/qq/driver.ts +375 -0
- package/src/worlds/qq/history-tools.ts +209 -0
- package/src/worlds/qq/normalize.ts +323 -0
- package/src/worlds/qq/qface-map.ts +282 -0
- package/src/worlds/qq/vision-prompt.ts +27 -0
- package/src/worlds/qq/vision.ts +642 -0
- package/src/worlds/qq/vlm.ts +142 -0
- package/src/worlds/qq/world.ts +1503 -0
- package/src/worlds/terminal/ENV_PROMPT.md +11 -0
- package/src/worlds/terminal/config.ts +9 -0
- package/src/worlds/terminal/definition.ts +14 -0
- package/src/worlds/terminal/world.ts +700 -0
- package/src/worlds/websearch/ENV_PROMPT.md +1 -0
- package/src/worlds/websearch/brave-client.ts +261 -0
- package/src/worlds/websearch/config.ts +75 -0
- package/src/worlds/websearch/definition.ts +20 -0
- package/src/worlds/websearch/world.ts +171 -0
|
@@ -0,0 +1,690 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const stage = document.getElementById('stage');
|
|
5
|
+
const query = new URLSearchParams(location.search);
|
|
6
|
+
const editorPreview = query.has('preview');
|
|
7
|
+
if (query.has('bg') || query.has('preview')) document.body.classList.add('preview');
|
|
8
|
+
|
|
9
|
+
let design = null;
|
|
10
|
+
let builtinStyles = [];
|
|
11
|
+
let announcement = { text: '' };
|
|
12
|
+
let hasEditorDraft = false;
|
|
13
|
+
const feeds = new Map();
|
|
14
|
+
const horizontalLanes = new Map();
|
|
15
|
+
const agentTypings = new Map();
|
|
16
|
+
const mockTimers = new Set();
|
|
17
|
+
let typingToken = 0;
|
|
18
|
+
let mockToken = 0;
|
|
19
|
+
let activeMockComponentId = '';
|
|
20
|
+
|
|
21
|
+
if (editorPreview) document.body.dataset.wallMode = 'dark';
|
|
22
|
+
|
|
23
|
+
const source = new EventSource('/stream');
|
|
24
|
+
source.onmessage = (message) => {
|
|
25
|
+
let packet;
|
|
26
|
+
try { packet = JSON.parse(message.data); } catch { return; }
|
|
27
|
+
if (packet.type === 'snapshot' || packet.type === 'state') {
|
|
28
|
+
if (editorPreview && hasEditorDraft) {
|
|
29
|
+
builtinStyles = Array.isArray(packet.builtinStyles) ? packet.builtinStyles : builtinStyles;
|
|
30
|
+
announcement = packet.agentAnnouncement || announcement;
|
|
31
|
+
updateAnnouncement();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
applyState(packet);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (packet.type === 'announcement' && packet.agentAnnouncement) {
|
|
38
|
+
announcement = packet.agentAnnouncement;
|
|
39
|
+
updateAnnouncement();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (packet.type === 'audience' && packet.event) renderAudience(packet.event);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
addEventListener('message', (message) => {
|
|
46
|
+
if (!editorPreview) return;
|
|
47
|
+
if (message.origin !== location.origin || message.source !== parent) return;
|
|
48
|
+
const packet = message.data;
|
|
49
|
+
if (!packet || typeof packet !== 'object') return;
|
|
50
|
+
if (packet.type === 'overlay-editor-preview' && packet.design) {
|
|
51
|
+
hasEditorDraft = true;
|
|
52
|
+
design = packet.design;
|
|
53
|
+
builtinStyles = Array.isArray(packet.builtinStyles) ? packet.builtinStyles : builtinStyles;
|
|
54
|
+
if (packet.wallMode === 'light' || packet.wallMode === 'dark') {
|
|
55
|
+
document.body.dataset.wallMode = packet.wallMode;
|
|
56
|
+
}
|
|
57
|
+
activeMockComponentId = typeof packet.mockComponentId === 'string' ? packet.mockComponentId : '';
|
|
58
|
+
renderScene();
|
|
59
|
+
if (Array.isArray(packet.events)) packet.events.forEach(renderAudience);
|
|
60
|
+
if (activeMockComponentId) runComponentMock(activeMockComponentId);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (packet.type === 'overlay-editor-audience' && packet.event) renderAudience(packet.event);
|
|
64
|
+
});
|
|
65
|
+
if (editorPreview) parent.postMessage({ type: 'overlay-editor-ready' }, location.origin);
|
|
66
|
+
|
|
67
|
+
addEventListener('resize', fitStage);
|
|
68
|
+
|
|
69
|
+
function applyState(packet) {
|
|
70
|
+
design = packet.design || design;
|
|
71
|
+
builtinStyles = Array.isArray(packet.builtinStyles) ? packet.builtinStyles : builtinStyles;
|
|
72
|
+
announcement = packet.agentAnnouncement || announcement;
|
|
73
|
+
renderScene();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function renderScene() {
|
|
77
|
+
if (!design) return;
|
|
78
|
+
cancelAgentTypings();
|
|
79
|
+
cancelMockTimers();
|
|
80
|
+
for (const track of stage.querySelectorAll('.scroll-track')) {
|
|
81
|
+
for (const animation of track.getAnimations()) animation.cancel();
|
|
82
|
+
}
|
|
83
|
+
feeds.clear();
|
|
84
|
+
horizontalLanes.clear();
|
|
85
|
+
stage.replaceChildren();
|
|
86
|
+
stage.style.width = `${design.canvas.width}px`;
|
|
87
|
+
stage.style.height = `${design.canvas.height}px`;
|
|
88
|
+
const components = new Map(design.components.map((item) => [item.id, item]));
|
|
89
|
+
const placements = [...design.placements].filter((item) => item.visible).sort((a, b) => a.z - b.z);
|
|
90
|
+
for (const placement of placements) {
|
|
91
|
+
const component = components.get(placement.componentId);
|
|
92
|
+
if (!component) continue;
|
|
93
|
+
renderPlacement(component, placement);
|
|
94
|
+
}
|
|
95
|
+
fitStage();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function renderPlacement(component, placement, isMock = false) {
|
|
99
|
+
const element = document.createElement('section');
|
|
100
|
+
element.className = `overlay-component ${component.kind}`;
|
|
101
|
+
element.dataset.componentId = component.id;
|
|
102
|
+
element.dataset.placementId = placement.id;
|
|
103
|
+
if (isMock) element.classList.add('editor-mock-placement');
|
|
104
|
+
Object.assign(element.style, {
|
|
105
|
+
left: `${placement.x}px`,
|
|
106
|
+
top: `${placement.y}px`,
|
|
107
|
+
width: `${placement.width}px`,
|
|
108
|
+
height: `${placement.height}px`,
|
|
109
|
+
zIndex: String(placement.z),
|
|
110
|
+
});
|
|
111
|
+
applyPanelStyle(element, styleById(component.styleId));
|
|
112
|
+
const content = componentFrame(element, component);
|
|
113
|
+
fillComponent(element, content, component, placement.id);
|
|
114
|
+
stage.appendChild(element);
|
|
115
|
+
return element;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function componentFrame(element, component) {
|
|
119
|
+
const content = document.createElement('div');
|
|
120
|
+
content.className = 'component-content';
|
|
121
|
+
const title = component.title;
|
|
122
|
+
if (!title || !title.text) {
|
|
123
|
+
element.appendChild(content);
|
|
124
|
+
return content;
|
|
125
|
+
}
|
|
126
|
+
element.classList.add('has-title', `title-${title.position}`);
|
|
127
|
+
const band = document.createElement('div');
|
|
128
|
+
band.className = `component-title align-${title.align}`;
|
|
129
|
+
band.textContent = title.text;
|
|
130
|
+
applyTextStyle(band, title.style);
|
|
131
|
+
if (title.position === 'bottom' || title.position === 'right') element.append(content, band);
|
|
132
|
+
else element.append(band, content);
|
|
133
|
+
return content;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function fillComponent(element, content, component, placementId) {
|
|
137
|
+
const style = styleById(component.styleId);
|
|
138
|
+
if (component.kind === 'danmaku') {
|
|
139
|
+
const feed = document.createElement('div');
|
|
140
|
+
feed.className = `danmaku-feed ${component.axis}`;
|
|
141
|
+
if (component.axis === 'vertical') feed.style.rowGap = `${component.gap}px`;
|
|
142
|
+
applyEdgeFade(feed, component.axis, component.edgeFadePx);
|
|
143
|
+
content.appendChild(feed);
|
|
144
|
+
const groupFontSizes = Array.isArray(design.groups)
|
|
145
|
+
? design.groups.flatMap((group) => [group.username && group.username.fontSize, group.body && group.body.fontSize])
|
|
146
|
+
: [];
|
|
147
|
+
const maxFontSize = Math.max(style.username.fontSize, style.body.fontSize, ...groupFontSizes.filter(Number.isFinite));
|
|
148
|
+
feeds.set(placementId, {
|
|
149
|
+
key: placementId,
|
|
150
|
+
element: feed,
|
|
151
|
+
component,
|
|
152
|
+
style,
|
|
153
|
+
laneHeight: maxFontSize * 1.5 + component.gap,
|
|
154
|
+
});
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (component.kind === 'fixed-notice') {
|
|
158
|
+
content.appendChild(textElement(component.text, style.body));
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (component.kind === 'agent-notice') {
|
|
162
|
+
renderAgentNotice(element, content, component, placementId, style.body);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (component.kind === 'image') {
|
|
166
|
+
element.classList.add('image-component');
|
|
167
|
+
const image = document.createElement('img');
|
|
168
|
+
image.alt = '';
|
|
169
|
+
image.referrerPolicy = 'no-referrer';
|
|
170
|
+
const source = componentImageSource(component);
|
|
171
|
+
if (source) image.src = source;
|
|
172
|
+
image.style.objectFit = component.fit;
|
|
173
|
+
image.style.opacity = String(component.opacity);
|
|
174
|
+
content.appendChild(image);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (component.kind === 'scroll-notice') {
|
|
178
|
+
const viewport = document.createElement('div');
|
|
179
|
+
viewport.className = 'scroll-viewport';
|
|
180
|
+
applyEdgeFade(viewport, component.axis, component.edgeFadePx);
|
|
181
|
+
const track = document.createElement('div');
|
|
182
|
+
track.className = `scroll-track ${component.axis}`;
|
|
183
|
+
viewport.appendChild(track);
|
|
184
|
+
content.appendChild(viewport);
|
|
185
|
+
restartNotice(track, viewport, component, style.body);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function renderAudience(event) {
|
|
190
|
+
for (const feed of feeds.values()) {
|
|
191
|
+
if (!acceptsAudience(feed.component, event)) continue;
|
|
192
|
+
renderAudienceInto(feed, event);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function acceptsAudience(component, event) {
|
|
197
|
+
return component.admission === 'all' || component.admission === event.eventKind;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function renderAudienceInto(feed, event) {
|
|
201
|
+
const item = audienceElement(event, feed);
|
|
202
|
+
if (feed.component.axis === 'vertical') appendVertical(feed, item);
|
|
203
|
+
else launchHorizontal(feed, item);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function updateAnnouncement() {
|
|
207
|
+
if (!design) return;
|
|
208
|
+
const components = new Map(design.components.map((item) => [item.id, item]));
|
|
209
|
+
for (const element of stage.querySelectorAll('.agent-notice')) {
|
|
210
|
+
const component = components.get(element.dataset.componentId);
|
|
211
|
+
if (!component || component.kind !== 'agent-notice') continue;
|
|
212
|
+
if (component.id === activeMockComponentId) continue;
|
|
213
|
+
const content = element.querySelector('.component-content');
|
|
214
|
+
if (!content) continue;
|
|
215
|
+
renderAgentNotice(
|
|
216
|
+
element,
|
|
217
|
+
content,
|
|
218
|
+
component,
|
|
219
|
+
element.dataset.placementId,
|
|
220
|
+
styleById(component.styleId).body,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function audienceElement(event, feed) {
|
|
226
|
+
const item = document.createElement('div');
|
|
227
|
+
item.className = 'audience-item';
|
|
228
|
+
item.dataset.eventKind = event.eventKind;
|
|
229
|
+
const group = Array.isArray(design.groups) ? design.groups.find((entry) => entry.id === event.groupId) : null;
|
|
230
|
+
const usernameStyle = Object.assign({}, feed.style.username, group ? group.username : null);
|
|
231
|
+
const bodyStyle = Object.assign({}, feed.style.body, group ? group.body : null);
|
|
232
|
+
if (feed.component.showAvatar) item.appendChild(avatarElement(event, usernameStyle.color));
|
|
233
|
+
const username = document.createElement('span');
|
|
234
|
+
username.className = 'audience-name';
|
|
235
|
+
username.textContent = truncateDisplay(event.username, feed.component.usernameMaxChars);
|
|
236
|
+
applyTextStyle(username, usernameStyle);
|
|
237
|
+
const body = document.createElement('span');
|
|
238
|
+
body.className = 'audience-body';
|
|
239
|
+
body.textContent = truncateDisplay(event.body, feed.component.bodyMaxChars);
|
|
240
|
+
applyTextStyle(body, bodyStyle);
|
|
241
|
+
item.append(username, body);
|
|
242
|
+
return item;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function truncateDisplay(value, maxChars) {
|
|
246
|
+
const characters = Array.from(String(value ?? ''));
|
|
247
|
+
const limit = Math.max(0, Math.floor(Number(maxChars) || 0));
|
|
248
|
+
if (!limit || characters.length <= limit) return characters.join('');
|
|
249
|
+
if (limit === 1) return '…';
|
|
250
|
+
return `${characters.slice(0, limit - 1).join('')}…`;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function avatarElement(event, color) {
|
|
254
|
+
if (event.avatarUrl) {
|
|
255
|
+
const image = document.createElement('img');
|
|
256
|
+
image.className = 'audience-avatar';
|
|
257
|
+
image.alt = '';
|
|
258
|
+
image.referrerPolicy = 'no-referrer';
|
|
259
|
+
image.src = event.avatarUrl;
|
|
260
|
+
image.addEventListener('error', () => image.replaceWith(avatarFallback(event.username, color)), { once: true });
|
|
261
|
+
return image;
|
|
262
|
+
}
|
|
263
|
+
return avatarFallback(event.username, color);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function avatarFallback(username, color) {
|
|
267
|
+
const fallback = document.createElement('span');
|
|
268
|
+
fallback.className = 'audience-avatar fallback';
|
|
269
|
+
fallback.textContent = Array.from(username || '?')[0] || '?';
|
|
270
|
+
fallback.style.color = color;
|
|
271
|
+
return fallback;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function renderAgentNotice(element, content, component, placementId, style) {
|
|
275
|
+
const hidden = !announcement.text && component.hideWhenEmpty;
|
|
276
|
+
element.hidden = hidden;
|
|
277
|
+
if (hidden) {
|
|
278
|
+
stopAgentTyping(placementId);
|
|
279
|
+
content.replaceChildren();
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
renderAgentText(content, announcement.text || component.emptyText, style, component.typingMs, placementId);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function renderAgentText(content, value, style, interval, placementId) {
|
|
286
|
+
stopAgentTyping(placementId);
|
|
287
|
+
content.replaceChildren();
|
|
288
|
+
const element = textElement('', style);
|
|
289
|
+
element.classList.add('agent-typing');
|
|
290
|
+
content.appendChild(element);
|
|
291
|
+
const characters = Array.from(value);
|
|
292
|
+
const delay = Math.max(0, Number(interval) || 0);
|
|
293
|
+
if (!delay || characters.length === 0) {
|
|
294
|
+
element.textContent = value;
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const text = document.createTextNode('');
|
|
299
|
+
const cursor = document.createElement('span');
|
|
300
|
+
cursor.className = 'agent-typing-cursor';
|
|
301
|
+
cursor.setAttribute('aria-hidden', 'true');
|
|
302
|
+
element.append(text, cursor);
|
|
303
|
+
const token = ++typingToken;
|
|
304
|
+
const state = { token, timer: 0 };
|
|
305
|
+
agentTypings.set(placementId, state);
|
|
306
|
+
let index = 0;
|
|
307
|
+
const typeNext = () => {
|
|
308
|
+
const current = agentTypings.get(placementId);
|
|
309
|
+
if (!current || current.token !== token) return;
|
|
310
|
+
text.data += characters[index];
|
|
311
|
+
index += 1;
|
|
312
|
+
if (index < characters.length) {
|
|
313
|
+
current.timer = setTimeout(typeNext, delay);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
cursor.remove();
|
|
317
|
+
agentTypings.delete(placementId);
|
|
318
|
+
};
|
|
319
|
+
state.timer = setTimeout(typeNext, delay);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function stopAgentTyping(placementId) {
|
|
323
|
+
const state = agentTypings.get(placementId);
|
|
324
|
+
if (!state) return;
|
|
325
|
+
clearTimeout(state.timer);
|
|
326
|
+
agentTypings.delete(placementId);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function cancelAgentTypings() {
|
|
330
|
+
typingToken += 1;
|
|
331
|
+
for (const state of agentTypings.values()) clearTimeout(state.timer);
|
|
332
|
+
agentTypings.clear();
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function runComponentMock(componentId) {
|
|
336
|
+
if (!design) return;
|
|
337
|
+
cancelMockTimers();
|
|
338
|
+
const component = design.components.find((item) => item.id === componentId);
|
|
339
|
+
if (!component) return;
|
|
340
|
+
|
|
341
|
+
let elements = Array.from(stage.children).filter((element) => element.dataset.componentId === componentId);
|
|
342
|
+
if (elements.length === 0) {
|
|
343
|
+
const placement = mockPlacement(component);
|
|
344
|
+
elements = [renderPlacement(component, placement, true)];
|
|
345
|
+
fitStage();
|
|
346
|
+
}
|
|
347
|
+
for (const element of elements) element.hidden = false;
|
|
348
|
+
|
|
349
|
+
const token = mockToken;
|
|
350
|
+
if (component.kind === 'danmaku') {
|
|
351
|
+
const events = [
|
|
352
|
+
mockAudience('danmaku', '星野', '这是一条带头像的测试弹幕', '#59c8ff', '星'),
|
|
353
|
+
mockAudience('gift', '海盐汽水', '赠送 小电视 × 1', '#ff9c75', '礼'),
|
|
354
|
+
mockAudience('danmaku', '这是一个为了测试截断而故意写得特别特别特别特别长的用户名', '用户名显示上限会在这里生效', '#9a8cff', '长'),
|
|
355
|
+
mockAudience('danmaku', '夜航员', '这是一条为了验证弹幕内容最大显示长度而故意写得特别特别长的测试弹幕,它会继续补充许多文字,确保默认上限和用户手动设置的更长上限都能被清楚地观察到。这段内容还会继续延伸,模拟真实直播里观众一次发来超长说明、连续提问和补充上下文的情况。', '#54d6a1', '航'),
|
|
356
|
+
mockAudience('gift', '舰长观众', '赠送 舰长 × 1,感谢支持', '#f2c14e', '舰'),
|
|
357
|
+
].filter((event) => acceptsAudience(component, event));
|
|
358
|
+
const delays = [520, 760, 620, 880, 680];
|
|
359
|
+
let index = 0;
|
|
360
|
+
const emitNext = () => {
|
|
361
|
+
const event = events[index % events.length];
|
|
362
|
+
for (const element of elements) {
|
|
363
|
+
const feed = feeds.get(element.dataset.placementId);
|
|
364
|
+
if (feed) renderAudienceInto(feed, event);
|
|
365
|
+
}
|
|
366
|
+
const delay = delays[index % delays.length];
|
|
367
|
+
index += 1;
|
|
368
|
+
scheduleMock(token, delay, emitNext);
|
|
369
|
+
};
|
|
370
|
+
emitNext();
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (component.kind === 'scroll-notice') {
|
|
375
|
+
for (const element of elements) {
|
|
376
|
+
const viewport = element.querySelector('.scroll-viewport');
|
|
377
|
+
const track = element.querySelector('.scroll-track');
|
|
378
|
+
if (!viewport || !track) continue;
|
|
379
|
+
restartNotice(
|
|
380
|
+
track,
|
|
381
|
+
viewport,
|
|
382
|
+
component,
|
|
383
|
+
styleById(component.styleId).body,
|
|
384
|
+
'欢迎来到直播间\n今晚八点开始联机挑战\n关注主播,不错过开播通知',
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (component.kind === 'fixed-notice') {
|
|
391
|
+
for (const element of elements) {
|
|
392
|
+
const content = element.querySelector('.component-content');
|
|
393
|
+
if (content) content.replaceChildren(textElement('Mock · 这是一条固定公告测试内容', styleById(component.styleId).body));
|
|
394
|
+
}
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (component.kind === 'agent-notice') {
|
|
399
|
+
for (const element of elements) {
|
|
400
|
+
const content = element.querySelector('.component-content');
|
|
401
|
+
if (content) renderAgentText(
|
|
402
|
+
content,
|
|
403
|
+
'今晚直播安排:先读弹幕,再一起看看新功能。',
|
|
404
|
+
styleById(component.styleId).body,
|
|
405
|
+
component.typingMs > 0 ? component.typingMs : 42,
|
|
406
|
+
element.dataset.placementId,
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (component.kind === 'image' && !componentImageSource(component)) {
|
|
413
|
+
for (const element of elements) {
|
|
414
|
+
const image = element.querySelector('.component-content img');
|
|
415
|
+
if (image) image.src = mockImageSource();
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function mockPlacement(component) {
|
|
421
|
+
const preset = component.kind === 'danmaku'
|
|
422
|
+
? { width: 720, height: 320 }
|
|
423
|
+
: component.kind === 'image'
|
|
424
|
+
? { width: 520, height: 300 }
|
|
425
|
+
: { width: 820, height: 140 };
|
|
426
|
+
const width = Math.min(preset.width, design.canvas.width);
|
|
427
|
+
const height = Math.min(preset.height, design.canvas.height);
|
|
428
|
+
const z = Math.max(0, ...design.placements.map((placement) => placement.z)) + 1;
|
|
429
|
+
return {
|
|
430
|
+
id: '__editor_mock__',
|
|
431
|
+
componentId: component.id,
|
|
432
|
+
x: (design.canvas.width - width) / 2,
|
|
433
|
+
y: (design.canvas.height - height) / 2,
|
|
434
|
+
width,
|
|
435
|
+
height,
|
|
436
|
+
z,
|
|
437
|
+
visible: true,
|
|
438
|
+
locked: false,
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function mockAudience(eventKind, username, body, color, avatarLabel) {
|
|
443
|
+
return {
|
|
444
|
+
eventKind,
|
|
445
|
+
username,
|
|
446
|
+
body,
|
|
447
|
+
avatarUrl: mockAvatarSource(color, avatarLabel),
|
|
448
|
+
facts: { eventKind },
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function mockAvatarSource(color, label) {
|
|
453
|
+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96"><rect width="96" height="96" rx="48" fill="${color}"/><circle cx="48" cy="37" r="18" fill="#fff" fill-opacity=".9"/><path d="M17 91c3-23 15-35 31-35s28 12 31 35" fill="#fff" fill-opacity=".9"/><text x="48" y="88" text-anchor="middle" font-size="15" font-family="sans-serif" fill="#18202e">${label}</text></svg>`;
|
|
454
|
+
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function mockImageSource() {
|
|
458
|
+
const svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 520 300"><defs><linearGradient id="g" x2="1" y2="1"><stop stop-color="#182536"/><stop offset="1" stop-color="#3c6781"/></linearGradient></defs><rect width="520" height="300" fill="url(#g)"/><circle cx="260" cy="122" r="46" fill="#fff" fill-opacity=".14"/><path d="m225 132 23-25 22 20 19-16 31 38h-95z" fill="#fff" fill-opacity=".8"/><text x="260" y="215" text-anchor="middle" font-size="24" font-family="sans-serif" fill="#fff">IMAGE MOCK</text></svg>';
|
|
459
|
+
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function scheduleMock(token, delay, action) {
|
|
463
|
+
const timer = setTimeout(() => {
|
|
464
|
+
mockTimers.delete(timer);
|
|
465
|
+
if (token === mockToken) action();
|
|
466
|
+
}, delay);
|
|
467
|
+
mockTimers.add(timer);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function cancelMockTimers() {
|
|
471
|
+
mockToken += 1;
|
|
472
|
+
for (const timer of mockTimers) clearTimeout(timer);
|
|
473
|
+
mockTimers.clear();
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function appendVertical(feed, item) {
|
|
477
|
+
const previous = new Map(Array.from(feed.element.children, (child) => [child, child.getBoundingClientRect().top]));
|
|
478
|
+
feed.element.appendChild(item);
|
|
479
|
+
const entryDuration = Math.max(100, item.getBoundingClientRect().height / feed.component.speed * 1000);
|
|
480
|
+
item.style.animationDuration = `${entryDuration}ms`;
|
|
481
|
+
while (feed.element.children.length > feed.component.maxItems) feed.element.firstElementChild.remove();
|
|
482
|
+
requestAnimationFrame(() => {
|
|
483
|
+
for (const child of feed.element.children) {
|
|
484
|
+
if (child === item) continue;
|
|
485
|
+
const oldTop = previous.get(child);
|
|
486
|
+
if (oldTop === undefined) continue;
|
|
487
|
+
const delta = oldTop - child.getBoundingClientRect().top;
|
|
488
|
+
if (Math.abs(delta) > .5) {
|
|
489
|
+
child.animate([{ transform: `translateY(${delta}px)` }, { transform: 'translateY(0)' }], {
|
|
490
|
+
duration: Math.max(100, Math.abs(delta) / feed.component.speed * 1000),
|
|
491
|
+
easing: 'ease-out',
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function launchHorizontal(feed, item) {
|
|
499
|
+
const lineHeight = Math.max(1, feed.laneHeight);
|
|
500
|
+
const laneCount = Math.max(1, Math.floor(feed.element.clientHeight / lineHeight));
|
|
501
|
+
const capacity = feed.component.maxItems * laneCount;
|
|
502
|
+
if (feed.element.children.length >= capacity) return;
|
|
503
|
+
feed.element.appendChild(item);
|
|
504
|
+
let laneState = horizontalLanes.get(feed.key);
|
|
505
|
+
if (!laneState || laneState.ready.length !== laneCount) {
|
|
506
|
+
laneState = { cursor: 0, ready: Array(laneCount).fill(0) };
|
|
507
|
+
horizontalLanes.set(feed.key, laneState);
|
|
508
|
+
}
|
|
509
|
+
const now = performance.now();
|
|
510
|
+
let lane = -1;
|
|
511
|
+
for (let offset = 0; offset < laneCount; offset += 1) {
|
|
512
|
+
const candidate = (laneState.cursor + offset) % laneCount;
|
|
513
|
+
if (laneState.ready[candidate] <= now) { lane = candidate; break; }
|
|
514
|
+
}
|
|
515
|
+
if (lane < 0) lane = laneState.ready.indexOf(Math.min(...laneState.ready));
|
|
516
|
+
const delay = Math.max(0, laneState.ready[lane] - now);
|
|
517
|
+
laneState.cursor = (lane + 1) % laneCount;
|
|
518
|
+
item.style.top = `${lane * lineHeight}px`;
|
|
519
|
+
const distance = feed.element.clientWidth + item.scrollWidth + 20;
|
|
520
|
+
const duration = Math.max(1000, distance / feed.component.speed * 1000);
|
|
521
|
+
const actualSpeed = distance / (duration / 1000);
|
|
522
|
+
laneState.ready[lane] = now + delay + (item.scrollWidth + feed.component.gap) / actualSpeed * 1000;
|
|
523
|
+
item.style.opacity = '0';
|
|
524
|
+
const animation = item.animate(
|
|
525
|
+
[
|
|
526
|
+
{ transform: 'translateX(0)', opacity: 1 },
|
|
527
|
+
{ transform: `translateX(-${distance}px)`, opacity: 1 },
|
|
528
|
+
],
|
|
529
|
+
{ duration, delay, easing: 'linear' },
|
|
530
|
+
);
|
|
531
|
+
animation.addEventListener('finish', () => item.remove(), { once: true });
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function restartNotice(track, viewport, component, style, text = component.text) {
|
|
535
|
+
const revision = String(Number(track.dataset.animationRevision || 0) + 1);
|
|
536
|
+
track.dataset.animationRevision = revision;
|
|
537
|
+
for (const animation of track.getAnimations()) animation.cancel();
|
|
538
|
+
track.classList.remove('line-carousel');
|
|
539
|
+
track.style.gap = '';
|
|
540
|
+
const lines = String(text ?? '').split(/\r?\n/).filter((line) => line.trim());
|
|
541
|
+
const lineCarousel = lines.length > 1 && component.lineTransitionMs > 0;
|
|
542
|
+
if (lineCarousel) {
|
|
543
|
+
track.classList.add('line-carousel');
|
|
544
|
+
const slides = lines.map((line) => {
|
|
545
|
+
const slide = textElement(line, style);
|
|
546
|
+
slide.classList.add('notice-slide');
|
|
547
|
+
return slide;
|
|
548
|
+
});
|
|
549
|
+
track.replaceChildren(...slides, slides[0].cloneNode(true));
|
|
550
|
+
} else {
|
|
551
|
+
const line = textElement(String(text ?? ''), style);
|
|
552
|
+
line.style.flex = '0 0 auto';
|
|
553
|
+
track.replaceChildren(line);
|
|
554
|
+
}
|
|
555
|
+
requestAnimationFrame(() => {
|
|
556
|
+
if (!track.isConnected || track.dataset.animationRevision !== revision) return;
|
|
557
|
+
if (lineCarousel) animateLineNotice(track, viewport, component, lines.length);
|
|
558
|
+
else animateContinuousNotice(track, viewport, component);
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
function animateContinuousNotice(track, viewport, component) {
|
|
563
|
+
const horizontal = component.axis === 'horizontal';
|
|
564
|
+
const contentDistance = horizontal
|
|
565
|
+
? viewport.clientWidth + track.scrollWidth
|
|
566
|
+
: viewport.clientHeight + track.scrollHeight;
|
|
567
|
+
const distance = contentDistance + component.gap;
|
|
568
|
+
const duration = Math.max(1000, distance / component.speed * 1000);
|
|
569
|
+
const from = horizontal ? 'translate(0,-50%)' : 'translate(-50%,0)';
|
|
570
|
+
const to = horizontal ? `translate(-${distance}px,-50%)` : `translate(-50%,-${distance}px)`;
|
|
571
|
+
track.animate([{ transform: from }, { transform: to }], {
|
|
572
|
+
duration,
|
|
573
|
+
iterations: Infinity,
|
|
574
|
+
easing: 'linear',
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function animateLineNotice(track, viewport, component, lineCount) {
|
|
579
|
+
const horizontal = component.axis === 'horizontal';
|
|
580
|
+
const width = viewport.clientWidth;
|
|
581
|
+
const height = viewport.clientHeight;
|
|
582
|
+
const gap = Math.max(0, Number(component.gap) || 0);
|
|
583
|
+
const step = (horizontal ? width : height) + gap;
|
|
584
|
+
track.style.gap = `${gap}px`;
|
|
585
|
+
for (const slide of track.children) {
|
|
586
|
+
slide.style.width = `${width}px`;
|
|
587
|
+
slide.style.height = `${height}px`;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
const hold = Math.max(0, Number(component.lineHoldMs) || 0);
|
|
591
|
+
const transition = Math.max(1, Number(component.lineTransitionMs) || 0);
|
|
592
|
+
const cycle = hold + transition;
|
|
593
|
+
const duration = lineCount * cycle;
|
|
594
|
+
const transform = (index) => horizontal
|
|
595
|
+
? `translateX(-${step * index}px)`
|
|
596
|
+
: `translateY(-${step * index}px)`;
|
|
597
|
+
const keyframes = [{ transform: transform(0), offset: 0 }];
|
|
598
|
+
for (let index = 0; index < lineCount; index += 1) {
|
|
599
|
+
if (hold) keyframes.push({
|
|
600
|
+
transform: transform(index),
|
|
601
|
+
offset: (index * cycle + hold) / duration,
|
|
602
|
+
});
|
|
603
|
+
keyframes.push({
|
|
604
|
+
transform: transform(index + 1),
|
|
605
|
+
offset: ((index + 1) * cycle) / duration,
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
track.animate(keyframes, { duration, iterations: Infinity, easing: 'linear' });
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function applyEdgeFade(element, axis, value) {
|
|
612
|
+
const fade = Math.max(0, Number(value) || 0);
|
|
613
|
+
if (!fade) return;
|
|
614
|
+
const direction = axis === 'vertical' ? 'to bottom' : 'to right';
|
|
615
|
+
const mask = `linear-gradient(${direction}, transparent 0, #000 min(${fade}px, 50%), #000 max(calc(100% - ${fade}px), 50%), transparent 100%)`;
|
|
616
|
+
element.style.maskImage = mask;
|
|
617
|
+
element.style.webkitMaskImage = mask;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function componentImageSource(component) {
|
|
621
|
+
if (component.source === 'upload' && component.assetId) {
|
|
622
|
+
return `/assets/${encodeURIComponent(component.assetId)}`;
|
|
623
|
+
}
|
|
624
|
+
return component.source === 'external' ? component.url || '' : '';
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
function applyPanelStyle(element, style) {
|
|
628
|
+
element.style.borderImage = '';
|
|
629
|
+
element.style.background = style.background;
|
|
630
|
+
element.style.borderColor = style.borderColor;
|
|
631
|
+
element.style.borderWidth = `${style.borderWidth}px`;
|
|
632
|
+
element.style.borderStyle = style.borderWidth ? 'solid' : 'none';
|
|
633
|
+
element.style.borderRadius = `${style.radius}px`;
|
|
634
|
+
element.style.padding = `${style.padding}px`;
|
|
635
|
+
if (style.nineSlice && style.nineSlice.assetId) {
|
|
636
|
+
const nine = style.nineSlice;
|
|
637
|
+
const slice = nine.slice || nine;
|
|
638
|
+
const width = nine.width || slice;
|
|
639
|
+
element.style.borderStyle = 'solid';
|
|
640
|
+
element.style.borderWidth = `${width.top}px ${width.right}px ${width.bottom}px ${width.left}px`;
|
|
641
|
+
element.style.borderColor = 'transparent';
|
|
642
|
+
element.style.borderImageSource = `url('/assets/${encodeURIComponent(nine.assetId)}')`;
|
|
643
|
+
element.style.borderImageSlice = `${slice.top} ${slice.right} ${slice.bottom} ${slice.left}${nine.fill ? ' fill' : ''}`;
|
|
644
|
+
element.style.borderImageWidth = `${width.top}px ${width.right}px ${width.bottom}px ${width.left}px`;
|
|
645
|
+
element.style.borderImageRepeat = nine.repeat || 'stretch';
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function textElement(text, style) {
|
|
650
|
+
const element = document.createElement('span');
|
|
651
|
+
element.className = 'overlay-text';
|
|
652
|
+
element.textContent = text;
|
|
653
|
+
applyTextStyle(element, style);
|
|
654
|
+
return element;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
function applyTextStyle(element, style) {
|
|
658
|
+
element.style.fontFamily = style.fontFamily;
|
|
659
|
+
element.style.fontSize = `${style.fontSize}px`;
|
|
660
|
+
element.style.fontWeight = String(style.fontWeight);
|
|
661
|
+
element.style.color = style.color;
|
|
662
|
+
element.style.webkitTextStroke = `${style.strokeWidth}px ${style.strokeColor}`;
|
|
663
|
+
element.style.paintOrder = 'stroke fill';
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
function styleById(id) {
|
|
667
|
+
return design.styles.find((item) => item.id === id)
|
|
668
|
+
|| builtinStyles.find((item) => item.id === id)
|
|
669
|
+
|| builtinStyles[0]
|
|
670
|
+
|| {
|
|
671
|
+
background: '#00000099', borderColor: '#00000000', borderWidth: 0, radius: 0, padding: 8,
|
|
672
|
+
username: defaultText(), body: defaultText(),
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
function defaultText() {
|
|
677
|
+
return {
|
|
678
|
+
fontFamily: 'Microsoft YaHei, sans-serif', fontSize: 30, fontWeight: 500,
|
|
679
|
+
color: '#ffffff', strokeColor: '#000000', strokeWidth: 0,
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function fitStage() {
|
|
684
|
+
if (!design) return;
|
|
685
|
+
const scale = Math.min(innerWidth / design.canvas.width, innerHeight / design.canvas.height);
|
|
686
|
+
const left = (innerWidth - design.canvas.width * scale) / 2;
|
|
687
|
+
const top = (innerHeight - design.canvas.height * scale) / 2;
|
|
688
|
+
stage.style.transform = `translate(${left}px,${top}px) scale(${scale})`;
|
|
689
|
+
}
|
|
690
|
+
})();
|