@mica-js/editor 0.12.0 → 0.13.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/dist/fused.js CHANGED
@@ -3211,6 +3211,18 @@ function resolveViewLayout(saved, manifest, availableHeight) {
3211
3211
  //#endregion
3212
3212
  //#region src/fused/simulation.ts
3213
3213
  /**
3214
+ * Host-only pause, resume and restart of the fused workbench run session.
3215
+ *
3216
+ * The workbench owns the runtime and serializes mode transitions; this control only mirrors the
3217
+ * status of the attached session and forwards the commands. Pausing keeps the session mounted:
3218
+ * models, views and subscriptions stay as they are, and only simulated time stops advancing.
3219
+ * Restarting rebuilds the models from the same captured bytes before advancing again.
3220
+ */
3221
+ /** 采样窗口与刷新间隔:窗口决定平滑度,间隔限制宿主收到的更新频率。 */
3222
+ var SPEED_WINDOW_MS = 1e3;
3223
+ var SPEED_INTERVAL_MS = 500;
3224
+ var PS_PER_MS = 1e9;
3225
+ /**
3214
3226
  * Creates the host control of one workbench run session.
3215
3227
  *
3216
3228
  * Only status transitions reach `onDidChangeStatus`; simulated time changes are ignored, so a host
@@ -3221,16 +3233,53 @@ function createSimulationControl(onDidChangeStatus) {
3221
3233
  let subscription;
3222
3234
  let status;
3223
3235
  let closed = false;
3236
+ const speedChanged = new Emitter();
3237
+ let speed;
3238
+ let samples = [];
3239
+ let sampledAt = 0;
3224
3240
  function publish(next) {
3225
3241
  if (next === status) return;
3226
3242
  status = next;
3227
3243
  if (!closed) onDidChangeStatus(next);
3228
3244
  }
3245
+ function publishSpeed(next) {
3246
+ if (next === speed) return;
3247
+ speed = next;
3248
+ if (!closed) speedChanged.fire(next);
3249
+ }
3250
+ /**
3251
+ * 用调用时刻与最近一秒的采样换算实时率;模拟时间推进得比现实快就大于 100%。
3252
+ * 停止、暂停与重载都会清空采样,恢复运行时不会把暂停时长算进速度。
3253
+ */
3254
+ function sample(state) {
3255
+ if (state.status !== "running") {
3256
+ samples = [];
3257
+ sampledAt = 0;
3258
+ publishSpeed(void 0);
3259
+ return;
3260
+ }
3261
+ const now = performance.now();
3262
+ samples.push({
3263
+ wallMs: now,
3264
+ simTimePs: state.simTime
3265
+ });
3266
+ while (samples.length > 2 && now - samples[0].wallMs > SPEED_WINDOW_MS) samples.shift();
3267
+ if (now - sampledAt < SPEED_INTERVAL_MS) return;
3268
+ const first = samples[0];
3269
+ const elapsed = now - first.wallMs;
3270
+ if (elapsed < SPEED_INTERVAL_MS) return;
3271
+ sampledAt = now;
3272
+ const ratio = (state.simTime - first.simTimePs) / (elapsed * PS_PER_MS);
3273
+ publishSpeed(Math.max(0, Math.round(ratio * 100)));
3274
+ }
3229
3275
  function detach() {
3230
3276
  subscription?.dispose();
3231
3277
  subscription = void 0;
3232
3278
  runtime = void 0;
3279
+ samples = [];
3280
+ sampledAt = 0;
3233
3281
  publish(void 0);
3282
+ publishSpeed(void 0);
3234
3283
  }
3235
3284
  function requireSession(operation) {
3236
3285
  if (closed) throw new Error(`simulation.${operation} requires an active workbench`);
@@ -3241,6 +3290,10 @@ function createSimulationControl(onDidChangeStatus) {
3241
3290
  get status() {
3242
3291
  return status;
3243
3292
  },
3293
+ get speed() {
3294
+ return speed;
3295
+ },
3296
+ onDidChangeSpeed: speedChanged.event,
3244
3297
  attach(next) {
3245
3298
  if (closed) throw new Error("The simulation control is disposed");
3246
3299
  detach();
@@ -3248,8 +3301,11 @@ function createSimulationControl(onDidChangeStatus) {
3248
3301
  subscription = next.onDidChangeState((state) => {
3249
3302
  if (runtime !== next) return;
3250
3303
  publish(state.status);
3304
+ sample(state);
3251
3305
  });
3252
- publish(next.getState().status);
3306
+ const initial = next.getState();
3307
+ publish(initial.status);
3308
+ sample(initial);
3253
3309
  },
3254
3310
  detach,
3255
3311
  async pause() {
@@ -3276,6 +3332,9 @@ function createSimulationControl(onDidChangeStatus) {
3276
3332
  subscription = void 0;
3277
3333
  runtime = void 0;
3278
3334
  status = void 0;
3335
+ speed = void 0;
3336
+ samples = [];
3337
+ speedChanged.dispose();
3279
3338
  }
3280
3339
  };
3281
3340
  }
@@ -4633,6 +4692,46 @@ function programPackageCandidates(paths, loader, formats = []) {
4633
4692
  }
4634
4693
  return [...directories].sort();
4635
4694
  }
4695
+ var IMAGE_EXTENSIONS = [
4696
+ "png",
4697
+ "jpg",
4698
+ "jpeg",
4699
+ "gif",
4700
+ "webp",
4701
+ "svg",
4702
+ "bmp"
4703
+ ];
4704
+ /** 资源属性查询资产的用途;固件与程序绑定都按程序请求。 */
4705
+ function assetRequestKind(descriptor) {
4706
+ if (descriptor.type === "image") return "image";
4707
+ return descriptor.type === "program" || descriptor.type === "firmware" ? "program" : "binary";
4708
+ }
4709
+ /** 属性接受的扩展名,不带点;返回 `undefined` 表示不限格式。 */
4710
+ function assetRequestFormats(descriptor, memory) {
4711
+ if (descriptor.type === "program") return memory ? descriptor.formats : ["zip"];
4712
+ if (descriptor.type === "firmware") return [
4713
+ "bin",
4714
+ "hex",
4715
+ "elf"
4716
+ ];
4717
+ if (descriptor.type === "binary") return descriptor.extensions?.map((ext) => ext.slice(1));
4718
+ }
4719
+ function assetRequestAccept(descriptor, memory) {
4720
+ const formats = assetRequestFormats(descriptor, memory) ?? (descriptor.type === "image" ? IMAGE_EXTENSIONS : void 0);
4721
+ return formats === void 0 || formats.length === 0 ? void 0 : formats.map((extension) => `.${extension}`).join(",");
4722
+ }
4723
+ /** 上传前按属性声明的格式快速拒绝,避免为不支持的文件打开写入流程。 */
4724
+ function resourceAcceptsFile(descriptor, memory, name) {
4725
+ const extension = name.split(".").at(-1)?.toLowerCase() ?? "";
4726
+ if (extension === "" || extension === name.toLowerCase()) return false;
4727
+ const formats = assetRequestFormats(descriptor, memory) ?? (descriptor.type === "image" ? IMAGE_EXTENSIONS : void 0);
4728
+ return formats === void 0 || formats.length === 0 ? true : formats.some((format) => format.toLowerCase() === extension);
4729
+ }
4730
+ /** 上传入口的动词按属性类型区分,已有绑定时说明会替换。 */
4731
+ function uploadLabel(descriptor, bound) {
4732
+ const noun = descriptor.type === "program" || descriptor.type === "firmware" ? "固件" : descriptor.type === "image" ? "图片" : "文件";
4733
+ return bound ? `上传并替换${noun}…` : `上传${noun}…`;
4734
+ }
4636
4735
  function programFormat(path) {
4637
4736
  const ext = path.split(".").at(-1)?.toLowerCase();
4638
4737
  return ext === "bin" ? "bin" : ext === "hex" || ext === "ihx" ? "hex" : ext === "elf" || ext === "axf" ? "elf" : void 0;
@@ -4685,6 +4784,7 @@ function ResourceField(props) {
4685
4784
  const [packagePicker, setPackagePicker] = useState(false);
4686
4785
  const [details, setDetails] = useState(false);
4687
4786
  const [busy, setBusy] = useState(false);
4787
+ const [dragging, setDragging] = useState(false);
4688
4788
  const [error, setError] = useState("");
4689
4789
  const [retry, setRetry] = useState(0);
4690
4790
  const [projectFormat, setProjectFormat] = useState();
@@ -4694,6 +4794,7 @@ function ResourceField(props) {
4694
4794
  const detailsId = useId();
4695
4795
  const menuButton = useRef(null);
4696
4796
  const detailsButton = useRef(null);
4797
+ const uploadInput = useRef(null);
4697
4798
  const pending = useRef(void 0);
4698
4799
  const retryChoice = useRef(void 0);
4699
4800
  const signature = JSON.stringify([target, props.value]);
@@ -4742,6 +4843,37 @@ function ResourceField(props) {
4742
4843
  };
4743
4844
  const program = descriptor.type === "program";
4744
4845
  const memory = target.loader === "memory";
4846
+ const canUpload = !props.disabled && props.assets?.upload !== void 0;
4847
+ const referenceAssetId = reference?.mode === "asset" ? reference.assetId : void 0;
4848
+ const chooseUpload = (file) => {
4849
+ if (!canUpload) return;
4850
+ if (!resourceAcceptsFile(descriptor, memory, file.name)) {
4851
+ fail(/* @__PURE__ */ new Error(`不支持的文件格式:${file.name}`));
4852
+ return;
4853
+ }
4854
+ perform(async (signal) => {
4855
+ const bytes = new Uint8Array(await file.arrayBuffer());
4856
+ signal.throwIfAborted();
4857
+ const formats = assetRequestFormats(descriptor, memory);
4858
+ const asset = await props.assets.upload({
4859
+ kind: assetRequestKind(descriptor),
4860
+ ...formats === void 0 ? {} : { formats },
4861
+ designator: props.designator,
4862
+ property: props.name,
4863
+ name: file.name,
4864
+ bytes,
4865
+ replace: props.assetReplacement(referenceAssetId),
4866
+ signal
4867
+ });
4868
+ if (asset === void 0) return void 0;
4869
+ return {
4870
+ mode: "asset",
4871
+ assetId: asset.id,
4872
+ name: asset.name
4873
+ };
4874
+ });
4875
+ };
4876
+ const acceptsDrop = (event) => canUpload && event.dataTransfer.types.includes("Files");
4745
4877
  const sourceName = reference?.mode === "package" ? reference.src : reference?.name;
4746
4878
  const name = reference?.mode === "project" ? reference.name ?? reference.projectId : sourceName;
4747
4879
  const source = reference?.mode === "asset" ? "资产库" : reference?.mode === "project" ? "项目" : reference?.mode === "package" ? "内置资源" : "";
@@ -4813,7 +4945,7 @@ function ResourceField(props) {
4813
4945
  };
4814
4946
  const snapshot = props.snapshot;
4815
4947
  return /* @__PURE__ */ jsxs("div", {
4816
- className: "mica-wb-program-control",
4948
+ className: `mica-wb-property mica-wb-resource-field${dragging ? " is-drop-target" : ""}`,
4817
4949
  onKeyDown: (event) => {
4818
4950
  if (event.key === "Escape") {
4819
4951
  if (menu) menuButton.current?.focus();
@@ -4823,245 +4955,299 @@ function ResourceField(props) {
4823
4955
  setPackagePicker(false);
4824
4956
  }
4825
4957
  },
4826
- children: [
4827
- /* @__PURE__ */ jsxs("div", {
4828
- className: "mica-wb-resource-control",
4829
- children: [
4830
- preview && /* @__PURE__ */ jsx("img", {
4831
- className: "mica-wb-resource-preview",
4832
- src: preview,
4833
- alt: ""
4834
- }),
4835
- /* @__PURE__ */ jsxs("div", {
4836
- className: "mica-wb-resource-summary",
4837
- children: [
4838
- source && /* @__PURE__ */ jsx("span", {
4839
- className: "mica-wb-resource-source",
4840
- title: source,
4841
- "aria-label": source,
4842
- children: /* @__PURE__ */ jsx(Icon, { name: sourceIcon })
4843
- }),
4844
- /* @__PURE__ */ jsx("span", {
4845
- className: "mica-wb-resource-name",
4846
- title: name,
4847
- children: name ?? "未设置"
4848
- }),
4849
- source && !props.overridden && /* @__PURE__ */ jsx("small", { children: "默认" })
4850
- ]
4851
- }),
4852
- /* @__PURE__ */ jsx("button", {
4853
- ref: menuButton,
4854
- className: "mica-wb-resource-action",
4855
- "aria-label": `${name ? "更换" : "选择"}${descriptor.title ?? props.name}来源`,
4856
- title: `${name ? "更换" : "选择"}来源`,
4857
- "aria-controls": menuId,
4858
- "aria-expanded": menu,
4859
- disabled: props.disabled || busy,
4860
- onClick: () => setMenu(!menu),
4861
- children: /* @__PURE__ */ jsx(Icon, { name: "chevron-down" })
4862
- }),
4863
- hasDetails && /* @__PURE__ */ jsx("button", {
4864
- ref: detailsButton,
4865
- className: "mica-wb-resource-action",
4866
- "aria-label": detailsTitle,
4867
- title: detailsTitle,
4868
- "aria-controls": detailsId,
4869
- "aria-expanded": details,
4870
- onClick: () => setDetails(!details),
4871
- children: /* @__PURE__ */ jsx(Icon, { name: program ? "settings-gear" : "info" })
4872
- }),
4873
- reference?.mode === "project" && /* @__PURE__ */ jsx("button", {
4874
- title: "打开项目",
4875
- "aria-label": "打开项目",
4876
- onClick: () => props.onOpenProject(reference.projectId),
4877
- children: /* @__PURE__ */ jsx(Icon, { name: "go-to-file" })
4878
- })
4879
- ]
4880
- }),
4881
- /* @__PURE__ */ jsxs(ResourcePopover, {
4882
- id: menuId,
4883
- anchor: menuButton,
4884
- open: menu,
4885
- onClose: () => setMenu(false),
4886
- className: "mica-wb-resource-menu",
4887
- label: "资源来源",
4888
- children: [
4889
- /* @__PURE__ */ jsx("button", {
4890
- disabled: !props.assets,
4891
- onClick: () => void perform(async (signal) => {
4892
- const asset = await props.assets.select({
4893
- kind: program || descriptor.type === "firmware" ? "program" : descriptor.type === "image" ? "image" : "binary",
4894
- ...program ? { formats: memory ? descriptor.formats : ["zip"] } : descriptor.type === "binary" && descriptor.extensions ? { formats: descriptor.extensions.map((ext) => ext.slice(1)) } : descriptor.type === "firmware" ? { formats: [
4895
- "bin",
4896
- "hex",
4897
- "elf"
4898
- ] } : {},
4899
- signal
4900
- });
4901
- if (asset === void 0) return void 0;
4902
- if (typeof asset.id !== "string" || !asset.id.trim() || typeof asset.name !== "string" || !asset.name.trim()) throw new Error("asset/invalid-selection: 资产信息无效");
4903
- return {
4904
- mode: "asset",
4905
- assetId: asset.id,
4906
- name: asset.name
4907
- };
4958
+ onDragEnter: (event) => {
4959
+ if (!acceptsDrop(event)) return;
4960
+ event.preventDefault();
4961
+ setDragging(true);
4962
+ },
4963
+ onDragOver: (event) => {
4964
+ if (!acceptsDrop(event)) return;
4965
+ event.preventDefault();
4966
+ event.dataTransfer.dropEffect = "copy";
4967
+ },
4968
+ onDragLeave: (event) => {
4969
+ if (event.currentTarget.contains(event.relatedTarget)) return;
4970
+ setDragging(false);
4971
+ },
4972
+ onDrop: (event) => {
4973
+ event.preventDefault();
4974
+ setDragging(false);
4975
+ if (!canUpload) return;
4976
+ const file = event.dataTransfer.files[0];
4977
+ if (file !== void 0) chooseUpload(file);
4978
+ },
4979
+ children: [/* @__PURE__ */ jsx("label", {
4980
+ title: descriptor.description,
4981
+ children: descriptor.title ?? props.name
4982
+ }), /* @__PURE__ */ jsxs("div", {
4983
+ className: "mica-wb-property-control",
4984
+ children: [
4985
+ /* @__PURE__ */ jsx("input", {
4986
+ ref: uploadInput,
4987
+ type: "file",
4988
+ hidden: true,
4989
+ disabled: !canUpload,
4990
+ accept: assetRequestAccept(descriptor, memory),
4991
+ "aria-label": uploadLabel(descriptor, reference !== void 0),
4992
+ onChange: (event) => {
4993
+ const file = event.currentTarget.files?.[0];
4994
+ event.currentTarget.value = "";
4995
+ if (file !== void 0) chooseUpload(file);
4996
+ }
4997
+ }),
4998
+ dragging && /* @__PURE__ */ jsx("span", {
4999
+ className: "mica-wb-resource-drop",
5000
+ children: reference === void 0 ? "松开以上传文件" : "松开以替换当前文件"
5001
+ }),
5002
+ /* @__PURE__ */ jsxs("div", {
5003
+ className: "mica-wb-program-control",
5004
+ children: [
5005
+ /* @__PURE__ */ jsxs("div", {
5006
+ className: "mica-wb-resource-control",
5007
+ children: [
5008
+ preview && /* @__PURE__ */ jsx("img", {
5009
+ className: "mica-wb-resource-preview",
5010
+ src: preview,
5011
+ alt: ""
5012
+ }),
5013
+ /* @__PURE__ */ jsxs("div", {
5014
+ className: "mica-wb-resource-summary",
5015
+ children: [
5016
+ source && /* @__PURE__ */ jsx("span", {
5017
+ className: "mica-wb-resource-source",
5018
+ title: source,
5019
+ "aria-label": source,
5020
+ children: /* @__PURE__ */ jsx(Icon, { name: sourceIcon })
5021
+ }),
5022
+ /* @__PURE__ */ jsx("span", {
5023
+ className: "mica-wb-resource-name",
5024
+ title: name,
5025
+ children: name ?? (canUpload ? "未设置 · 可拖入文件" : "未设置")
5026
+ }),
5027
+ source && !props.overridden && /* @__PURE__ */ jsx("small", { children: "默认" })
5028
+ ]
5029
+ }),
5030
+ /* @__PURE__ */ jsx("button", {
5031
+ ref: menuButton,
5032
+ className: "mica-wb-resource-action",
5033
+ "aria-label": `${name ? "更换" : "选择"}${descriptor.title ?? props.name}来源`,
5034
+ title: `${name ? "更换" : "选择"}来源`,
5035
+ "aria-controls": menuId,
5036
+ "aria-expanded": menu,
5037
+ disabled: props.disabled || busy,
5038
+ onClick: () => setMenu(!menu),
5039
+ children: /* @__PURE__ */ jsx(Icon, { name: "chevron-down" })
5040
+ }),
5041
+ hasDetails && /* @__PURE__ */ jsx("button", {
5042
+ ref: detailsButton,
5043
+ className: "mica-wb-resource-action",
5044
+ "aria-label": detailsTitle,
5045
+ title: detailsTitle,
5046
+ "aria-controls": detailsId,
5047
+ "aria-expanded": details,
5048
+ onClick: () => setDetails(!details),
5049
+ children: /* @__PURE__ */ jsx(Icon, { name: program ? "settings-gear" : "info" })
5050
+ }),
5051
+ reference?.mode === "project" && /* @__PURE__ */ jsx("button", {
5052
+ title: "打开项目",
5053
+ "aria-label": "打开项目",
5054
+ onClick: () => props.onOpenProject(reference.projectId),
5055
+ children: /* @__PURE__ */ jsx(Icon, { name: "go-to-file" })
5056
+ })
5057
+ ]
4908
5058
  }),
4909
- children: "从资产库选择…"
4910
- }),
4911
- program && /* @__PURE__ */ jsx("button", {
4912
- disabled: !sources?.projects?.select,
4913
- onClick: () => void perform(async (signal) => {
4914
- const project = await sources.projects.select({
4915
- ...target,
4916
- templateIds: [...descriptor.templates ?? []]
4917
- }, signal);
4918
- if (project === void 0) return void 0;
4919
- validateProgramProjects([project]);
4920
- return {
4921
- mode: "project",
4922
- projectId: project.id,
4923
- name: project.title
4924
- };
5059
+ /* @__PURE__ */ jsxs(ResourcePopover, {
5060
+ id: menuId,
5061
+ anchor: menuButton,
5062
+ open: menu,
5063
+ onClose: () => setMenu(false),
5064
+ className: "mica-wb-resource-menu",
5065
+ label: "资源来源",
5066
+ children: [
5067
+ props.assets?.upload !== void 0 && /* @__PURE__ */ jsx("button", {
5068
+ disabled: props.disabled,
5069
+ onClick: () => {
5070
+ setMenu(false);
5071
+ uploadInput.current?.click();
5072
+ },
5073
+ children: uploadLabel(descriptor, reference !== void 0)
5074
+ }),
5075
+ /* @__PURE__ */ jsx("button", {
5076
+ disabled: !props.assets,
5077
+ onClick: () => void perform(async (signal) => {
5078
+ const formats = assetRequestFormats(descriptor, memory);
5079
+ const asset = await props.assets.select({
5080
+ kind: assetRequestKind(descriptor),
5081
+ ...formats === void 0 ? {} : { formats },
5082
+ signal
5083
+ });
5084
+ if (asset === void 0) return void 0;
5085
+ if (typeof asset.id !== "string" || !asset.id.trim() || typeof asset.name !== "string" || !asset.name.trim()) throw new Error("asset/invalid-selection: 资产信息无效");
5086
+ return {
5087
+ mode: "asset",
5088
+ assetId: asset.id,
5089
+ name: asset.name
5090
+ };
5091
+ }),
5092
+ children: "从资产库选择…"
5093
+ }),
5094
+ program && /* @__PURE__ */ jsx("button", {
5095
+ disabled: !sources?.projects?.select,
5096
+ onClick: () => void perform(async (signal) => {
5097
+ const project = await sources.projects.select({
5098
+ ...target,
5099
+ templateIds: [...descriptor.templates ?? []]
5100
+ }, signal);
5101
+ if (project === void 0) return void 0;
5102
+ validateProgramProjects([project]);
5103
+ return {
5104
+ mode: "project",
5105
+ projectId: project.id,
5106
+ name: project.title
5107
+ };
5108
+ }),
5109
+ children: "从项目选择…"
5110
+ }),
5111
+ /* @__PURE__ */ jsx("button", {
5112
+ onClick: () => {
5113
+ setMenu(false);
5114
+ setPackagePicker(true);
5115
+ },
5116
+ children: "从内置资源选择…"
5117
+ }),
5118
+ props.overridden && descriptor.default !== void 0 && /* @__PURE__ */ jsx("button", {
5119
+ onClick: () => {
5120
+ setMenu(false);
5121
+ props.reset();
5122
+ },
5123
+ children: "恢复默认"
5124
+ }),
5125
+ descriptor.nullable && props.value !== null && /* @__PURE__ */ jsx("button", {
5126
+ onClick: () => {
5127
+ setMenu(false);
5128
+ props.set(null);
5129
+ },
5130
+ children: "清除"
5131
+ })
5132
+ ]
4925
5133
  }),
4926
- children: "从项目选择…"
4927
- }),
4928
- /* @__PURE__ */ jsx("button", {
4929
- onClick: () => {
4930
- setMenu(false);
4931
- setPackagePicker(true);
4932
- },
4933
- children: "从内置资源选择…"
4934
- }),
4935
- props.overridden && descriptor.default !== void 0 && /* @__PURE__ */ jsx("button", {
4936
- onClick: () => {
4937
- setMenu(false);
4938
- props.reset();
4939
- },
4940
- children: "恢复默认"
4941
- }),
4942
- descriptor.nullable && props.value !== null && /* @__PURE__ */ jsx("button", {
4943
- onClick: () => {
4944
- setMenu(false);
4945
- props.set(null);
4946
- },
4947
- children: "清除"
4948
- })
4949
- ]
4950
- }),
4951
- packagePicker && /* @__PURE__ */ jsxs("div", {
4952
- className: "mica-wb-resource-control",
4953
- children: [
4954
- /* @__PURE__ */ jsxs("select", {
4955
- "aria-label": "内置资源",
4956
- disabled: busy || props.disabled,
4957
- value: "",
4958
- onChange: (event) => {
4959
- const src = event.target.value;
4960
- if (src) perform(async () => ({
4961
- mode: "package",
4962
- src
4963
- }));
4964
- },
4965
- children: [/* @__PURE__ */ jsx("option", {
4966
- value: "",
4967
- children: "选择内置资源"
4968
- }), candidates.map((path) => /* @__PURE__ */ jsx("option", {
4969
- value: path,
4970
- children: path
4971
- }, path))]
4972
- }),
4973
- !candidates.length && /* @__PURE__ */ jsx("span", { children: "没有匹配的内置资源" }),
4974
- /* @__PURE__ */ jsx("button", {
4975
- disabled: busy,
4976
- onClick: () => setPackagePicker(false),
4977
- children: "取消"
4978
- })
4979
- ]
4980
- }),
4981
- busy && /* @__PURE__ */ jsx("span", {
4982
- role: "status",
4983
- children: "选择/校验中…"
4984
- }),
4985
- error && /* @__PURE__ */ jsxs("div", {
4986
- role: "alert",
4987
- children: [error, /* @__PURE__ */ jsx("button", {
4988
- disabled: busy,
4989
- onClick: () => {
4990
- setError("");
4991
- setRetry((value) => value + 1);
4992
- if (!props.disabled && retryChoice.current) perform(retryChoice.current);
4993
- },
4994
- children: "重试"
4995
- })]
4996
- }),
4997
- /* @__PURE__ */ jsxs(ResourcePopover, {
4998
- id: detailsId,
4999
- anchor: detailsButton,
5000
- open: details && hasDetails,
5001
- onClose: () => setDetails(false),
5002
- className: "mica-wb-resource-details",
5003
- label: detailsTitle,
5004
- children: [!program && reference?.mode === "asset" && /* @__PURE__ */ jsxs("div", {
5005
- className: "mica-wb-resource-details",
5006
- children: ["UUID:", reference.assetId]
5007
- }), program && /* @__PURE__ */ jsxs("div", {
5008
- className: "mica-wb-resource-details",
5009
- children: [reference?.mode === "asset" && /* @__PURE__ */ jsxs("div", { children: ["UUID:", reference.assetId] }), memory && /* @__PURE__ */ jsxs(Fragment, { children: [
5010
- /* @__PURE__ */ jsxs("div", { children: ["格式:", format?.toUpperCase() ?? "等待产物信息"] }),
5011
- format === "elf" && Object.entries(spaces).map(([id, space]) => /* @__PURE__ */ jsxs("div", { children: [
5012
- space.title ?? id,
5013
- ": [0x",
5014
- space.start.toString(16),
5015
- ", 0x",
5016
- (space.start + space.size).toString(16),
5017
- ")"
5018
- ] }, id)),
5019
- (format === "bin" || format === "hex") && /* @__PURE__ */ jsxs("label", { children: ["地址空间", /* @__PURE__ */ jsx("select", {
5020
- "aria-label": "地址空间",
5021
- value: selectedSpace ?? "",
5022
- disabled: props.disabled || busy || Object.keys(spaces).length < 2 || !reference,
5023
- onChange: (event) => {
5024
- const addressSpace = event.target.value;
5025
- if (format === "bin" && loadAddress !== void 0 && !validAddress(addressSpace, loadAddress)) {
5026
- fail(/* @__PURE__ */ new Error("program/out-of-range"));
5027
- return;
5028
- }
5029
- if (reference) perform(async () => ({
5030
- ...reference,
5031
- addressSpace
5032
- }));
5033
- },
5034
- children: Object.entries(spaces).map(([id, space]) => /* @__PURE__ */ jsx("option", {
5035
- value: id,
5036
- children: space.title ?? id
5037
- }, id))
5038
- })] }),
5039
- format === "bin" && /* @__PURE__ */ jsxs("label", { children: ["装载地址", /* @__PURE__ */ jsx("input", {
5040
- "aria-label": "装载地址",
5041
- value: address,
5042
- disabled: props.disabled || busy || !reference,
5043
- onChange: (event) => setAddress(event.target.value),
5044
- onBlur: saveAddress,
5045
- onKeyDown: (event) => {
5046
- if (event.key === "Enter") {
5047
- event.preventDefault();
5048
- saveAddress();
5049
- }
5050
- }
5051
- })] }),
5052
- snapshot && typeof snapshot === "object" && "kind" in snapshot && snapshot.kind === "firmware" && snapshot.segments.map((segment, index) => /* @__PURE__ */ jsxs("div", { children: [
5053
- segment.addressSpace,
5054
- ": 0x",
5055
- segment.address.toString(16),
5056
- " ·",
5057
- " ",
5058
- segment.bytes.byteLength,
5059
- " 字节"
5060
- ] }, index))
5061
- ] })]
5062
- })]
5063
- })
5064
- ]
5134
+ packagePicker && /* @__PURE__ */ jsxs("div", {
5135
+ className: "mica-wb-resource-control",
5136
+ children: [
5137
+ /* @__PURE__ */ jsxs("select", {
5138
+ "aria-label": "内置资源",
5139
+ disabled: busy || props.disabled,
5140
+ value: "",
5141
+ onChange: (event) => {
5142
+ const src = event.target.value;
5143
+ if (src) perform(async () => ({
5144
+ mode: "package",
5145
+ src
5146
+ }));
5147
+ },
5148
+ children: [/* @__PURE__ */ jsx("option", {
5149
+ value: "",
5150
+ children: "选择内置资源"
5151
+ }), candidates.map((path) => /* @__PURE__ */ jsx("option", {
5152
+ value: path,
5153
+ children: path
5154
+ }, path))]
5155
+ }),
5156
+ !candidates.length && /* @__PURE__ */ jsx("span", { children: "没有匹配的内置资源" }),
5157
+ /* @__PURE__ */ jsx("button", {
5158
+ disabled: busy,
5159
+ onClick: () => setPackagePicker(false),
5160
+ children: "取消"
5161
+ })
5162
+ ]
5163
+ }),
5164
+ busy && /* @__PURE__ */ jsx("span", {
5165
+ role: "status",
5166
+ children: "选择/校验中…"
5167
+ }),
5168
+ error && /* @__PURE__ */ jsxs("div", {
5169
+ role: "alert",
5170
+ children: [error, /* @__PURE__ */ jsx("button", {
5171
+ disabled: busy,
5172
+ onClick: () => {
5173
+ setError("");
5174
+ setRetry((value) => value + 1);
5175
+ if (!props.disabled && retryChoice.current) perform(retryChoice.current);
5176
+ },
5177
+ children: "重试"
5178
+ })]
5179
+ }),
5180
+ /* @__PURE__ */ jsxs(ResourcePopover, {
5181
+ id: detailsId,
5182
+ anchor: detailsButton,
5183
+ open: details && hasDetails,
5184
+ onClose: () => setDetails(false),
5185
+ className: "mica-wb-resource-details",
5186
+ label: detailsTitle,
5187
+ children: [!program && reference?.mode === "asset" && /* @__PURE__ */ jsxs("div", {
5188
+ className: "mica-wb-resource-details",
5189
+ children: ["UUID:", reference.assetId]
5190
+ }), program && /* @__PURE__ */ jsxs("div", {
5191
+ className: "mica-wb-resource-details",
5192
+ children: [reference?.mode === "asset" && /* @__PURE__ */ jsxs("div", { children: ["UUID:", reference.assetId] }), memory && /* @__PURE__ */ jsxs(Fragment, { children: [
5193
+ /* @__PURE__ */ jsxs("div", { children: ["格式:", format?.toUpperCase() ?? "等待产物信息"] }),
5194
+ format === "elf" && Object.entries(spaces).map(([id, space]) => /* @__PURE__ */ jsxs("div", { children: [
5195
+ space.title ?? id,
5196
+ ": [0x",
5197
+ space.start.toString(16),
5198
+ ", 0x",
5199
+ (space.start + space.size).toString(16),
5200
+ ")"
5201
+ ] }, id)),
5202
+ (format === "bin" || format === "hex") && /* @__PURE__ */ jsxs("label", { children: ["地址空间", /* @__PURE__ */ jsx("select", {
5203
+ "aria-label": "地址空间",
5204
+ value: selectedSpace ?? "",
5205
+ disabled: props.disabled || busy || Object.keys(spaces).length < 2 || !reference,
5206
+ onChange: (event) => {
5207
+ const addressSpace = event.target.value;
5208
+ if (format === "bin" && loadAddress !== void 0 && !validAddress(addressSpace, loadAddress)) {
5209
+ fail(/* @__PURE__ */ new Error("program/out-of-range"));
5210
+ return;
5211
+ }
5212
+ if (reference) perform(async () => ({
5213
+ ...reference,
5214
+ addressSpace
5215
+ }));
5216
+ },
5217
+ children: Object.entries(spaces).map(([id, space]) => /* @__PURE__ */ jsx("option", {
5218
+ value: id,
5219
+ children: space.title ?? id
5220
+ }, id))
5221
+ })] }),
5222
+ format === "bin" && /* @__PURE__ */ jsxs("label", { children: ["装载地址", /* @__PURE__ */ jsx("input", {
5223
+ "aria-label": "装载地址",
5224
+ value: address,
5225
+ disabled: props.disabled || busy || !reference,
5226
+ onChange: (event) => setAddress(event.target.value),
5227
+ onBlur: saveAddress,
5228
+ onKeyDown: (event) => {
5229
+ if (event.key === "Enter") {
5230
+ event.preventDefault();
5231
+ saveAddress();
5232
+ }
5233
+ }
5234
+ })] }),
5235
+ snapshot && typeof snapshot === "object" && "kind" in snapshot && snapshot.kind === "firmware" && snapshot.segments.map((segment, index) => /* @__PURE__ */ jsxs("div", { children: [
5236
+ segment.addressSpace,
5237
+ ": 0x",
5238
+ segment.address.toString(16),
5239
+ " ·",
5240
+ " ",
5241
+ segment.bytes.byteLength,
5242
+ " 字节"
5243
+ ] }, index))
5244
+ ] })]
5245
+ })]
5246
+ })
5247
+ ]
5248
+ })
5249
+ ]
5250
+ })]
5065
5251
  });
5066
5252
  }
5067
5253
  //#endregion
@@ -5082,6 +5268,31 @@ function PropertyField(props) {
5082
5268
  props.set(number);
5083
5269
  } else props.set(draft);
5084
5270
  };
5271
+ if ([
5272
+ "program",
5273
+ "image",
5274
+ "binary",
5275
+ "firmware"
5276
+ ].includes(descriptor.type)) return /* @__PURE__ */ jsx(ResourceField, {
5277
+ name: props.name,
5278
+ descriptor,
5279
+ value,
5280
+ target: props.programTarget,
5281
+ sources: props.programSources,
5282
+ packagePaths: props.packagePaths,
5283
+ snapshot: props.snapshot,
5284
+ disabled: props.disabled,
5285
+ overridden: props.overridden,
5286
+ assets: props.assets,
5287
+ designator: props.designator,
5288
+ assetReplacement: props.assetReplacement,
5289
+ validate: props.validate,
5290
+ commit: props.commitResource,
5291
+ reset: props.reset,
5292
+ set: props.set,
5293
+ onOpenProject: props.openProject,
5294
+ onError: props.onError
5295
+ });
5085
5296
  return /* @__PURE__ */ jsxs("div", {
5086
5297
  className: "mica-wb-property",
5087
5298
  children: [/* @__PURE__ */ jsx("label", {
@@ -5089,29 +5300,7 @@ function PropertyField(props) {
5089
5300
  children: descriptor.title ?? props.name
5090
5301
  }), /* @__PURE__ */ jsxs("div", {
5091
5302
  className: "mica-wb-property-control",
5092
- children: [[
5093
- "program",
5094
- "image",
5095
- "binary",
5096
- "firmware"
5097
- ].includes(descriptor.type) ? /* @__PURE__ */ jsx(ResourceField, {
5098
- name: props.name,
5099
- descriptor,
5100
- value,
5101
- target: props.programTarget,
5102
- sources: props.programSources,
5103
- packagePaths: props.packagePaths,
5104
- snapshot: props.snapshot,
5105
- disabled: props.disabled,
5106
- overridden: props.overridden,
5107
- assets: props.assets,
5108
- validate: props.validate,
5109
- commit: props.commitResource,
5110
- reset: props.reset,
5111
- set: props.set,
5112
- onOpenProject: props.openProject,
5113
- onError: props.onError
5114
- }) : descriptor.type === "boolean" ? /* @__PURE__ */ jsx("input", {
5303
+ children: [descriptor.type === "boolean" ? /* @__PURE__ */ jsx("input", {
5115
5304
  type: "checkbox",
5116
5305
  "aria-label": descriptor.title ?? props.name,
5117
5306
  checked: value === true,
@@ -5316,6 +5505,7 @@ function AnnotationFields(props) {
5316
5505
  ] });
5317
5506
  }
5318
5507
  function InspectorView(props) {
5508
+ const [fileDrag, setFileDrag] = useState(false);
5319
5509
  const header = /* @__PURE__ */ jsxs("summary", {
5320
5510
  className: "mica-wb-panel-title",
5321
5511
  style: props.hideHeader ? { display: "none" } : void 0,
@@ -5368,8 +5558,23 @@ function InspectorView(props) {
5368
5558
  open: props.hideHeader || !props.collapsed,
5369
5559
  onToggle: toggled,
5370
5560
  children: [header, /* @__PURE__ */ jsxs("div", {
5371
- className: "mica-wb-property-fields",
5561
+ className: `mica-wb-property-fields${fileDrag ? " is-file-drag" : ""}`,
5372
5562
  style: { height: props.fill ? void 0 : props.height },
5563
+ onDragEnter: (event) => {
5564
+ if (props.disabled || !event.dataTransfer.types.includes("Files")) return;
5565
+ event.preventDefault();
5566
+ setFileDrag(true);
5567
+ },
5568
+ onDragOver: (event) => {
5569
+ if (props.disabled || !event.dataTransfer.types.includes("Files")) return;
5570
+ event.preventDefault();
5571
+ event.dataTransfer.dropEffect = "copy";
5572
+ },
5573
+ onDragLeave: (event) => {
5574
+ if (event.currentTarget.contains(event.relatedTarget)) return;
5575
+ setFileDrag(false);
5576
+ },
5577
+ onDrop: () => setFileDrag(false),
5373
5578
  children: [/* @__PURE__ */ jsxs("dl", {
5374
5579
  className: "mica-wb-identity",
5375
5580
  children: [
@@ -5396,6 +5601,8 @@ function InspectorView(props) {
5396
5601
  reset: () => props.onReset(name),
5397
5602
  onError: props.onError,
5398
5603
  assets: props.assets,
5604
+ designator: props.instance?.designator,
5605
+ assetReplacement: (assetId) => props.exclusiveAsset?.(name, assetId),
5399
5606
  validate: (value, signal) => props.onValidate(name, value, signal),
5400
5607
  commitResource: (value, signal) => props.onCommit(name, value, signal),
5401
5608
  openProject: props.onOpenProject
@@ -5817,6 +6024,12 @@ function FloatingView({ ui, handle }) {
5817
6024
  }
5818
6025
  //#endregion
5819
6026
  //#region src/fused/parts/workbench-part.tsx
6027
+ /** 属性值引用的资产 UUID;其它形态返回 `undefined`。 */
6028
+ function referencedAsset(value) {
6029
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return void 0;
6030
+ const candidate = value;
6031
+ return candidate.mode === "asset" && typeof candidate.assetId === "string" ? candidate.assetId : void 0;
6032
+ }
5820
6033
  function WorkbenchPart({ ui }) {
5821
6034
  const frame = useSyncExternalStore((callback) => {
5822
6035
  const subscription = ui.onDidUpdate(callback);
@@ -5889,6 +6102,18 @@ function WorkbenchPart({ ui }) {
5889
6102
  value
5890
6103
  }));
5891
6104
  };
6105
+ /**
6106
+ * 上传只覆盖当前属性独占的资产。文档里还有其他属性引用同一个 UUID 时返回 `undefined`,
6107
+ * 宿主会另建资产,别的器件继续用原文件。
6108
+ */
6109
+ const exclusiveAsset = (key, assetId) => {
6110
+ if (assetId === void 0 || single === void 0) return void 0;
6111
+ for (const instance of document.instances) for (const [name, value] of Object.entries(instance.properties ?? {})) {
6112
+ if (instance.id === single.id && name === key) continue;
6113
+ if (referencedAsset(value) === assetId) return void 0;
6114
+ }
6115
+ return assetId;
6116
+ };
5892
6117
  const propertyCollapsed = layout?.properties.collapsed ?? emptyPropertiesCollapsed;
5893
6118
  const toggleProperties = (collapsed) => {
5894
6119
  if (single === void 0 || layout === void 0) setEmptyPropertiesCollapsed(collapsed);
@@ -6071,42 +6296,50 @@ function WorkbenchPart({ ui }) {
6071
6296
  context: ui.context,
6072
6297
  onError: ui.error
6073
6298
  }),
6074
- state.busy && /* @__PURE__ */ jsx("div", {
6075
- className: "mica-wb-status",
6076
- role: "status",
6077
- children: "正在准备…"
6078
- }),
6079
- !state.busy && state.mode === "run" && state.simulationStatus === "starting" && /* @__PURE__ */ jsx("div", {
6080
- className: "mica-wb-status",
6081
- role: "status",
6082
- children: "仿真启动中…"
6083
- }),
6084
- !state.busy && state.mode === "edit" && state.readOnly && /* @__PURE__ */ jsx("div", {
6085
- className: "mica-wb-status",
6086
- role: "status",
6087
- children: "只读"
6088
- }),
6089
- !state.busy && ui.placement !== void 0 && /* @__PURE__ */ jsx("div", {
6090
- className: "mica-wb-status",
6091
- role: "status",
6092
- children: "单击放置 · 空格旋转 · Esc 取消"
6093
- }),
6094
- !state.busy && state.mode === "edit" && !state.readOnly && [
6095
- "power",
6096
- "ground",
6097
- "label"
6098
- ].includes(state.tool) && /* @__PURE__ */ jsxs("div", {
6099
- className: "mica-wb-status",
6100
- role: "status",
6101
- children: [state.tool === "label" && /* @__PURE__ */ jsxs("label", { children: [
6102
- "网标",
6103
- " ",
6104
- /* @__PURE__ */ jsx("input", {
6105
- "aria-label": "网标名称",
6106
- value: ui.labelName,
6107
- onChange: (event) => ui.setLabelName(event.target.value)
6299
+ /* @__PURE__ */ jsxs("div", {
6300
+ className: "mica-wb-canvas-status",
6301
+ children: [
6302
+ state.mode === "run" && !state.busy && state.simulationStatus !== void 0 && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(SimulationSpeed, {
6303
+ ui,
6304
+ status: state.simulationStatus
6305
+ }), state.simulationStatus === "starting" && /* @__PURE__ */ jsx("div", {
6306
+ className: "mica-wb-status",
6307
+ role: "status",
6308
+ children: "仿真启动中…"
6309
+ })] }),
6310
+ state.busy && /* @__PURE__ */ jsx("div", {
6311
+ className: "mica-wb-status",
6312
+ role: "status",
6313
+ children: "正在准备…"
6314
+ }),
6315
+ !state.busy && state.mode === "edit" && state.readOnly && /* @__PURE__ */ jsx("div", {
6316
+ className: "mica-wb-status",
6317
+ role: "status",
6318
+ children: "只读"
6319
+ }),
6320
+ !state.busy && ui.placement !== void 0 && /* @__PURE__ */ jsx("div", {
6321
+ className: "mica-wb-status",
6322
+ role: "status",
6323
+ children: "单击放置 · 空格旋转 · Esc 取消"
6324
+ }),
6325
+ !state.busy && state.mode === "edit" && !state.readOnly && [
6326
+ "power",
6327
+ "ground",
6328
+ "label"
6329
+ ].includes(state.tool) && /* @__PURE__ */ jsxs("div", {
6330
+ className: "mica-wb-status",
6331
+ role: "status",
6332
+ children: [state.tool === "label" && /* @__PURE__ */ jsxs("label", { children: [
6333
+ "网标",
6334
+ " ",
6335
+ /* @__PURE__ */ jsx("input", {
6336
+ "aria-label": "网标名称",
6337
+ value: ui.labelName,
6338
+ onChange: (event) => ui.setLabelName(event.target.value)
6339
+ })
6340
+ ] }), "单击放置 · Esc 取消"]
6108
6341
  })
6109
- ] }), "单击放置 · Esc 取消"]
6342
+ ]
6110
6343
  }),
6111
6344
  ui.diagnostics.some((diagnostic) => diagnostic.severity === "error") && /* @__PURE__ */ jsxs("button", {
6112
6345
  className: "mica-wb-problem-indicator",
@@ -6157,6 +6390,7 @@ function WorkbenchPart({ ui }) {
6157
6390
  selectionCount: selection.length,
6158
6391
  disabled: state.readOnly || state.mode !== "edit" || state.busy,
6159
6392
  assets: ui.options.assetProvider,
6393
+ exclusiveAsset,
6160
6394
  programs: ui.options.programs,
6161
6395
  packagePaths: single === void 0 ? [] : ui.programPackagePaths(single.device),
6162
6396
  snapshots: single === void 0 ? void 0 : ui.programSnapshot(single.id),
@@ -6219,6 +6453,25 @@ function WorkbenchPart({ ui }) {
6219
6453
  ]
6220
6454
  });
6221
6455
  }
6456
+ /**
6457
+ * 画布左下角的实时率读数。只订阅速度事件,运行中每半秒更新一次,
6458
+ * 不会带动整个工作台重渲染。
6459
+ */
6460
+ function SimulationSpeed(props) {
6461
+ const speed = useSyncExternalStore((callback) => {
6462
+ const subscription = props.ui.onDidChangeSimulationSpeed(callback);
6463
+ return () => subscription.dispose();
6464
+ }, () => props.ui.simulationSpeed());
6465
+ const text = props.status === "starting" ? "—%" : speed === void 0 ? "已暂停" : `${speed}%`;
6466
+ return /* @__PURE__ */ jsxs("div", {
6467
+ className: "mica-wb-simulation-speed",
6468
+ role: "status",
6469
+ "aria-live": "off",
6470
+ "aria-label": `仿真速度 ${text}`,
6471
+ title: speed === void 0 ? "仿真速度:暂无数据" : `仿真速度为现实时间的 ${speed}%`,
6472
+ children: [/* @__PURE__ */ jsx(Icon, { name: "dashboard" }), /* @__PURE__ */ jsx("span", { children: text })]
6473
+ });
6474
+ }
6222
6475
  //#endregion
6223
6476
  //#region src/fused/workbench.tsx
6224
6477
  function errorMessage(error) {
@@ -7092,6 +7345,8 @@ async function createWorkbench(options) {
7092
7345
  changeViewLayout,
7093
7346
  contributions: registry,
7094
7347
  onDidUpdate: uiChanged.event,
7348
+ simulationSpeed: () => simulation.speed,
7349
+ onDidChangeSimulationSpeed: simulation.onDidChangeSpeed,
7095
7350
  get devices() {
7096
7351
  return documentSession.getDevices().getLoadedDevices();
7097
7352
  },