@vibedev-si/dsh-island 0.1.0 → 0.3.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/contributions.js CHANGED
@@ -2,7 +2,8 @@
2
2
  * 别的 VibeDev 插件放到岛上的后台任务(`island` 服务的 `contributor()` 背后的那本账)。
3
3
  *
4
4
  * 规划见 docs/island-plan.md 第 3 节。这一块是**后台任务**:有标题、有一句说明、有真实进度
5
- * (或者没有,那就转圈)、会结束。状态项、页、通知走 `ctx.island` 的直接方法(island-items.js)。
5
+ * (或者没有,那就转圈)、会结束。同一个入口上的「最近生成」在 recent.js,通知上的「查看」在 openers.js;
6
+ * 状态项、页、通知也可以走 `ctx.island` 的直接方法(island-items.js)。
6
7
  *
7
8
  * 几条不变的规则(规划 3.7):
8
9
  * - 只收文字和数字。标题最多 40 个字、说明最多 120 个字,超出的截断;进度夹到 0–1,
@@ -57,6 +58,8 @@ function progressOf(value) {
57
58
  * @param {() => number} [options.now]
58
59
  * @param {(line: string) => void} [options.log]
59
60
  * @param {(owner: string) => boolean} [options.allowed] 这个插件现在能不能上岛;不给就都能。
61
+ * @param {(owner: string) => { title: string, icon: string }} [options.displayOf] 插件在小岛上的名字和图标;
62
+ * 不给就用插件名和通用图标。
60
63
  */
61
64
  export function createContributions(options) {
62
65
  const send = options.send
@@ -65,6 +68,7 @@ export function createContributions(options) {
65
68
  const now = options.now ?? Date.now
66
69
  const log = options.log ?? nothing
67
70
  const allowed = options.allowed ?? (() => true)
71
+ const displayOf = options.displayOf ?? ((owner) => ({ title: owner, icon: 'box' }))
68
72
  /** 插件名 → (任务 id → 任务)。 */
69
73
  const owners = new Map()
70
74
  /** 已经说过一次「不收」的插件名,免得日志被刷满。 */
@@ -88,12 +92,18 @@ export function createContributions(options) {
88
92
  /** 把这条任务现在的样子送到岛上;和上一次送的一样就不送,太密就等一会儿只送最后一份。 */
89
93
  const flush = (task) => {
90
94
  if (task.gone || !allowed(task.owner)) return
95
+ const display = displayOf(task.owner)
91
96
  const payload = {
92
97
  id: task.wireId,
93
98
  kind: 'task',
99
+ owner: task.owner,
100
+ source: display.title,
101
+ icon: display.icon,
94
102
  title: task.title,
95
103
  detail: task.detail ?? null,
96
104
  progress: task.progress,
105
+ // 结束了的任务说明是走完了还是失败了(小岛据此画成「已完成」或「失败」);还在进行的不带。
106
+ ...(task.state === undefined ? {} : { state: task.state }),
97
107
  }
98
108
  const serialized = JSON.stringify(payload)
99
109
  if (serialized === task.shown) return
@@ -149,6 +159,7 @@ export function createContributions(options) {
149
159
  applyPatch(task, patch)
150
160
  const failed = isRecord(patch) && patch.state === 'failed'
151
161
  const stated = isRecord(patch) && 'progress' in patch
162
+ task.state = failed ? 'failed' : 'done'
152
163
  // 结束了的任务不再转圈:走完的画满;没走完的停在原来的进度上,本来就不知道进度的画成空的。
153
164
  if (!stated) task.progress = failed ? (task.progress ?? 0) : 1
154
165
  flush(task)
@@ -205,6 +216,7 @@ export function createContributions(options) {
205
216
  // 结束后还留着的那几秒里又开始了:按新任务算。
206
217
  stopTimer(task, 'linger')
207
218
  task.finished = false
219
+ task.state = undefined
208
220
  }
209
221
  applyPatch(task, { subtitle: undefined, progress: null, ...spec })
210
222
  flush(task)
@@ -0,0 +1,68 @@
1
+ /**
2
+ * 小岛上怎么称呼一个放东西的插件:名字和图标。
3
+ *
4
+ * 插件拿入口时可以自己报:`ctx.island.contributor('dsh-film', { title: '影视工作台', icon: 'film' })`。
5
+ * 没报的时候,认得的 VibeDev 插件用这里的默认值(老版本的影视工作台还不会报),其余的用包名和一个通用图标。
6
+ * 图标只能从下面几种里挑:小岛不画插件给的图,也不放插件的 HTML。
7
+ */
8
+
9
+ /** 小岛认得的图标。 */
10
+ export const ICONS = Object.freeze(['film', 'image', 'music', 'sparkles', 'box'])
11
+ /** 名字最多这么多个字。 */
12
+ export const DISPLAY_TITLE_MAX = 20
13
+
14
+ /** VibeDev 自己的插件:还没报名字的版本也显示得像样。 */
15
+ export const KNOWN_CONTRIBUTORS = Object.freeze({
16
+ 'dsh-film': Object.freeze({ title: '影视工作台', icon: 'film' }),
17
+ })
18
+
19
+ /** 去掉多余空白,截到 `max` 个字(截过的末尾是省略号);不是字符串或只有空白时是 undefined。 */
20
+ function cut(value, max) {
21
+ if (typeof value !== 'string') return undefined
22
+ const text = value.replace(/\s+/g, ' ').trim()
23
+ if (text === '') return undefined
24
+ const letters = Array.from(text)
25
+ return letters.length <= max ? text : `${letters.slice(0, max - 1).join('')}…`
26
+ }
27
+
28
+ /**
29
+ * 插件报上来的名字和图标里能用的部分。
30
+ * @param {unknown} input `{ title?, icon? }`
31
+ * @returns {{ title?: string, icon?: string }}
32
+ */
33
+ export function displayInput(input) {
34
+ if (input === null || typeof input !== 'object') return {}
35
+ const title = cut(input.title, DISPLAY_TITLE_MAX)
36
+ const icon = ICONS.includes(input.icon) ? input.icon : undefined
37
+ return { ...(title === undefined ? {} : { title }), ...(icon === undefined ? {} : { icon }) }
38
+ }
39
+
40
+ /** 每个插件在小岛上的名字和图标。 */
41
+ export function createDisplays() {
42
+ /** 插件名 → 它自己报的。 */
43
+ const declared = new Map()
44
+ return {
45
+ /**
46
+ * 插件报上名字和图标。返回和之前比有没有变(变了的话,已经在岛上的东西要重发一遍)。
47
+ * @param {string} owner
48
+ * @param {unknown} input
49
+ */
50
+ declare(owner, input) {
51
+ const next = displayInput(input)
52
+ if (Object.keys(next).length === 0) return false
53
+ const before = JSON.stringify(declared.get(owner) ?? {})
54
+ declared.set(owner, next)
55
+ return JSON.stringify(next) !== before
56
+ },
57
+ /**
58
+ * 这个插件在小岛上的名字和图标。
59
+ * @param {string} owner
60
+ * @returns {{ title: string, icon: string }}
61
+ */
62
+ of(owner) {
63
+ const own = declared.get(owner) ?? {}
64
+ const known = KNOWN_CONTRIBUTORS[owner] ?? {}
65
+ return { title: own.title ?? known.title ?? owner, icon: own.icon ?? known.icon ?? 'box' }
66
+ },
67
+ }
68
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * `ctx.island.contributor(名字)` 交给插件的那个对象:后台任务、最近生成、通知,合在一个入口上。
3
+ *
4
+ * 各自的规则在各自的模块里:任务在 contributions.js,最近生成在 recent.js,通知在 island-items.js,
5
+ * 通知上的「查看」在 openers.js。这里只是把它们按插件名接起来。
6
+ */
7
+
8
+ const refuse = () => false
9
+ const idleJob = Object.freeze({ update: () => undefined, finish: () => undefined, remove: () => undefined })
10
+
11
+ /** 名字不能用时给的入口:什么都不做。 */
12
+ export const IDLE_ENTRY = Object.freeze({
13
+ task: () => idleJob,
14
+ recent: refuse,
15
+ forget: refuse,
16
+ notify: refuse,
17
+ clear: () => undefined,
18
+ })
19
+
20
+ /**
21
+ * @param {string} name 插件名
22
+ * @param {object} books
23
+ * @param {{ contributor(name: string): { task: Function, clear(): void } }} books.tasks contributions.js
24
+ * @param {{ put: Function, forget: Function, clear(name: string): void }} books.recent recent.js
25
+ * @param {{ notify(name: string, input: unknown, openId?: string): boolean }} books.items island-items.js
26
+ * @param {{ register(name: string, open: () => void): string, clear(name: string): void }} books.openers openers.js
27
+ */
28
+ export function contributorEntry(name, { tasks, recent, items, openers }) {
29
+ if (typeof name !== 'string' || name.trim() === '') return IDLE_ENTRY
30
+ const own = tasks.contributor(name)
31
+ return Object.freeze({
32
+ /** 一条后台任务:`task(id, { title, subtitle?, progress? })`,返回 `update` / `finish` / `remove`。 */
33
+ task: (id, spec) => own.task(id, spec),
34
+ /** 一条最近生成:`recent(id, { title, subtitle?, media?, thumbnail?, at?, open? })`。返回收下了没有。 */
35
+ recent: (id, spec) => recent.put(name, id, spec),
36
+ /** 撤下一条最近生成。 */
37
+ forget: (id) => recent.forget(name, id),
38
+ /** 一条会自己消失的通知:`notify({ title, body?, open? })`;有 `open` 时带「查看」。一分钟最多 3 条。 */
39
+ notify(spec) {
40
+ const open = typeof spec?.open === 'function' ? spec.open : undefined
41
+ return items.notify(name, spec, open === undefined ? undefined : openers.register(name, open))
42
+ },
43
+ /** 这个插件的任务和最近生成全部撤下。 */
44
+ clear() {
45
+ own.clear()
46
+ recent.clear(name)
47
+ },
48
+ })
49
+ }
package/executable.js CHANGED
@@ -1,28 +1,28 @@
1
1
  /**
2
2
  * 岛屿程序在哪。
3
3
  *
4
- * 顺序:设置里指定的路径 › 随插件一起装上的(从 npm 装的包自带,见 native/scripts/pack-plugin.mjs)
5
- * › `%LOCALAPPDATA%\VibeDev\island`(开发时手动拷过去的那一份)。随包的排在前面:从 npm 装过之后,
6
- * 机器上旧的手动拷贝不会顶替新版本。开发用的插件目录里没有随包的程序,所以开发时照旧用手动拷的那份。
4
+ * 顺序:设置里指定的路径 › 随插件一起装上的(从 npm 装的包自带,`bin/<系统>-<架构>/`,见 native/scripts/pack-plugin.mjs)
5
+ * › 开发时手动放的那一份(Windows 是 `%LOCALAPPDATA%\VibeDev\island`,macOS 是
6
+ * `~/Library/Application Support/VibeDev/island`,见 platform.js)。随包的排在前面:从 npm 装过之后,
7
+ * 机器上旧的手动拷贝不会顶替新版本。开发用的插件目录里没有随包的程序,所以开发时照旧用手动放的那份。
7
8
  *
8
- * 岛屿只有 Windows 版:别的系统上没有候选,宿主据此说明「只支持 Windows」。
9
+ * 岛屿有 Windows 版;macOS 版在做(docs/macos-port.md)。别的系统上没有候选,宿主据此说明不支持。
9
10
  */
10
11
  import { join } from 'node:path'
11
-
12
- /** 随包的岛屿程序相对插件目录的位置。打包脚本把编译好的程序放到这里。 */
13
- export const BUNDLED_EXECUTABLE = Object.freeze(['bin', 'win32-x64', 'vibedev-island.exe'])
12
+ import { bundledProgram } from './platform.js'
14
13
 
15
14
  /**
16
15
  * 按顺序该去哪几个地方找岛屿程序。
17
16
  *
18
- * @param {{ configured?: unknown, platform: string, pluginDir: string, localAppData: string, appDirName: string }} where
19
- * @returns {string[]} 候选路径;设置里指定了就只有那一个,不是 Windows 时是空的
17
+ * @param {{ configured?: unknown, platform: string, arch: string, pluginDir: string, devDir: string }} where
18
+ * @returns {string[]} 候选路径;设置里指定了就只有那一个,这个系统没有岛屿程序时是空的
20
19
  */
21
- export function executableCandidates({ configured, platform, pluginDir, localAppData, appDirName }) {
20
+ export function executableCandidates({ configured, platform, arch, pluginDir, devDir }) {
22
21
  const chosen = typeof configured === 'string' ? configured.trim() : ''
23
22
  if (chosen !== '') return [chosen]
24
- if (platform !== 'win32') return []
25
- return [join(pluginDir, ...BUNDLED_EXECUTABLE), join(localAppData, appDirName, 'island', 'vibedev-island.exe')]
23
+ const bundled = bundledProgram({ platform, arch })
24
+ if (bundled === undefined) return []
25
+ return [join(pluginDir, ...bundled), join(devDir, bundled[bundled.length - 1])]
26
26
  }
27
27
 
28
28
  /**
@@ -32,6 +32,8 @@ export function executableCandidates({ configured, platform, pluginDir, localApp
32
32
  * @param {string[]} candidates 找过的地方
33
33
  */
34
34
  export function missingExecutableMessage(platform, candidates) {
35
- if (platform !== 'win32' && candidates.length === 0) return 'VibeDev 岛屿只支持 Windows。'
36
- return `没有找到岛屿程序(找过:${candidates.join(';')})。重新安装插件试试。`
35
+ if (candidates.length === 0) return 'VibeDev 岛屿不支持这个系统。'
36
+ const where = candidates.join(';')
37
+ if (platform === 'darwin') return `这个版本的插件里没有 macOS 的岛屿程序(找过:${where})。`
38
+ return `没有找到岛屿程序(找过:${where})。重新安装插件试试。`
37
39
  }
package/features.js ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * 这台机器上的小岛有哪些要靠系统接口的功能(岛屿程序的 `src-tauri/src/features.rs`,握手时报上来)。
3
+ * 插件页据此不显示没有的设置:没有的功能不放开关。
4
+ *
5
+ * 岛屿没在运行、或者是不报这一项的旧版时,按系统猜:旧版岛屿只有 Windows 版,Windows 上全有;
6
+ * 别的系统上只有不靠系统接口的 QQ 邮箱。
7
+ */
8
+
9
+ /** Windows 版岛屿的全部功能,和 features.rs 的 Windows 列表一致。 */
10
+ export const WINDOWS_FEATURES = Object.freeze([
11
+ 'shortcuts',
12
+ 'clipboard',
13
+ 'screenshot',
14
+ 'media',
15
+ 'sound',
16
+ 'fullscreen',
17
+ 'readOtherApps',
18
+ 'wechat',
19
+ 'qqNotifications',
20
+ 'qqMail',
21
+ ])
22
+
23
+ /** 别的系统上没有握手结果时的猜测,和 features.rs 的非 Windows 列表一致。 */
24
+ export const OTHER_FEATURES = Object.freeze(['qqMail'])
25
+
26
+ /**
27
+ * @param {{ reported: unknown, platform: string }} input `reported` 是握手结果里的 `features`
28
+ * @returns {string[]}
29
+ */
30
+ export function featuresOf({ reported, platform }) {
31
+ if (Array.isArray(reported)) return reported.filter((feature) => typeof feature === 'string')
32
+ return [...(platform === 'win32' ? WINDOWS_FEATURES : OTHER_FEATURES)]
33
+ }