dsh-prompt-pool 1.0.2 → 1.0.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.
- package/client.js +137 -34
- package/package.json +1 -1
package/client.js
CHANGED
|
@@ -18,13 +18,15 @@ window.__ModuleLoader__.load({
|
|
|
18
18
|
|
|
19
19
|
const NS = 'prompt-pool';
|
|
20
20
|
const STORAGE_KEY = 'dsh.prompt-pool.v1';
|
|
21
|
+
/** One pre-migration copy of the stored payload, written before the first rewrite. */
|
|
22
|
+
const BACKUP_KEY = STORAGE_KEY + '.backup';
|
|
21
23
|
/** The sidebar tool tab this plugin owns; it also names the open tab. */
|
|
22
24
|
const SIDEBAR_TAB_ID = 'prompt-pool:pool';
|
|
23
25
|
/** Native right-sidebar registry identity (DSH 0.2) and the page kind it opens. */
|
|
24
26
|
const NATIVE_TAB_ID = 'dsh-prompt-pool';
|
|
25
27
|
const NATIVE_TAB_KIND = 'prompt-pool';
|
|
26
28
|
/** Shown in the settings about card; keep in step with plugin/package.json. */
|
|
27
|
-
const PLUGIN_VERSION = '1.0.
|
|
29
|
+
const PLUGIN_VERSION = '1.0.3';
|
|
28
30
|
const PLUGIN_AUTHOR = 'GanttShen';
|
|
29
31
|
/** Replace once the public repository exists; the row hides while it is empty. */
|
|
30
32
|
const PLUGIN_REPO = 'https://github.com/Ganttfly/dsh-prompt-pool';
|
|
@@ -218,6 +220,7 @@ window.__ModuleLoader__.load({
|
|
|
218
220
|
'confirm.cancel': '再想想',
|
|
219
221
|
'status.saved': '已存入池子',
|
|
220
222
|
'status.savedPartial': '已存入池子,但这 {n} 个附件没有一起保存',
|
|
223
|
+
'status.storageFailed': '保存失败:浏览器存储不可写(可能已满)。改动只在当前页面有效。',
|
|
221
224
|
'status.nothing': '草稿是空的,先写点什么',
|
|
222
225
|
'status.emptyBody': '内容不能是空的',
|
|
223
226
|
'status.inserted': '已插入到输入框',
|
|
@@ -389,6 +392,7 @@ window.__ModuleLoader__.load({
|
|
|
389
392
|
'confirm.cancel': 'Not yet',
|
|
390
393
|
'status.saved': 'Saved to the pool',
|
|
391
394
|
'status.savedPartial': 'Saved to the pool; {n} attachment(s) were not captured',
|
|
395
|
+
'status.storageFailed': 'Could not save: browser storage is unwritable (possibly full). Changes last only for this page.',
|
|
392
396
|
'status.nothing': 'The composer is empty',
|
|
393
397
|
'status.emptyBody': 'The content cannot be empty',
|
|
394
398
|
'status.inserted': 'Inserted into the composer',
|
|
@@ -464,6 +468,12 @@ window.__ModuleLoader__.load({
|
|
|
464
468
|
};
|
|
465
469
|
}
|
|
466
470
|
|
|
471
|
+
/**
|
|
472
|
+
* Whether the last write reached localStorage. Both views surface this,
|
|
473
|
+
* because a full quota would otherwise look like a successful save.
|
|
474
|
+
*/
|
|
475
|
+
const storageState = createStore({ failed: false });
|
|
476
|
+
|
|
467
477
|
function uid() {
|
|
468
478
|
try {
|
|
469
479
|
return crypto.randomUUID();
|
|
@@ -616,6 +626,61 @@ window.__ModuleLoader__.load({
|
|
|
616
626
|
);
|
|
617
627
|
}
|
|
618
628
|
|
|
629
|
+
function deleteImageBlobs(ids) {
|
|
630
|
+
const list = (Array.isArray(ids) ? ids : []).filter((id) => typeof id === 'string' && id);
|
|
631
|
+
if (list.length === 0) return Promise.resolve(true);
|
|
632
|
+
return openImageDb().then(
|
|
633
|
+
(db) =>
|
|
634
|
+
new Promise((resolve, reject) => {
|
|
635
|
+
const tx = db.transaction(IMAGE_STORE, 'readwrite');
|
|
636
|
+
const store = tx.objectStore(IMAGE_STORE);
|
|
637
|
+
for (const id of list) store.delete(id);
|
|
638
|
+
tx.oncomplete = () => resolve(true);
|
|
639
|
+
tx.onerror = () => reject(tx.error || new Error('image-delete-failed'));
|
|
640
|
+
tx.onabort = () => reject(tx.error || new Error('image-delete-failed'));
|
|
641
|
+
}),
|
|
642
|
+
);
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/** Drop bytes the editor added but never saved; stored images stay. */
|
|
646
|
+
function discardUnsavedImages(edited, stored) {
|
|
647
|
+
const keep = new Set(((stored && stored.images) || []).map((image) => image.id));
|
|
648
|
+
const drop = ((edited && edited.images) || []).map((image) => image.id).filter((imageId) => !keep.has(imageId));
|
|
649
|
+
if (drop.length > 0) deleteImageBlobs(drop).catch(() => {});
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* Every image id the pool still points at. Duplicated items and queued
|
|
654
|
+
* copies share ids, so an id is garbage only when nothing references it.
|
|
655
|
+
*/
|
|
656
|
+
function referencedImageIds() {
|
|
657
|
+
const keep = new Set();
|
|
658
|
+
const state = pool.get();
|
|
659
|
+
for (const item of state.items) for (const image of item.images || []) keep.add(image.id);
|
|
660
|
+
for (const entry of state.tray) for (const image of entry.images || []) keep.add(image.id);
|
|
661
|
+
return keep;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* Delete blobs nothing references, once per page load. This also clears the
|
|
666
|
+
* bytes of items deleted earlier, which cannot be freed immediately because
|
|
667
|
+
* the delete offers an undo.
|
|
668
|
+
*/
|
|
669
|
+
function sweepImageBlobs() {
|
|
670
|
+
return new Promise((resolve, reject) => {
|
|
671
|
+
openImageDb().then((db) => {
|
|
672
|
+
const tx = db.transaction(IMAGE_STORE, 'readonly');
|
|
673
|
+
const request = tx.objectStore(IMAGE_STORE).getAllKeys();
|
|
674
|
+
request.onsuccess = () => resolve(request.result || []);
|
|
675
|
+
request.onerror = () => reject(request.error || new Error('image-read-failed'));
|
|
676
|
+
}, reject);
|
|
677
|
+
}).then((keys) => {
|
|
678
|
+
const keep = referencedImageIds();
|
|
679
|
+
const drop = keys.filter((key) => !keep.has(key));
|
|
680
|
+
return drop.length > 0 ? deleteImageBlobs(drop) : null;
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
|
|
619
684
|
/** A small data-URL thumbnail keeps the list rendering synchronous. */
|
|
620
685
|
function makeImageThumb(file) {
|
|
621
686
|
return new Promise((resolve) => {
|
|
@@ -765,10 +830,19 @@ window.__ModuleLoader__.load({
|
|
|
765
830
|
}
|
|
766
831
|
|
|
767
832
|
|
|
768
|
-
/**
|
|
833
|
+
/**
|
|
834
|
+
* The one read path: item → canonical group id. Older pools predate the
|
|
835
|
+
* group id and stored pinned / categoryId (`cat:<id>`) / kind instead, so
|
|
836
|
+
* map those once on load rather than dropping the user's classification.
|
|
837
|
+
*/
|
|
769
838
|
function groupOf(item) {
|
|
770
839
|
const group = item && typeof item.group === 'string' ? item.group : '';
|
|
771
|
-
|
|
840
|
+
if (group && familyOf(group)) return group;
|
|
841
|
+
const categoryId = item && typeof item.categoryId === 'string' ? item.categoryId : '';
|
|
842
|
+
if (categoryId) return 'custom:' + categoryId.replace(/^cat:/, '');
|
|
843
|
+
if (item && item.pinned === true) return BUILTIN_GROUP.oftenuse;
|
|
844
|
+
if (item && item.kind === 'snippet') return BUILTIN_GROUP.template;
|
|
845
|
+
return BUILTIN_GROUP.note;
|
|
772
846
|
}
|
|
773
847
|
|
|
774
848
|
/** The one write path: canonical group id → item. */
|
|
@@ -1157,34 +1231,25 @@ window.__ModuleLoader__.load({
|
|
|
1157
1231
|
if (!raw) {
|
|
1158
1232
|
// Brand-new install only: an existing (even empty) pool is never reseeded.
|
|
1159
1233
|
const seeded = seededPool();
|
|
1160
|
-
|
|
1161
|
-
localStorage.setItem(STORAGE_KEY, JSON.stringify(seeded));
|
|
1162
|
-
} catch (_error) {
|
|
1163
|
-
/* storage unavailable: keep the in-memory copy only */
|
|
1164
|
-
}
|
|
1234
|
+
persist(seeded);
|
|
1165
1235
|
return seeded;
|
|
1166
1236
|
}
|
|
1167
1237
|
const parsed = JSON.parse(raw);
|
|
1168
1238
|
const storedVersion = parsed && typeof parsed.version === 'number' ? parsed.version : 1;
|
|
1169
1239
|
if (storedVersion < POOL_VERSION) {
|
|
1170
|
-
//
|
|
1171
|
-
|
|
1240
|
+
// Keep the payload this build first saw: normalization is lossy, and the
|
|
1241
|
+
// backup is the only way back if an older shape maps differently.
|
|
1172
1242
|
try {
|
|
1173
|
-
localStorage.setItem(
|
|
1243
|
+
localStorage.setItem(BACKUP_KEY, raw);
|
|
1174
1244
|
} catch (_error) {
|
|
1175
|
-
/*
|
|
1245
|
+
/* the backup is best effort; the pool itself still loads */
|
|
1176
1246
|
}
|
|
1177
|
-
return reset;
|
|
1178
1247
|
}
|
|
1179
1248
|
const pool = normalizePool(parsed);
|
|
1180
1249
|
const level = Number(pool.seeded) || 0;
|
|
1181
|
-
if (level >= SEED_LEVEL) return pool;
|
|
1182
|
-
const migrated = { ...pool, items: [...pool.items, ...seedItems(pool.items, level)], seeded: SEED_LEVEL };
|
|
1183
|
-
|
|
1184
|
-
localStorage.setItem(STORAGE_KEY, JSON.stringify(migrated));
|
|
1185
|
-
} catch (_error) {
|
|
1186
|
-
/* storage unavailable: keep the in-memory copy only */
|
|
1187
|
-
}
|
|
1250
|
+
if (storedVersion >= POOL_VERSION && level >= SEED_LEVEL) return pool;
|
|
1251
|
+
const migrated = level >= SEED_LEVEL ? pool : { ...pool, items: [...pool.items, ...seedItems(pool.items, level)], seeded: SEED_LEVEL };
|
|
1252
|
+
persist(migrated);
|
|
1188
1253
|
return migrated;
|
|
1189
1254
|
} catch (_error) {
|
|
1190
1255
|
return emptyPool();
|
|
@@ -1217,6 +1282,14 @@ window.__ModuleLoader__.load({
|
|
|
1217
1282
|
return window[POOL_SINGLETON];
|
|
1218
1283
|
})();
|
|
1219
1284
|
|
|
1285
|
+
// Clear orphaned image bytes once the pool is readable; the delay keeps the
|
|
1286
|
+
// first paint cheap and lets another window finish its own load.
|
|
1287
|
+
if (typeof window !== 'undefined' && typeof indexedDB !== 'undefined' && typeof window.setTimeout === 'function') {
|
|
1288
|
+
window.setTimeout(() => {
|
|
1289
|
+
sweepImageBlobs().catch(() => {});
|
|
1290
|
+
}, 5000);
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1220
1293
|
const ui = createStore({ open: false });
|
|
1221
1294
|
|
|
1222
1295
|
/**
|
|
@@ -1343,15 +1416,19 @@ window.__ModuleLoader__.load({
|
|
|
1343
1416
|
|
|
1344
1417
|
const bridge = (() => {
|
|
1345
1418
|
const create = () => {
|
|
1346
|
-
|
|
1419
|
+
const handlers = [];
|
|
1347
1420
|
return {
|
|
1348
1421
|
listen(fn) {
|
|
1349
|
-
|
|
1422
|
+
handlers.push(fn);
|
|
1350
1423
|
return () => {
|
|
1351
|
-
|
|
1424
|
+
const at = handlers.lastIndexOf(fn);
|
|
1425
|
+
if (at >= 0) handlers.splice(at, 1);
|
|
1352
1426
|
};
|
|
1353
1427
|
},
|
|
1354
1428
|
request(command) {
|
|
1429
|
+
// The newest mounted panel owns the composer; an older one takes
|
|
1430
|
+
// over again when the newer unmounts.
|
|
1431
|
+
const handler = handlers[handlers.length - 1];
|
|
1355
1432
|
if (!handler) return { ok: false, reason: 'unavailable' };
|
|
1356
1433
|
try {
|
|
1357
1434
|
return handler(command) || { ok: true };
|
|
@@ -1369,8 +1446,10 @@ window.__ModuleLoader__.load({
|
|
|
1369
1446
|
function persist(next) {
|
|
1370
1447
|
try {
|
|
1371
1448
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(next));
|
|
1449
|
+
if (storageState.get().failed) storageState.set({ failed: false });
|
|
1372
1450
|
} catch (_error) {
|
|
1373
|
-
|
|
1451
|
+
// The in-memory copy stays usable, but both views now say it is not saved.
|
|
1452
|
+
if (!storageState.get().failed) storageState.set({ failed: true });
|
|
1374
1453
|
}
|
|
1375
1454
|
}
|
|
1376
1455
|
|
|
@@ -1397,10 +1476,21 @@ window.__ModuleLoader__.load({
|
|
|
1397
1476
|
});
|
|
1398
1477
|
},
|
|
1399
1478
|
patch(id, fields) {
|
|
1479
|
+
const before = pool.get().items.find((item) => item.id === id);
|
|
1400
1480
|
mutate((state) => ({
|
|
1401
1481
|
...state,
|
|
1402
1482
|
items: state.items.map((item) => (item.id === id ? { ...item, ...fields, updatedAt: Date.now() } : item)),
|
|
1403
1483
|
}));
|
|
1484
|
+
// Saving is the point of no return for an image removed in the editor.
|
|
1485
|
+
// A duplicate or a queued copy may share the id, so drop it only when
|
|
1486
|
+
// nothing else points at it any more.
|
|
1487
|
+
if (before && Array.isArray(fields.images)) {
|
|
1488
|
+
const keep = new Set(fields.images.map((image) => image.id));
|
|
1489
|
+
const drop = (before.images || []).map((image) => image.id).filter((imageId) => !keep.has(imageId));
|
|
1490
|
+
const used = referencedImageIds();
|
|
1491
|
+
const garbage = drop.filter((imageId) => !used.has(imageId));
|
|
1492
|
+
if (garbage.length > 0) deleteImageBlobs(garbage).catch(() => {});
|
|
1493
|
+
}
|
|
1404
1494
|
},
|
|
1405
1495
|
|
|
1406
1496
|
addCategory(name) {
|
|
@@ -1522,6 +1612,10 @@ window.__ModuleLoader__.load({
|
|
|
1522
1612
|
return useSyncExternalStore(pool.subscribe, pool.get, pool.get);
|
|
1523
1613
|
}
|
|
1524
1614
|
|
|
1615
|
+
function useStorageState() {
|
|
1616
|
+
return useSyncExternalStore(storageState.subscribe, storageState.get, storageState.get);
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1525
1619
|
function useUiState() {
|
|
1526
1620
|
return useSyncExternalStore(ui.subscribe, ui.get, ui.get);
|
|
1527
1621
|
}
|
|
@@ -2048,6 +2142,7 @@ window.__ModuleLoader__.load({
|
|
|
2048
2142
|
function PromptPoolPage(props) {
|
|
2049
2143
|
useLocaleTick();
|
|
2050
2144
|
const state = usePoolState();
|
|
2145
|
+
const storage = useStorageState();
|
|
2051
2146
|
const queueCollapsed = state.settings && state.settings.queueCollapsed === true;
|
|
2052
2147
|
const workspaces = useWorkspaceDir();
|
|
2053
2148
|
const sessionHint = useSessionDir().sessionId;
|
|
@@ -2087,6 +2182,8 @@ window.__ModuleLoader__.load({
|
|
|
2087
2182
|
statusTimer.current = window.setTimeout(() => setStatus(null), 5000);
|
|
2088
2183
|
}, []);
|
|
2089
2184
|
useEffect(() => () => window.clearTimeout(statusTimer.current), []);
|
|
2185
|
+
// A failed write outranks a transient toast: the change is not saved.
|
|
2186
|
+
const notice = status || (storage.failed ? { kind: 'error', text: translate('status.storageFailed') } : null);
|
|
2090
2187
|
const trayRef = useRef(null);
|
|
2091
2188
|
const trayCount = state.tray.length;
|
|
2092
2189
|
const lastTrayCount = useRef(trayCount);
|
|
@@ -2314,6 +2411,7 @@ window.__ModuleLoader__.load({
|
|
|
2314
2411
|
setEditing(null);
|
|
2315
2412
|
if (!current) return;
|
|
2316
2413
|
const stored = pool.get().items.find((item) => item.id === current.id);
|
|
2414
|
+
discardUnsavedImages(current, stored);
|
|
2317
2415
|
if (stored && !stored.body.trim() && !(stored.images && stored.images.length > 0)) poolActions.remove(stored.id);
|
|
2318
2416
|
};
|
|
2319
2417
|
|
|
@@ -2585,19 +2683,19 @@ window.__ModuleLoader__.load({
|
|
|
2585
2683
|
h(
|
|
2586
2684
|
'div',
|
|
2587
2685
|
{ className: 'pp-queue-dock' },
|
|
2588
|
-
|
|
2686
|
+
notice
|
|
2589
2687
|
? h(
|
|
2590
2688
|
'div',
|
|
2591
|
-
{ className: 'pp-status pp-status-float', 'data-kind':
|
|
2592
|
-
h('span', null,
|
|
2593
|
-
|
|
2689
|
+
{ className: 'pp-status pp-status-float', 'data-kind': notice.kind || 'info' },
|
|
2690
|
+
h('span', null, notice.text),
|
|
2691
|
+
notice.undo
|
|
2594
2692
|
? h(
|
|
2595
2693
|
'button',
|
|
2596
2694
|
{
|
|
2597
2695
|
type: 'button',
|
|
2598
2696
|
className: 'pp-link',
|
|
2599
2697
|
onClick: () => {
|
|
2600
|
-
|
|
2698
|
+
notice.undo();
|
|
2601
2699
|
},
|
|
2602
2700
|
},
|
|
2603
2701
|
translate('status.undo'),
|
|
@@ -3275,6 +3373,7 @@ window.__ModuleLoader__.load({
|
|
|
3275
3373
|
function PromptPoolPanel(props) {
|
|
3276
3374
|
useLocaleTick();
|
|
3277
3375
|
const state = usePoolState();
|
|
3376
|
+
const storage = useStorageState();
|
|
3278
3377
|
const view = useUiState();
|
|
3279
3378
|
const workspaces = useWorkspaceDir();
|
|
3280
3379
|
const sessionHint = useSessionDir().sessionId;
|
|
@@ -3351,6 +3450,9 @@ window.__ModuleLoader__.load({
|
|
|
3351
3450
|
}, []);
|
|
3352
3451
|
useEffect(() => () => window.clearTimeout(statusTimer.current), []);
|
|
3353
3452
|
|
|
3453
|
+
// A failed write outranks a transient toast: the change is not saved.
|
|
3454
|
+
const notice = status || (storage.failed ? { kind: 'error', text: translate('status.storageFailed') } : null);
|
|
3455
|
+
|
|
3354
3456
|
const confirmCategoryDelete = () => {
|
|
3355
3457
|
const target = pendingCategory;
|
|
3356
3458
|
setPendingCategory(null);
|
|
@@ -3640,6 +3742,7 @@ window.__ModuleLoader__.load({
|
|
|
3640
3742
|
setEditing(null);
|
|
3641
3743
|
if (!current) return;
|
|
3642
3744
|
const stored = pool.get().items.find((item) => item.id === current.id);
|
|
3745
|
+
discardUnsavedImages(current, stored);
|
|
3643
3746
|
if (stored && !stored.body.trim() && !(stored.images && stored.images.length > 0)) poolActions.remove(stored.id);
|
|
3644
3747
|
};
|
|
3645
3748
|
|
|
@@ -4116,19 +4219,19 @@ window.__ModuleLoader__.load({
|
|
|
4116
4219
|
h(
|
|
4117
4220
|
'div',
|
|
4118
4221
|
{ className: 'pp-panel-dock' },
|
|
4119
|
-
|
|
4222
|
+
notice
|
|
4120
4223
|
? h(
|
|
4121
4224
|
'div',
|
|
4122
|
-
{ className: 'pp-status pp-status-float', 'data-kind':
|
|
4123
|
-
h('span', null,
|
|
4124
|
-
|
|
4225
|
+
{ className: 'pp-status pp-status-float', 'data-kind': notice.kind || 'info' },
|
|
4226
|
+
h('span', null, notice.text),
|
|
4227
|
+
notice.undo
|
|
4125
4228
|
? h(
|
|
4126
4229
|
'button',
|
|
4127
4230
|
{
|
|
4128
4231
|
type: 'button',
|
|
4129
4232
|
className: 'pp-link',
|
|
4130
4233
|
onClick: () => {
|
|
4131
|
-
|
|
4234
|
+
notice.undo();
|
|
4132
4235
|
},
|
|
4133
4236
|
},
|
|
4134
4237
|
translate('status.undo'),
|
package/package.json
CHANGED