create-songloft-plugin 2.13.2 → 2.13.3

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.
@@ -0,0 +1,35 @@
1
+ <script setup>
2
+ // **单一 HTML 实现**:不走 <flutter-cupertino-switch> 双分支,统一用宿主 components.css
3
+ // 的 `.switch` 结构,与主程序 M3 Switch 一致。
4
+ //
5
+ // 采用宿主类的好处:track/thumb 是标准 <span> + `var(--md-*)`,CSS 变量在 WebF 下正常
6
+ // 渲染、自动跟随主题,无需任何 JS 读色(cupertino switch 的主色只认字面 hex,反而要
7
+ // 用 getColorScheme() 读色再喂进去)。
8
+ //
9
+ // ⚠️ 宿主 `.switch` 三条写法(display:inline-block + flex-shrink:0、把手 translateX、
10
+ // 选中态 `~` 兄弟选择器)由宿主 CSS 提供,勿在插件侧改。
11
+ const props = defineProps({
12
+ modelValue: { type: Boolean, default: false },
13
+ disabled: { type: Boolean, default: false },
14
+ ariaLabel: { type: String, default: '' },
15
+ });
16
+ const emit = defineEmits(['update:modelValue']);
17
+
18
+ function onChange(e) {
19
+ emit('update:modelValue', e.target.checked);
20
+ }
21
+ </script>
22
+
23
+ <template>
24
+ <label class="switch">
25
+ <input
26
+ type="checkbox"
27
+ :checked="modelValue"
28
+ :disabled="disabled"
29
+ :aria-label="ariaLabel || undefined"
30
+ @change="onChange"
31
+ />
32
+ <span class="switch-track"></span>
33
+ <span class="switch-thumb"></span>
34
+ </label>
35
+ </template>
@@ -0,0 +1,41 @@
1
+ import { watchEffect } from 'vue';
2
+
3
+ /**
4
+ * 把一组值**命令式**地写到 webf-ui 原生元素的 JS 属性上。
5
+ *
6
+ * ── 为什么不直接在模板里 `:checked="x"` ──────────────────────────────────────
7
+ *
8
+ * webf_cupertino_ui 的同一个逻辑属性有**两个语义不同的入口**:
9
+ *
10
+ * HTML 属性:setter 认字符串('true' / '')
11
+ * JS 属性: setter 认真布尔(value === true)
12
+ *
13
+ * 也就是说:字符串 'true' 走 JS 属性会变成 **false**(Dart 里 'true' == true 为假)。
14
+ * 而 Vue 3 对自定义元素是**启发式**决定走 prop 还是 attr 的(大致看 `key in el`),
15
+ * 我们没法在插件侧确定它选了哪条 —— 选错就是「开关点了没反应」这种无声故障。
16
+ *
17
+ * 所以布尔类属性一律绕开模板绑定,直接赋 JS 属性并传**真布尔**,行为确定。
18
+ * 字符串类属性(val / placeholder / type 等)两条路都会 toString(),模板绑定是安全的。
19
+ *
20
+ * @param {import('vue').Ref<HTMLElement|null>} elRef 目标元素的模板 ref
21
+ * @param {() => Record<string, unknown>} getter 每次依赖变化时返回要写入的属性表
22
+ */
23
+ export function bindNativeProps(elRef, getter) {
24
+ watchEffect(
25
+ () => {
26
+ const el = elRef.value;
27
+ if (!el) return;
28
+ const props = getter();
29
+ for (const key of Object.keys(props)) {
30
+ try {
31
+ el[key] = props[key];
32
+ } catch (e) {
33
+ // 元素没注册上时是空盒子,其上没有这些属性。宁可这一个属性不生效,
34
+ // 也不要抛异常打断整个渲染。
35
+ }
36
+ }
37
+ },
38
+ // flush:'post' —— 必须等 DOM 更新完、元素真的挂上去了再写属性。
39
+ { flush: 'post' },
40
+ );
41
+ }
@@ -0,0 +1,23 @@
1
+ import { ref } from 'vue';
2
+
3
+ /**
4
+ * 「当前展开的是哪个下拉」——**所有 SlSelect 实例共享的一份**状态。
5
+ *
6
+ * ── 为什么必须放在独立模块里 ────────────────────────────────────────────────
7
+ *
8
+ * 因为 **`<script setup>` 的顶层变量不是模块级的**。整个 `<script setup>` 体会被编译进
9
+ * 组件的 `setup()` 函数,所以在那里写 `const openToken = ref(null)` 会**每个实例各建一份**
10
+ * —— 多个下拉各有各的 token,互斥完全不生效。放到普通 `.js` 模块里,模块只求值一次,
11
+ * 作用域语义没有任何歧义。
12
+ *
13
+ * 用法:每个实例领一个 `nextSelectToken()`,展开时把 `openToken` 赋成自己的 token。
14
+ * 「展开自己」和「收起别人」因此是同一个动作。
15
+ */
16
+ export const openToken = ref(null);
17
+
18
+ let seq = 0;
19
+
20
+ /** 领取一个实例身份。用自增整数(QuickJS 侧没必要多依赖 Symbol,整数已足够唯一)。 */
21
+ export function nextSelectToken() {
22
+ return ++seq;
23
+ }
@@ -0,0 +1,88 @@
1
+ import { defineConfig } from 'vite';
2
+ import vue from '@vitejs/plugin-vue';
3
+
4
+ // WebF 插件前端构建配置。
5
+ //
6
+ // 产物直接写进 ../static,由 @songloft/plugin-builder 的 frontend/ 钩子调用
7
+ // (build 时先 `npm install`(仅当 node_modules 缺失)再 `npm run build`),
8
+ // 然后 builder 把 ../static 拷进 build/static 继续处理。
9
+ //
10
+ // ⚠️ 三条不能改的约束,都是 builder 与 WebF 的硬要求:
11
+ //
12
+ // ① **必须只产出一个 JS 文件,且文件名恰好是 js/app.js**。
13
+ // builder 会把 build/static/js/app.js 重打成 IIFE,然后删掉 js/ 下其它 .js,
14
+ // 并用正则把 index.html 里 `src="static/js/app.js"` 换掉 —— 匹配不到会直接 throw。
15
+ // 所以既不能改名,也不能让 rollup 分出 chunk(故 inlineDynamicImports)。
16
+ // ② **产物文件名不要自带 hash**。builder 会给 static/ 下资源注入内容 hash 并改写引用。
17
+ // ③ **HTML 里的资源引用必须是 `static/xxx` 形式**(见下面的 html-transform)。
18
+ // WebF 不采纳 `<base href>`,插件页 URL 形如 `/api/v1/jsplugin/<entryPath>/`,
19
+ // 相对路径 `./js/app.js` 在那里解析不对。
20
+ export default defineConfig({
21
+ base: './',
22
+ plugins: [
23
+ vue({
24
+ template: {
25
+ compilerOptions: {
26
+ // webf-ui 的原生元素是**自定义标签**,Vue 默认会当成未注册组件而告警。
27
+ // 三个前缀:flutter-cupertino-*(宿主 webf_cupertino_ui)、
28
+ // webf-*(webf 内建,如 webf-list-view)、songloft-*(宿主自有元素)。
29
+ isCustomElement: (tag) =>
30
+ tag.startsWith('flutter-') ||
31
+ tag.startsWith('webf-') ||
32
+ tag.startsWith('songloft-'),
33
+ },
34
+ },
35
+ }),
36
+ {
37
+ name: 'html-transform',
38
+ apply: 'build',
39
+ transformIndexHtml(html) {
40
+ // ./xxx → static/xxx(约束 ③)
41
+ let out = html.replace(/"\.\//g, '"static/');
42
+ // 去掉 Vite 注入的 crossorigin:插件资源与页面同源,不需要 CORS 模式,
43
+ // 而 WebF 的 fetch 对该属性的处理未验证,去掉是零成本的保险。
44
+ out = out.replace(/\s+crossorigin(?==|\s|>)/g, '');
45
+ // 把 script 挪到 </body> 之前:宿主注入的 common.js 是 render-blocking 且在
46
+ // <head>,我们的脚本必须在它之后跑(window.SongloftPlugin 才存在)。
47
+ const m = out.match(/<script\b[^>]*\bsrc="static\/js\/app\.js"[^>]*><\/script>/);
48
+ if (!m) {
49
+ throw new Error(
50
+ 'html-transform: 没找到 <script src="static/js/app.js">,' +
51
+ 'builder 会因此 throw。检查 entryFileNames 是否仍是 js/app.js。',
52
+ );
53
+ }
54
+ out = out.replace(m[0], '');
55
+ out = out.replace('</body>', `${m[0]}\n </body>`);
56
+ return out;
57
+ },
58
+ },
59
+ ],
60
+ build: {
61
+ outDir: '../static',
62
+ emptyOutDir: true,
63
+ // WebF 的 CSS 支持面接近 chrome61,别让 Vite 产出更新的语法
64
+ cssTarget: 'chrome61',
65
+ rollupOptions: {
66
+ output: {
67
+ // 约束 ①:单文件 + 固定名
68
+ inlineDynamicImports: true,
69
+ entryFileNames: 'js/app.js',
70
+ assetFileNames: (assetInfo) => {
71
+ if (assetInfo.name && assetInfo.name.endsWith('.css')) {
72
+ return 'css/style.css';
73
+ }
74
+ return 'assets/[name].[ext]';
75
+ },
76
+ },
77
+ },
78
+ },
79
+ server: {
80
+ proxy: {
81
+ // 本地 vite dev 时把 API 打到后端。插件页真实运行时是同源的,不走这里。
82
+ '/api': {
83
+ target: 'http://127.0.0.1:58091',
84
+ changeOrigin: true,
85
+ },
86
+ },
87
+ },
88
+ });
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "{{entryPath}}",
3
+ "version": "0.1.0",
4
+ "description": "{{description}}",
5
+ "private": true,
6
+ "scripts": {
7
+ "dev:frontend": "cd frontend && npm run dev",
8
+ "build:frontend": "cd frontend && npm run build",
9
+ "build:plugin": "songloft-plugin build",
10
+ "build": "rm -rf dist && npm run build:frontend && npm run build:plugin",
11
+ "dev": "songloft-plugin dev",
12
+ "validate": "songloft-plugin validate",
13
+ "publish:release": "songloft-plugin publish"
14
+ },
15
+ "devDependencies": {
16
+ "@songloft/plugin-builder": "workspace:^",
17
+ "@songloft/plugin-sdk": "workspace:^",
18
+ "typescript": "^5.9.3"
19
+ }
20
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "$schema": "https://raw.githubusercontent.com/songloft-org/plugin-toolchain/main/schemas/plugin.schema.json",
3
+ "name": "{{name}}",
4
+ "version": "0.1.0",
5
+ "description": "{{description}}",
6
+ "author": "{{author}}",
7
+ "homepage": "",
8
+ "license": "Apache-2.0",
9
+ "icon": "icon.svg",
10
+ "entryPath": "{{entryPath}}",
11
+ "main": "main.js",
12
+ "minHostVersion": "2.6.0",
13
+ "renderEngine": "webf",
14
+ "permissions": {{permissions}},
15
+ "updateUrl": "",
16
+ "download_url": "",
17
+ "entryHash": "",
18
+ "zipHash": ""
19
+ }