morph-embed 0.1.0 → 0.2.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/README.md +32 -6
- package/dist/morph-embed.esm.mjs +736 -161
- package/dist/morph-embed.js +736 -161
- package/package.json +1 -1
package/dist/morph-embed.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! Morph Embed SDK v0.
|
|
1
|
+
/*! Morph Embed SDK v0.2.0 — https://usemorph.xyz — MIT */
|
|
2
2
|
|
|
3
3
|
/* ---- shared/dsl.js ---- */
|
|
4
4
|
/*
|
|
@@ -1320,151 +1320,255 @@
|
|
|
1320
1320
|
|
|
1321
1321
|
|
|
1322
1322
|
/* ---- sdk/src/storage.js ---- */
|
|
1323
|
-
/*
|
|
1324
|
-
* Morph SDK — recipe store.
|
|
1325
|
-
*
|
|
1326
|
-
*
|
|
1327
|
-
*
|
|
1328
|
-
*
|
|
1329
|
-
*
|
|
1330
|
-
*
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
'
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
function
|
|
1398
|
-
const
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
});
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
});
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1323
|
+
/*
|
|
1324
|
+
* Morph SDK — recipe store. SCHEMA v2: a recipe is a LIST OF EDITS
|
|
1325
|
+
* ({id, prompt, ts, enabled, transform}) per page path, which is what makes
|
|
1326
|
+
* per-edit undo, history, and toggling possible in the SDK UI (Nielsen #3,
|
|
1327
|
+
* user control and freedom). v1 recipes (one merged blob) migrate on read.
|
|
1328
|
+
*
|
|
1329
|
+
* Default backing is localStorage (per-browser); a vendor may pass a custom
|
|
1330
|
+
* adapter {get,set,remove} (sync or Promise) to store recipes in their own
|
|
1331
|
+
* backend so a user's generated features follow them across devices.
|
|
1332
|
+
* Storage failures NEVER throw — a broken store must never break the host page.
|
|
1333
|
+
* UMD: Node module in tests, browser global `MorphStorage` in the SDK bundle.
|
|
1334
|
+
*/
|
|
1335
|
+
(function (global, factory) {
|
|
1336
|
+
const api = factory();
|
|
1337
|
+
if (typeof module !== 'undefined' && module.exports) module.exports = api;
|
|
1338
|
+
else global.MorphStorage = api;
|
|
1339
|
+
})(typeof self !== 'undefined' ? self : this, function () {
|
|
1340
|
+
'use strict';
|
|
1341
|
+
|
|
1342
|
+
const PREFIX = 'morph:sdk:recipe:';
|
|
1343
|
+
const SCHEMA_VERSION = 2;
|
|
1344
|
+
const MAX_PROMPT_LABEL = 300;
|
|
1345
|
+
const MAX_EDITS = 20;
|
|
1346
|
+
|
|
1347
|
+
// Default adapter: window.localStorage, JSON values, fully guarded.
|
|
1348
|
+
function localAdapter() {
|
|
1349
|
+
function ls() {
|
|
1350
|
+
try {
|
|
1351
|
+
return (typeof window !== 'undefined' && window.localStorage) ? window.localStorage : null;
|
|
1352
|
+
} catch (_) { return null; } // SecurityError in sandboxed frames
|
|
1353
|
+
}
|
|
1354
|
+
return {
|
|
1355
|
+
get(key) {
|
|
1356
|
+
const s = ls();
|
|
1357
|
+
if (!s) return undefined;
|
|
1358
|
+
const raw = s.getItem(key);
|
|
1359
|
+
if (raw == null) return undefined;
|
|
1360
|
+
try { return JSON.parse(raw); } catch (_) { return undefined; }
|
|
1361
|
+
},
|
|
1362
|
+
set(key, value) {
|
|
1363
|
+
const s = ls();
|
|
1364
|
+
if (!s) return;
|
|
1365
|
+
s.setItem(key, JSON.stringify(value));
|
|
1366
|
+
},
|
|
1367
|
+
remove(key) {
|
|
1368
|
+
const s = ls();
|
|
1369
|
+
if (!s) return;
|
|
1370
|
+
s.removeItem(key);
|
|
1371
|
+
},
|
|
1372
|
+
};
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
function genId() {
|
|
1376
|
+
return 'e_' + Date.now().toString(36) + Math.random().toString(36).slice(2, 8);
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
// mergeTransforms concats; recipes must UNION. Drop structurally identical
|
|
1380
|
+
// ops (first occurrence wins) so repeating a prompt can't bloat the recipe.
|
|
1381
|
+
function dedupeOps(transform) {
|
|
1382
|
+
if (!transform || !Array.isArray(transform.ops)) return transform;
|
|
1383
|
+
const seen = new Set();
|
|
1384
|
+
const ops = transform.ops.filter((op) => {
|
|
1385
|
+
let key;
|
|
1386
|
+
try { key = JSON.stringify(op); } catch (_) { return true; }
|
|
1387
|
+
if (seen.has(key)) return false;
|
|
1388
|
+
seen.add(key);
|
|
1389
|
+
return true;
|
|
1390
|
+
});
|
|
1391
|
+
return Object.assign({}, transform, { ops });
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
// Merge the ENABLED edits, in order, into one transform (deduped). Returns
|
|
1395
|
+
// null when nothing is enabled. mergeFn is DSL.mergeTransforms when the
|
|
1396
|
+
// caller has the DSL; the concat fallback keeps this module dependency-free.
|
|
1397
|
+
function composeEdits(edits, mergeFn) {
|
|
1398
|
+
const enabled = (Array.isArray(edits) ? edits : []).filter((e) => e && e.enabled !== false && e.transform);
|
|
1399
|
+
if (!enabled.length) return null;
|
|
1400
|
+
let merged = enabled[0].transform;
|
|
1401
|
+
for (let i = 1; i < enabled.length; i++) {
|
|
1402
|
+
merged = (typeof mergeFn === 'function')
|
|
1403
|
+
? mergeFn(merged, enabled[i].transform)
|
|
1404
|
+
: Object.assign({}, merged, { ops: (merged.ops || []).concat(enabled[i].transform.ops || []) });
|
|
1405
|
+
}
|
|
1406
|
+
return dedupeOps(merged);
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
// Human label for the whole recipe: unique enabled-edit prompts, in order.
|
|
1410
|
+
function promptLabel(edits) {
|
|
1411
|
+
const seen = new Set();
|
|
1412
|
+
const parts = [];
|
|
1413
|
+
for (const e of Array.isArray(edits) ? edits : []) {
|
|
1414
|
+
if (!e || e.enabled === false) continue;
|
|
1415
|
+
const p = String(e.prompt || '').trim();
|
|
1416
|
+
const k = p.toLowerCase();
|
|
1417
|
+
if (!p || seen.has(k)) continue;
|
|
1418
|
+
seen.add(k);
|
|
1419
|
+
parts.push(p);
|
|
1420
|
+
}
|
|
1421
|
+
return parts.join(' + ').slice(0, MAX_PROMPT_LABEL);
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
// Any stored shape -> v2. v1 recipes (single merged transform) become one
|
|
1425
|
+
// edit so nothing a user saved before the redesign is lost.
|
|
1426
|
+
function migrate(raw, pathKey) {
|
|
1427
|
+
const base = { path: String(pathKey || '/'), enabled: true, schemaVersion: SCHEMA_VERSION, createdAt: (raw && raw.createdAt) || Date.now(), edits: [] };
|
|
1428
|
+
if (!raw || typeof raw !== 'object') return base;
|
|
1429
|
+
if (Array.isArray(raw.edits)) {
|
|
1430
|
+
base.edits = raw.edits.filter((e) => e && typeof e === 'object' && e.transform);
|
|
1431
|
+
return base;
|
|
1432
|
+
}
|
|
1433
|
+
if (raw.transform) {
|
|
1434
|
+
base.edits = [{
|
|
1435
|
+
id: genId(),
|
|
1436
|
+
prompt: String(raw.prompt || 'Earlier changes').slice(0, MAX_PROMPT_LABEL),
|
|
1437
|
+
ts: raw.createdAt || Date.now(),
|
|
1438
|
+
enabled: raw.enabled !== false,
|
|
1439
|
+
transform: raw.transform,
|
|
1440
|
+
}];
|
|
1441
|
+
}
|
|
1442
|
+
return base;
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
function createStore(adapter) {
|
|
1446
|
+
const backing = (adapter && typeof adapter === 'object') ? adapter : localAdapter();
|
|
1447
|
+
const keyOf = (pathKey) => PREFIX + String(pathKey || '/');
|
|
1448
|
+
|
|
1449
|
+
// Serialize read-modify-write so rapid appends can't lose edits (same
|
|
1450
|
+
// pattern as extension/src/recipes.js).
|
|
1451
|
+
let writeChain = Promise.resolve();
|
|
1452
|
+
function serialize(fn) {
|
|
1453
|
+
const run = writeChain.then(fn, fn);
|
|
1454
|
+
writeChain = run.then(() => {}, () => {});
|
|
1455
|
+
return run;
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
async function readMigrated(pathKey) {
|
|
1459
|
+
let raw;
|
|
1460
|
+
try { raw = await backing.get(keyOf(pathKey)); } catch (_) { raw = undefined; }
|
|
1461
|
+
if (!raw || typeof raw !== 'object') return null;
|
|
1462
|
+
return migrate(raw, pathKey);
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1465
|
+
async function persist(pathKey, recipe) {
|
|
1466
|
+
await backing.set(keyOf(pathKey), recipe);
|
|
1467
|
+
return recipe;
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
// Back-compat read shape: composed transform + merged prompt + the edits.
|
|
1471
|
+
async function loadRecipe(pathKey) {
|
|
1472
|
+
try {
|
|
1473
|
+
const r = await readMigrated(pathKey);
|
|
1474
|
+
if (!r) return null;
|
|
1475
|
+
return {
|
|
1476
|
+
path: r.path,
|
|
1477
|
+
enabled: r.edits.some((e) => e.enabled !== false),
|
|
1478
|
+
prompt: promptLabel(r.edits),
|
|
1479
|
+
transform: composeEdits(r.edits),
|
|
1480
|
+
edits: r.edits,
|
|
1481
|
+
schemaVersion: SCHEMA_VERSION,
|
|
1482
|
+
createdAt: r.createdAt,
|
|
1483
|
+
};
|
|
1484
|
+
} catch (_) { return null; }
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
function saveRecipe(pathKey, recipe) {
|
|
1488
|
+
return serialize(async () => {
|
|
1489
|
+
try {
|
|
1490
|
+
const merged = Object.assign(
|
|
1491
|
+
{ path: String(pathKey || '/'), enabled: true, createdAt: Date.now() },
|
|
1492
|
+
recipe,
|
|
1493
|
+
{ schemaVersion: SCHEMA_VERSION, path: String(pathKey || '/') }
|
|
1494
|
+
);
|
|
1495
|
+
await persist(pathKey, merged);
|
|
1496
|
+
return merged;
|
|
1497
|
+
} catch (_) { return null; }
|
|
1498
|
+
});
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
// Add one edit (the unit of undo/history). Returns the new edit or null.
|
|
1502
|
+
function appendEdit(pathKey, transform, meta) {
|
|
1503
|
+
return serialize(async () => {
|
|
1504
|
+
try {
|
|
1505
|
+
const r = (await readMigrated(pathKey)) || migrate(null, pathKey);
|
|
1506
|
+
const edit = {
|
|
1507
|
+
id: genId(),
|
|
1508
|
+
prompt: String((meta && meta.prompt) || '').slice(0, MAX_PROMPT_LABEL),
|
|
1509
|
+
ts: Date.now(),
|
|
1510
|
+
enabled: true,
|
|
1511
|
+
transform,
|
|
1512
|
+
};
|
|
1513
|
+
r.edits.push(edit);
|
|
1514
|
+
if (r.edits.length > MAX_EDITS) r.edits.splice(0, r.edits.length - MAX_EDITS);
|
|
1515
|
+
await persist(pathKey, r);
|
|
1516
|
+
return edit;
|
|
1517
|
+
} catch (_) { return null; }
|
|
1518
|
+
});
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
async function listEdits(pathKey) {
|
|
1522
|
+
try {
|
|
1523
|
+
const r = await readMigrated(pathKey);
|
|
1524
|
+
return r ? r.edits : [];
|
|
1525
|
+
} catch (_) { return []; }
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
function setEditEnabled(pathKey, id, enabled) {
|
|
1529
|
+
return serialize(async () => {
|
|
1530
|
+
try {
|
|
1531
|
+
const r = await readMigrated(pathKey);
|
|
1532
|
+
if (!r) return false;
|
|
1533
|
+
const edit = r.edits.find((e) => e.id === id);
|
|
1534
|
+
if (!edit) return false;
|
|
1535
|
+
edit.enabled = enabled !== false;
|
|
1536
|
+
await persist(pathKey, r);
|
|
1537
|
+
return true;
|
|
1538
|
+
} catch (_) { return false; }
|
|
1539
|
+
});
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
function removeEdit(pathKey, id) {
|
|
1543
|
+
return serialize(async () => {
|
|
1544
|
+
try {
|
|
1545
|
+
const r = await readMigrated(pathKey);
|
|
1546
|
+
if (!r) return false;
|
|
1547
|
+
const i = r.edits.findIndex((e) => e.id === id);
|
|
1548
|
+
if (i < 0) return false;
|
|
1549
|
+
r.edits.splice(i, 1);
|
|
1550
|
+
await persist(pathKey, r);
|
|
1551
|
+
return true;
|
|
1552
|
+
} catch (_) { return false; }
|
|
1553
|
+
});
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
// v1 API kept for existing callers: appends as an edit under the hood.
|
|
1557
|
+
function appendTransform(pathKey, transform, meta, mergeFn) {
|
|
1558
|
+
return appendEdit(pathKey, transform, meta).then((edit) => (edit ? loadRecipe(pathKey) : null), () => null);
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
function removeRecipe(pathKey) {
|
|
1562
|
+
return serialize(async () => {
|
|
1563
|
+
try { await backing.remove(keyOf(pathKey)); } catch (_) { /* never throws */ }
|
|
1564
|
+
});
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
return { loadRecipe, saveRecipe, appendTransform, appendEdit, listEdits, setEditEnabled, removeEdit, removeRecipe };
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
return { createStore, composeEdits, SCHEMA_VERSION };
|
|
1571
|
+
});
|
|
1468
1572
|
|
|
1469
1573
|
|
|
1470
1574
|
/* ---- extension/src/context.js ---- */
|
|
@@ -2185,6 +2289,301 @@
|
|
|
2185
2289
|
})();
|
|
2186
2290
|
|
|
2187
2291
|
|
|
2292
|
+
/* ---- sdk/src/ui.js ---- */
|
|
2293
|
+
/*
|
|
2294
|
+
* Morph SDK — UI layer: launcher + edits panel + undo toast, isolated in a
|
|
2295
|
+
* closed-over Shadow DOM so host-page CSS can't leak in either direction.
|
|
2296
|
+
*
|
|
2297
|
+
* Every behavior here is deliberate, from the sprint's research pass:
|
|
2298
|
+
* - The launcher is small, corner-pinned, and NEVER auto-opens (Baymard's
|
|
2299
|
+
* live-chat testing: sticky elements that act on their own are "highly
|
|
2300
|
+
* disruptive"; everything is user-initiated).
|
|
2301
|
+
* - First-run pulse fires exactly once (persisted flag) and not at all under
|
|
2302
|
+
* prefers-reduced-motion (NN/g animation-usability).
|
|
2303
|
+
* - Edits apply immediately and the toast offers Undo afterwards — the Gmail
|
|
2304
|
+
* pattern; confirmation-first dialogs habituate and fail (NN/g).
|
|
2305
|
+
* - The panel gives per-edit visibility, toggle, and delete — HAX G17
|
|
2306
|
+
* "provide global controls", Nielsen #3 "user control and freedom".
|
|
2307
|
+
* - The launcher menu never has more than two options (Hick's law), and the
|
|
2308
|
+
* whole surface is at most two disclosure levels deep (NN/g progressive
|
|
2309
|
+
* disclosure).
|
|
2310
|
+
* UMD: Node module in tests, browser global `MorphUI` in the SDK bundle.
|
|
2311
|
+
*/
|
|
2312
|
+
(function (global, factory) {
|
|
2313
|
+
const api = factory();
|
|
2314
|
+
if (typeof module !== 'undefined' && module.exports) module.exports = api;
|
|
2315
|
+
else global.MorphUI = api;
|
|
2316
|
+
})(typeof self !== 'undefined' ? self : this, function () {
|
|
2317
|
+
'use strict';
|
|
2318
|
+
|
|
2319
|
+
const HOST_ID = 'morph-sdk-ui';
|
|
2320
|
+
const NUDGE_FLAG = 'morph:sdk:nudged';
|
|
2321
|
+
|
|
2322
|
+
const CSS = `
|
|
2323
|
+
:host { all: initial; }
|
|
2324
|
+
* { box-sizing: border-box; font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
|
|
2325
|
+
.morph-ui-root { position: fixed; z-index: 2147483600; bottom: 20px; right: 20px;
|
|
2326
|
+
display: flex; flex-direction: column; align-items: flex-end; gap: 10px; }
|
|
2327
|
+
.morph-ui-root.pos-bottom-left { right: auto; left: 20px; align-items: flex-start; }
|
|
2328
|
+
|
|
2329
|
+
.morph-fab { width: 46px; height: 46px; border-radius: 50%; border: none; cursor: pointer;
|
|
2330
|
+
background: var(--morph-accent, #111114); color: #fff; font-size: 20px; line-height: 1;
|
|
2331
|
+
box-shadow: 0 4px 18px rgba(0,0,0,.28); display: flex; align-items: center; justify-content: center;
|
|
2332
|
+
position: relative; transition: transform .12s ease; }
|
|
2333
|
+
.morph-fab:hover { transform: scale(1.06); }
|
|
2334
|
+
.morph-fab:focus-visible { outline: 2px solid #4c8dff; outline-offset: 2px; }
|
|
2335
|
+
.morph-fab.pulse { animation: morphPulse 1.6s ease-out 2; }
|
|
2336
|
+
@keyframes morphPulse {
|
|
2337
|
+
0% { box-shadow: 0 0 0 0 rgba(76,141,255,.55), 0 4px 18px rgba(0,0,0,.28); }
|
|
2338
|
+
100% { box-shadow: 0 0 0 16px rgba(76,141,255,0), 0 4px 18px rgba(0,0,0,.28); }
|
|
2339
|
+
}
|
|
2340
|
+
@media (prefers-reduced-motion: reduce) { .morph-fab.pulse { animation: none; } }
|
|
2341
|
+
|
|
2342
|
+
.morph-badge { position: absolute; top: -4px; right: -4px; min-width: 18px; height: 18px;
|
|
2343
|
+
border-radius: 9px; background: #e8554d; color: #fff; font-size: 11px; font-weight: 700;
|
|
2344
|
+
display: none; align-items: center; justify-content: center; padding: 0 5px; }
|
|
2345
|
+
.morph-badge.show { display: flex; }
|
|
2346
|
+
|
|
2347
|
+
.morph-menu { display: none; flex-direction: column; background: #fff; border-radius: 10px;
|
|
2348
|
+
box-shadow: 0 8px 30px rgba(0,0,0,.22); overflow: hidden; min-width: 200px; }
|
|
2349
|
+
.morph-menu.open { display: flex; }
|
|
2350
|
+
.morph-menu-item { border: none; background: none; text-align: left; padding: 12px 16px;
|
|
2351
|
+
font-size: 13.5px; color: #17171c; cursor: pointer; display: flex; gap: 9px; align-items: center; }
|
|
2352
|
+
.morph-menu-item:hover { background: #f2f3f7; }
|
|
2353
|
+
|
|
2354
|
+
.morph-panel { display: none; width: 320px; max-height: 420px; overflow: auto; background: #fff;
|
|
2355
|
+
border-radius: 12px; box-shadow: 0 10px 38px rgba(0,0,0,.26); padding: 14px 14px 10px; }
|
|
2356
|
+
.morph-panel.open { display: block; }
|
|
2357
|
+
.morph-panel h3 { margin: 0 0 2px; font-size: 13px; color: #17171c; }
|
|
2358
|
+
.morph-panel .morph-panel-sub { margin: 0 0 10px; font-size: 11px; color: #7a7a85; }
|
|
2359
|
+
.morph-edit-row { display: flex; align-items: center; gap: 8px; padding: 8px 4px;
|
|
2360
|
+
border-top: 1px solid #eef0f4; }
|
|
2361
|
+
.morph-edit-label { flex: 1; min-width: 0; }
|
|
2362
|
+
.morph-edit-label .p { font-size: 12.5px; color: #17171c; overflow: hidden; text-overflow: ellipsis;
|
|
2363
|
+
white-space: nowrap; }
|
|
2364
|
+
.morph-edit-label .t { font-size: 10.5px; color: #9a9aa5; }
|
|
2365
|
+
.morph-edit-row.off .morph-edit-label .p { color: #9a9aa5; text-decoration: line-through; }
|
|
2366
|
+
.morph-edit-toggle { width: 32px; height: 18px; border-radius: 9px; border: none; cursor: pointer;
|
|
2367
|
+
background: #cfd3dc; position: relative; flex: none; }
|
|
2368
|
+
.morph-edit-toggle::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px;
|
|
2369
|
+
border-radius: 50%; background: #fff; transition: left .12s ease; }
|
|
2370
|
+
.morph-edit-toggle[aria-checked="true"] { background: var(--morph-accent, #2f9e63); }
|
|
2371
|
+
.morph-edit-toggle[aria-checked="true"]::after { left: 16px; }
|
|
2372
|
+
.morph-edit-delete { border: none; background: none; color: #b9bcc6; cursor: pointer; font-size: 15px;
|
|
2373
|
+
padding: 4px; flex: none; }
|
|
2374
|
+
.morph-edit-delete:hover { color: #e8554d; }
|
|
2375
|
+
.morph-panel-empty { padding: 14px 4px 10px; font-size: 12.5px; color: #6f6f7a; line-height: 1.5; }
|
|
2376
|
+
.morph-panel-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 10px;
|
|
2377
|
+
padding-top: 8px; border-top: 1px solid #eef0f4; }
|
|
2378
|
+
.morph-panel-reset { border: none; background: none; color: #e8554d; font-size: 12px; cursor: pointer; padding: 4px; }
|
|
2379
|
+
.morph-panel-close { border: none; background: none; color: #7a7a85; font-size: 12px; cursor: pointer; padding: 4px; }
|
|
2380
|
+
|
|
2381
|
+
.morph-toast { position: fixed; left: 50%; transform: translateX(-50%) translateY(8px); bottom: 24px;
|
|
2382
|
+
display: none; align-items: center; gap: 12px; background: #17171c; color: #fff; border-radius: 10px;
|
|
2383
|
+
padding: 11px 16px; font-size: 13px; box-shadow: 0 8px 30px rgba(0,0,0,.32); opacity: 0;
|
|
2384
|
+
transition: opacity .15s ease, transform .15s ease; max-width: min(92vw, 460px); }
|
|
2385
|
+
.morph-toast.show { display: flex; opacity: 1; transform: translateX(-50%) translateY(0); }
|
|
2386
|
+
.morph-toast-action { border: none; background: none; color: #7db4ff; font-weight: 700; font-size: 13px;
|
|
2387
|
+
cursor: pointer; padding: 2px 4px; flex: none; }
|
|
2388
|
+
`;
|
|
2389
|
+
|
|
2390
|
+
function createUI(opts) {
|
|
2391
|
+
opts = opts || {};
|
|
2392
|
+
const win = opts.win;
|
|
2393
|
+
const position = opts.position === undefined ? 'bottom-right' : opts.position;
|
|
2394
|
+
const getFlag = opts.getFlag || defaultGetFlag(win);
|
|
2395
|
+
const setFlag = opts.setFlag || defaultSetFlag(win);
|
|
2396
|
+
const reducedMotion = opts.reducedMotion !== undefined ? !!opts.reducedMotion : prefersReducedMotion(win);
|
|
2397
|
+
|
|
2398
|
+
// Vendor opted out of the launcher entirely -> inert API (they drive
|
|
2399
|
+
// everything through Morph.open() and the bar).
|
|
2400
|
+
if (position === false) {
|
|
2401
|
+
const noop = () => {};
|
|
2402
|
+
return { mount: noop, setCount: noop, showToast: noop, renderPanel: noop, showPanel: noop, hidePanel: noop };
|
|
2403
|
+
}
|
|
2404
|
+
|
|
2405
|
+
let root = null, fab = null, badge = null, menu = null, panel = null, toast = null;
|
|
2406
|
+
let toastTimer = null;
|
|
2407
|
+
let handlers = { onPrimary: null, onPanel: null };
|
|
2408
|
+
let panelHandlers = null;
|
|
2409
|
+
let count = 0;
|
|
2410
|
+
|
|
2411
|
+
function el(tag, cls, text) {
|
|
2412
|
+
const n = win.document.createElement(tag);
|
|
2413
|
+
if (cls) n.className = cls;
|
|
2414
|
+
if (text !== undefined) n.textContent = text;
|
|
2415
|
+
return n;
|
|
2416
|
+
}
|
|
2417
|
+
|
|
2418
|
+
function mount(h) {
|
|
2419
|
+
handlers = h || handlers;
|
|
2420
|
+
if (win.document.getElementById(HOST_ID)) return;
|
|
2421
|
+
const host = el('div');
|
|
2422
|
+
host.id = HOST_ID;
|
|
2423
|
+
const shadow = host.attachShadow({ mode: 'open' });
|
|
2424
|
+
const style = el('style'); style.textContent = CSS;
|
|
2425
|
+
shadow.appendChild(style);
|
|
2426
|
+
|
|
2427
|
+
root = el('div', 'morph-ui-root' + (position === 'bottom-left' ? ' pos-bottom-left' : ''));
|
|
2428
|
+
if (opts.accent) root.style.setProperty('--morph-accent', String(opts.accent));
|
|
2429
|
+
|
|
2430
|
+
panel = el('div', 'morph-panel');
|
|
2431
|
+
menu = el('div', 'morph-menu');
|
|
2432
|
+
fab = el('button', 'morph-fab', '✦');
|
|
2433
|
+
fab.setAttribute('aria-label', 'Customize this page with Morph (Alt+M)');
|
|
2434
|
+
fab.setAttribute('title', 'Customize this page (Alt+M)');
|
|
2435
|
+
badge = el('span', 'morph-badge');
|
|
2436
|
+
badge.setAttribute('aria-hidden', 'true');
|
|
2437
|
+
fab.appendChild(badge);
|
|
2438
|
+
|
|
2439
|
+
toast = el('div', 'morph-toast');
|
|
2440
|
+
toast.setAttribute('role', 'status');
|
|
2441
|
+
toast.setAttribute('aria-live', 'polite');
|
|
2442
|
+
|
|
2443
|
+
fab.addEventListener('click', () => {
|
|
2444
|
+
hidePanel();
|
|
2445
|
+
if (count > 0) { menu.classList.toggle('open'); renderMenu(); return; }
|
|
2446
|
+
menu.classList.remove('open');
|
|
2447
|
+
if (handlers.onPrimary) handlers.onPrimary();
|
|
2448
|
+
});
|
|
2449
|
+
|
|
2450
|
+
// One escape hatch for everything floating (Nielsen #3).
|
|
2451
|
+
win.document.addEventListener('keydown', (e) => {
|
|
2452
|
+
if (e.key === 'Escape') { hidePanel(); menu.classList.remove('open'); }
|
|
2453
|
+
});
|
|
2454
|
+
|
|
2455
|
+
root.appendChild(panel);
|
|
2456
|
+
root.appendChild(menu);
|
|
2457
|
+
root.appendChild(fab);
|
|
2458
|
+
shadow.appendChild(root);
|
|
2459
|
+
shadow.appendChild(toast);
|
|
2460
|
+
win.document.documentElement.appendChild(host);
|
|
2461
|
+
|
|
2462
|
+
// First-run nudge: one pulse, ever, and none under reduced motion.
|
|
2463
|
+
let seen;
|
|
2464
|
+
try { seen = getFlag(NUDGE_FLAG); } catch (_) { seen = true; }
|
|
2465
|
+
if (!seen && !reducedMotion) {
|
|
2466
|
+
fab.classList.add('pulse');
|
|
2467
|
+
try { setFlag(NUDGE_FLAG, '1'); } catch (_) { /* private mode */ }
|
|
2468
|
+
} else if (!seen) {
|
|
2469
|
+
try { setFlag(NUDGE_FLAG, '1'); } catch (_) { /* private mode */ }
|
|
2470
|
+
}
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2473
|
+
function renderMenu() {
|
|
2474
|
+
if (!menu) return;
|
|
2475
|
+
menu.textContent = '';
|
|
2476
|
+
const make = el('button', 'morph-menu-item', '✦ Make a change');
|
|
2477
|
+
make.addEventListener('click', () => { menu.classList.remove('open'); if (handlers.onPrimary) handlers.onPrimary(); });
|
|
2478
|
+
const edits = el('button', 'morph-menu-item', '☰ Your edits (' + count + ')');
|
|
2479
|
+
edits.addEventListener('click', () => { menu.classList.remove('open'); if (handlers.onPanel) handlers.onPanel(); });
|
|
2480
|
+
menu.appendChild(make);
|
|
2481
|
+
menu.appendChild(edits);
|
|
2482
|
+
}
|
|
2483
|
+
|
|
2484
|
+
function setCount(n) {
|
|
2485
|
+
count = Number(n) || 0;
|
|
2486
|
+
if (!badge) return;
|
|
2487
|
+
badge.textContent = String(count);
|
|
2488
|
+
badge.classList.toggle('show', count > 0);
|
|
2489
|
+
}
|
|
2490
|
+
|
|
2491
|
+
// Act-then-undo toast (the Gmail pattern). One at a time; new replaces old.
|
|
2492
|
+
function showToast(t) {
|
|
2493
|
+
if (!toast) return;
|
|
2494
|
+
t = t || {};
|
|
2495
|
+
if (toastTimer) { clearTimeout(toastTimer); toastTimer = null; }
|
|
2496
|
+
toast.textContent = '';
|
|
2497
|
+
const txt = el('span', 'morph-toast-text', String(t.text || ''));
|
|
2498
|
+
toast.appendChild(txt);
|
|
2499
|
+
if (t.actionLabel) {
|
|
2500
|
+
const act = el('button', 'morph-toast-action', String(t.actionLabel));
|
|
2501
|
+
act.addEventListener('click', () => { hideToast(); if (t.onAction) t.onAction(); });
|
|
2502
|
+
toast.appendChild(act);
|
|
2503
|
+
}
|
|
2504
|
+
toast.classList.add('show');
|
|
2505
|
+
const ttl = typeof t.ttlMs === 'number' ? t.ttlMs : 10000;
|
|
2506
|
+
toastTimer = setTimeout(hideToast, ttl);
|
|
2507
|
+
if (toastTimer && toastTimer.unref) toastTimer.unref();
|
|
2508
|
+
}
|
|
2509
|
+
function hideToast() {
|
|
2510
|
+
if (toast) toast.classList.remove('show');
|
|
2511
|
+
if (toastTimer) { clearTimeout(toastTimer); toastTimer = null; }
|
|
2512
|
+
}
|
|
2513
|
+
|
|
2514
|
+
function fmtWhen(ts) {
|
|
2515
|
+
try {
|
|
2516
|
+
const d = new Date(ts);
|
|
2517
|
+
return d.toLocaleDateString(undefined, { month: 'short', day: 'numeric' }) + ', ' +
|
|
2518
|
+
d.toLocaleTimeString(undefined, { hour: 'numeric', minute: '2-digit' });
|
|
2519
|
+
} catch (_) { return ''; }
|
|
2520
|
+
}
|
|
2521
|
+
|
|
2522
|
+
// "Your edits on this page": visibility + per-edit control (HAX G17).
|
|
2523
|
+
function renderPanel(edits, h) {
|
|
2524
|
+
panelHandlers = h || panelHandlers || {};
|
|
2525
|
+
if (!panel) return;
|
|
2526
|
+
panel.textContent = '';
|
|
2527
|
+
panel.appendChild(el('h3', '', 'Your edits on this page'));
|
|
2528
|
+
panel.appendChild(el('p', 'morph-panel-sub', 'These re-apply every time you visit. Toggle one off to preview the page without it.'));
|
|
2529
|
+
|
|
2530
|
+
if (!edits || !edits.length) {
|
|
2531
|
+
panel.appendChild(el('div', 'morph-panel-empty',
|
|
2532
|
+
'Nothing yet. Click the ✦ button and describe a change — “make this easier to read”, “add a box totaling this table” — and it will live here.'));
|
|
2533
|
+
} else {
|
|
2534
|
+
edits.forEach((e) => {
|
|
2535
|
+
const row = el('div', 'morph-edit-row' + (e.enabled === false ? ' off' : ''));
|
|
2536
|
+
const label = el('div', 'morph-edit-label');
|
|
2537
|
+
label.appendChild(el('div', 'p', e.prompt || 'Untitled edit'));
|
|
2538
|
+
label.appendChild(el('div', 't', fmtWhen(e.ts)));
|
|
2539
|
+
const toggle = el('button', 'morph-edit-toggle');
|
|
2540
|
+
toggle.setAttribute('role', 'switch');
|
|
2541
|
+
toggle.setAttribute('aria-checked', e.enabled === false ? 'false' : 'true');
|
|
2542
|
+
toggle.setAttribute('aria-label', 'Toggle edit: ' + (e.prompt || 'untitled'));
|
|
2543
|
+
toggle.addEventListener('click', () => {
|
|
2544
|
+
if (panelHandlers.onToggle) panelHandlers.onToggle(e.id, e.enabled === false);
|
|
2545
|
+
});
|
|
2546
|
+
const del = el('button', 'morph-edit-delete', '✕');
|
|
2547
|
+
del.setAttribute('aria-label', 'Remove edit: ' + (e.prompt || 'untitled'));
|
|
2548
|
+
del.addEventListener('click', () => { if (panelHandlers.onDelete) panelHandlers.onDelete(e.id); });
|
|
2549
|
+
row.appendChild(label);
|
|
2550
|
+
row.appendChild(toggle);
|
|
2551
|
+
row.appendChild(del);
|
|
2552
|
+
panel.appendChild(row);
|
|
2553
|
+
});
|
|
2554
|
+
}
|
|
2555
|
+
|
|
2556
|
+
const foot = el('div', 'morph-panel-foot');
|
|
2557
|
+
const reset = el('button', 'morph-panel-reset', 'Reset this page');
|
|
2558
|
+
reset.addEventListener('click', () => { if (panelHandlers.onReset) panelHandlers.onReset(); });
|
|
2559
|
+
const close = el('button', 'morph-panel-close', 'Close');
|
|
2560
|
+
close.addEventListener('click', () => { hidePanel(); if (panelHandlers.onClose) panelHandlers.onClose(); });
|
|
2561
|
+
foot.appendChild(reset);
|
|
2562
|
+
foot.appendChild(close);
|
|
2563
|
+
panel.appendChild(foot);
|
|
2564
|
+
}
|
|
2565
|
+
|
|
2566
|
+
function showPanel() { if (panel) { menu && menu.classList.remove('open'); panel.classList.add('open'); } }
|
|
2567
|
+
function hidePanel() { if (panel) panel.classList.remove('open'); }
|
|
2568
|
+
|
|
2569
|
+
return { mount, setCount, showToast, renderPanel, showPanel, hidePanel };
|
|
2570
|
+
}
|
|
2571
|
+
|
|
2572
|
+
function defaultGetFlag(win) {
|
|
2573
|
+
return (k) => { try { return win.localStorage.getItem(k); } catch (_) { return '1'; } };
|
|
2574
|
+
}
|
|
2575
|
+
function defaultSetFlag(win) {
|
|
2576
|
+
return (k, v) => { try { win.localStorage.setItem(k, v); } catch (_) { /* noop */ } };
|
|
2577
|
+
}
|
|
2578
|
+
function prefersReducedMotion(win) {
|
|
2579
|
+
try { return !!(win.matchMedia && win.matchMedia('(prefers-reduced-motion: reduce)').matches); }
|
|
2580
|
+
catch (_) { return false; }
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
return { createUI };
|
|
2584
|
+
});
|
|
2585
|
+
|
|
2586
|
+
|
|
2188
2587
|
/* ---- sdk/src/embed.js ---- */
|
|
2189
2588
|
/*
|
|
2190
2589
|
* Morph SDK — embed entry. Wires the engine (MorphDSL), the vendor registry,
|
|
@@ -2209,6 +2608,56 @@
|
|
|
2209
2608
|
|
|
2210
2609
|
const DEFAULT_BACKEND = 'https://morph-api-234584917638.us-central1.run.app';
|
|
2211
2610
|
|
|
2611
|
+
// Hotkey matching on the PHYSICAL key (e.code), not the produced character:
|
|
2612
|
+
// on macOS Option acts as a character modifier (Option+M types 'µ'), so an
|
|
2613
|
+
// e.key comparison never fires there. Modifier state must match EXACTLY —
|
|
2614
|
+
// that also keeps AltGr (reported as ctrl+alt on Windows) from triggering
|
|
2615
|
+
// an alt-only shortcut while someone types accented characters.
|
|
2616
|
+
function matchesTrigger(e, trigger) {
|
|
2617
|
+
if (!e || typeof trigger !== 'string' || !trigger) return false;
|
|
2618
|
+
// IME guard: isComposing can be false on the first/last composition
|
|
2619
|
+
// keystroke while keyCode is still the spec-mandated 229 — check both.
|
|
2620
|
+
if (e.isComposing || e.keyCode === 229) return false;
|
|
2621
|
+
const parts = trigger.toLowerCase().split('+').map((s) => s.trim()).filter(Boolean);
|
|
2622
|
+
if (!parts.length) return false;
|
|
2623
|
+
const key = parts[parts.length - 1];
|
|
2624
|
+
const mods = new Set(parts.slice(0, -1));
|
|
2625
|
+
const want = {
|
|
2626
|
+
alt: mods.has('alt') || mods.has('option'),
|
|
2627
|
+
ctrl: mods.has('ctrl') || mods.has('control'),
|
|
2628
|
+
shift: mods.has('shift'),
|
|
2629
|
+
meta: mods.has('meta') || mods.has('cmd') || mods.has('command'),
|
|
2630
|
+
};
|
|
2631
|
+
if (!!e.altKey !== want.alt || !!e.ctrlKey !== want.ctrl ||
|
|
2632
|
+
!!e.shiftKey !== want.shift || !!e.metaKey !== want.meta) return false;
|
|
2633
|
+
if (typeof e.code === 'string' && e.code) {
|
|
2634
|
+
if (/^[a-z]$/.test(key)) return e.code === 'Key' + key.toUpperCase();
|
|
2635
|
+
if (/^[0-9]$/.test(key)) return e.code === 'Digit' + key;
|
|
2636
|
+
}
|
|
2637
|
+
return typeof e.key === 'string' && e.key.toLowerCase() === key;
|
|
2638
|
+
}
|
|
2639
|
+
|
|
2640
|
+
// Don't hijack keys while the user is typing in the host app (Mousetrap /
|
|
2641
|
+
// github-hotkey convention). composedPath()[0] sees the REAL target even
|
|
2642
|
+
// when the event comes from inside a shadow root (retargeting would
|
|
2643
|
+
// otherwise hide the input element from us).
|
|
2644
|
+
function isTypingTarget(e) {
|
|
2645
|
+
let t = e && e.target;
|
|
2646
|
+
try { if (e && typeof e.composedPath === 'function') t = e.composedPath()[0] || t; } catch (_) { /* keep target */ }
|
|
2647
|
+
if (!t) return false;
|
|
2648
|
+
const tag = String(t.tagName || '').toUpperCase();
|
|
2649
|
+
return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || t.isContentEditable === true;
|
|
2650
|
+
}
|
|
2651
|
+
|
|
2652
|
+
// Wait narration (NN/g response-time bands: acknowledge <100ms, actively
|
|
2653
|
+
// manage the 2-10s AI band; a moving/staged indicator triples patience).
|
|
2654
|
+
const STAGE_MESSAGES = [
|
|
2655
|
+
'Reading this page…',
|
|
2656
|
+
'Designing your change…',
|
|
2657
|
+
'Still working — bigger changes can take a few seconds…',
|
|
2658
|
+
];
|
|
2659
|
+
const STAGE_DELAYS = [1500, 6000];
|
|
2660
|
+
|
|
2212
2661
|
function createEmbed(deps) {
|
|
2213
2662
|
const config = (deps && deps.config) || {};
|
|
2214
2663
|
const win = deps.win;
|
|
@@ -2216,8 +2665,17 @@
|
|
|
2216
2665
|
const registry = deps.registry;
|
|
2217
2666
|
const store = deps.store;
|
|
2218
2667
|
const bar = deps.bar;
|
|
2668
|
+
const ui = deps.ui || null;
|
|
2219
2669
|
const extractContext = deps.extractContext;
|
|
2220
2670
|
const fetchFn = deps.fetchFn;
|
|
2671
|
+
const stageDelays = deps.stageDelays || STAGE_DELAYS;
|
|
2672
|
+
// Merge enabled edits into one transform (storage exports the canonical
|
|
2673
|
+
// deduping composer; the fallback keeps createEmbed usable without it).
|
|
2674
|
+
const compose = deps.compose || function (edits) {
|
|
2675
|
+
const on = (edits || []).filter((e) => e && e.enabled !== false && e.transform);
|
|
2676
|
+
if (!on.length) return null;
|
|
2677
|
+
return on.slice(1).reduce((m, e) => DSL.mergeTransforms(m, e.transform), on[0].transform);
|
|
2678
|
+
};
|
|
2221
2679
|
|
|
2222
2680
|
let backend = DEFAULT_BACKEND;
|
|
2223
2681
|
let currentHandle = null; // live undo handle for everything applied this session
|
|
@@ -2227,6 +2685,14 @@
|
|
|
2227
2685
|
try { return win.location.pathname || '/'; } catch (_) { return '/'; }
|
|
2228
2686
|
}
|
|
2229
2687
|
|
|
2688
|
+
async function refreshCount() {
|
|
2689
|
+
if (!ui) return;
|
|
2690
|
+
try {
|
|
2691
|
+
const edits = await store.listEdits(pathKey());
|
|
2692
|
+
ui.setCount(edits.filter((e) => e.enabled !== false).length);
|
|
2693
|
+
} catch (_) { /* badge is best-effort */ }
|
|
2694
|
+
}
|
|
2695
|
+
|
|
2230
2696
|
function init() {
|
|
2231
2697
|
if (typeof config.key !== 'string' || !config.key) {
|
|
2232
2698
|
throw new TypeError('Morph: init requires a publishable key (config.key)');
|
|
@@ -2279,19 +2745,52 @@
|
|
|
2279
2745
|
return { transform: Object.assign({}, transform, { ops }), data, failedCount: failedSources.size };
|
|
2280
2746
|
}
|
|
2281
2747
|
|
|
2748
|
+
// Replace whatever this session applied with `transform` (fresh handle).
|
|
2749
|
+
function applyFresh(transform, data) {
|
|
2750
|
+
if (currentHandle) DSL.undoTransform(currentHandle);
|
|
2751
|
+
const handle = DSL.applyTransform(transform, win.document, { groupId: 'morph-sdk', data, onAction });
|
|
2752
|
+
currentHandle = handle;
|
|
2753
|
+
activeTransform = transform;
|
|
2754
|
+
return handle;
|
|
2755
|
+
}
|
|
2756
|
+
|
|
2282
2757
|
async function applyMerged(incoming, data) {
|
|
2283
2758
|
// Stack on top of anything already applied this session (additive), so
|
|
2284
2759
|
// successive prompts accumulate instead of replacing each other.
|
|
2285
2760
|
const merged = activeTransform ? DSL.mergeTransforms(activeTransform, incoming) : incoming;
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2761
|
+
return applyFresh(merged, data);
|
|
2762
|
+
}
|
|
2763
|
+
|
|
2764
|
+
// Rebuild the applied state from the store (after per-edit remove/toggle).
|
|
2765
|
+
async function reapplyFromStore() {
|
|
2766
|
+
const edits = await store.listEdits(pathKey());
|
|
2767
|
+
const merged = compose(edits);
|
|
2768
|
+
if (!merged || !merged.ops || !merged.ops.length) {
|
|
2769
|
+
if (currentHandle) { DSL.undoTransform(currentHandle); currentHandle = null; }
|
|
2770
|
+
activeTransform = null;
|
|
2771
|
+
return;
|
|
2772
|
+
}
|
|
2773
|
+
const gated = dropUnknownSourceOps(merged);
|
|
2774
|
+
const { transform, data } = await resolveAllData(gated);
|
|
2775
|
+
if (!transform.ops.length) {
|
|
2776
|
+
if (currentHandle) { DSL.undoTransform(currentHandle); currentHandle = null; }
|
|
2777
|
+
activeTransform = null;
|
|
2778
|
+
return;
|
|
2779
|
+
}
|
|
2780
|
+
applyFresh(transform, data);
|
|
2291
2781
|
}
|
|
2292
2782
|
|
|
2293
2783
|
async function onSubmit(prompt) {
|
|
2294
2784
|
try { bar.setBusy(true); bar.showActions(false); } catch (_) { /* bar optional in tests */ }
|
|
2785
|
+
// Acknowledge instantly, then narrate the wait in stages.
|
|
2786
|
+
const stageTimers = [];
|
|
2787
|
+
try {
|
|
2788
|
+
bar.setStatus(STAGE_MESSAGES[0], '');
|
|
2789
|
+
stageTimers.push(setTimeout(() => { try { bar.setStatus(STAGE_MESSAGES[1], ''); } catch (_) { /* noop */ } }, stageDelays[0]));
|
|
2790
|
+
stageTimers.push(setTimeout(() => { try { bar.setStatus(STAGE_MESSAGES[2], ''); } catch (_) { /* noop */ } }, stageDelays[1]));
|
|
2791
|
+
} catch (_) { /* noop */ }
|
|
2792
|
+
const clearStages = () => { while (stageTimers.length) clearTimeout(stageTimers.pop()); };
|
|
2793
|
+
|
|
2295
2794
|
let context = '';
|
|
2296
2795
|
try { context = extractContext() || ''; } catch (_) { /* snapshot is best-effort */ }
|
|
2297
2796
|
|
|
@@ -2310,9 +2809,11 @@
|
|
|
2310
2809
|
}
|
|
2311
2810
|
resp = await r.json();
|
|
2312
2811
|
} catch (e) {
|
|
2812
|
+
clearStages();
|
|
2313
2813
|
try { bar.setBusy(false); bar.setStatus('Could not reach Morph: ' + String((e && e.message) || e), 'err'); } catch (_) { /* noop */ }
|
|
2314
2814
|
return;
|
|
2315
2815
|
}
|
|
2816
|
+
clearStages();
|
|
2316
2817
|
|
|
2317
2818
|
try {
|
|
2318
2819
|
const v = DSL.validateTransform(resp && resp.transform);
|
|
@@ -2335,23 +2836,79 @@
|
|
|
2335
2836
|
return;
|
|
2336
2837
|
}
|
|
2337
2838
|
const anchored = DSL.withAnchors ? DSL.withAnchors(transform, win.document) : transform;
|
|
2338
|
-
await store.
|
|
2839
|
+
const edit = await store.appendEdit(pathKey(), anchored, { prompt });
|
|
2339
2840
|
const note = handle.failed ? ' (' + handle.failed + ' op(s) skipped)' : '';
|
|
2340
|
-
|
|
2841
|
+
const explanation = (resp.transform && resp.transform.explanation) || 'Done.';
|
|
2842
|
+
bar.setStatus('✓ ' + explanation + ' Saved for this page.' + note, 'ok');
|
|
2341
2843
|
bar.showActions(true);
|
|
2342
2844
|
try { bar.clearInput(); } catch (_) { /* noop */ }
|
|
2845
|
+
await refreshCount();
|
|
2846
|
+
// Act-then-undo (the Gmail pattern): the change is already saved;
|
|
2847
|
+
// the toast is the low-cost escape hatch — never a confirm dialog.
|
|
2848
|
+
if (ui && edit) {
|
|
2849
|
+
ui.showToast({
|
|
2850
|
+
text: '✓ ' + explanation + ' Saved for this page.',
|
|
2851
|
+
actionLabel: 'Undo',
|
|
2852
|
+
onAction: () => removeEdit(edit.id),
|
|
2853
|
+
});
|
|
2854
|
+
}
|
|
2343
2855
|
} catch (e) {
|
|
2344
2856
|
try { bar.setBusy(false); bar.setStatus('Something went wrong applying that change.', 'err'); } catch (_) { /* noop */ }
|
|
2345
2857
|
}
|
|
2346
2858
|
}
|
|
2347
2859
|
|
|
2348
|
-
|
|
2860
|
+
// Per-edit dismissal (HAX G8/G9): drop ONE edit, keep the rest.
|
|
2861
|
+
async function removeEdit(id) {
|
|
2862
|
+
await store.removeEdit(pathKey(), id);
|
|
2863
|
+
await reapplyFromStore();
|
|
2864
|
+
await refreshCount();
|
|
2865
|
+
try { bar.setStatus('Change removed.', ''); } catch (_) { /* noop */ }
|
|
2866
|
+
if (ui) await openPanelIfRendered();
|
|
2867
|
+
}
|
|
2868
|
+
|
|
2869
|
+
async function toggleEdit(id, enabled) {
|
|
2870
|
+
await store.setEditEnabled(pathKey(), id, enabled);
|
|
2871
|
+
await reapplyFromStore();
|
|
2872
|
+
await refreshCount();
|
|
2873
|
+
if (ui) await openPanelIfRendered();
|
|
2874
|
+
}
|
|
2875
|
+
|
|
2876
|
+
// Full reset — the "emergency exit" (Nielsen #3). Bar's Undo maps here.
|
|
2877
|
+
async function resetPage() {
|
|
2349
2878
|
if (currentHandle) { DSL.undoTransform(currentHandle); currentHandle = null; }
|
|
2350
2879
|
activeTransform = null;
|
|
2351
2880
|
await store.removeRecipe(pathKey());
|
|
2881
|
+
await refreshCount();
|
|
2882
|
+
if (ui) await openPanelIfRendered();
|
|
2352
2883
|
try { bar.setStatus('Reverted this page to its original look.', ''); bar.showActions(false); } catch (_) { /* noop */ }
|
|
2353
2884
|
}
|
|
2354
2885
|
|
|
2886
|
+
async function onUndo() { return resetPage(); }
|
|
2887
|
+
|
|
2888
|
+
// "Your edits on this page" — visibility + per-edit control (HAX G17).
|
|
2889
|
+
let panelRendered = false;
|
|
2890
|
+
async function renderPanel() {
|
|
2891
|
+
if (!ui) return;
|
|
2892
|
+
const edits = await store.listEdits(pathKey());
|
|
2893
|
+
ui.renderPanel(edits, {
|
|
2894
|
+
onToggle: (id, enabled) => toggleEdit(id, enabled),
|
|
2895
|
+
onDelete: (id) => removeEdit(id),
|
|
2896
|
+
onReset: () => {
|
|
2897
|
+
// Destroying ALL edits is the one legitimately confirm-worthy act.
|
|
2898
|
+
let ok = true;
|
|
2899
|
+
try { ok = win.confirm('Remove all your edits on this page?'); } catch (_) { /* headless */ }
|
|
2900
|
+
if (ok) resetPage();
|
|
2901
|
+
},
|
|
2902
|
+
onClose: () => {},
|
|
2903
|
+
});
|
|
2904
|
+
panelRendered = true;
|
|
2905
|
+
}
|
|
2906
|
+
async function openPanelIfRendered() { if (panelRendered) await renderPanel(); }
|
|
2907
|
+
async function openPanel() {
|
|
2908
|
+
await renderPanel();
|
|
2909
|
+
if (ui) ui.showPanel();
|
|
2910
|
+
}
|
|
2911
|
+
|
|
2355
2912
|
// Reapply the saved recipe: CSS immediately (FOUC kill), the full
|
|
2356
2913
|
// transform (with fresh data) once the DOM is ready.
|
|
2357
2914
|
async function applySaved() {
|
|
@@ -2378,9 +2935,10 @@
|
|
|
2378
2935
|
} else {
|
|
2379
2936
|
await full();
|
|
2380
2937
|
}
|
|
2938
|
+
await refreshCount();
|
|
2381
2939
|
}
|
|
2382
2940
|
|
|
2383
|
-
const api = { init, onSubmit, onUndo, applySaved };
|
|
2941
|
+
const api = { init, onSubmit, onUndo, applySaved, removeEdit, toggleEdit, resetPage, openPanel };
|
|
2384
2942
|
return api;
|
|
2385
2943
|
}
|
|
2386
2944
|
|
|
@@ -2396,19 +2954,36 @@
|
|
|
2396
2954
|
const doFetch = typeof window.fetch === 'function'
|
|
2397
2955
|
? window.fetch.bind(window)
|
|
2398
2956
|
: () => Promise.reject(new Error('Morph: fetch unavailable'));
|
|
2957
|
+
// Visible launcher (discoverability: HAX G7 — an invisible hotkey is
|
|
2958
|
+
// not an entry point). config.launcher: false to hide, or
|
|
2959
|
+
// { position: 'bottom-left', accent: '#hex' } to theme.
|
|
2960
|
+
const launcherCfg = config.launcher === false ? { position: false } : (config.launcher || {});
|
|
2961
|
+
const ui = window.MorphUI.createUI({
|
|
2962
|
+
win: window,
|
|
2963
|
+
position: launcherCfg.position === undefined ? 'bottom-right' : launcherCfg.position,
|
|
2964
|
+
accent: launcherCfg.accent,
|
|
2965
|
+
});
|
|
2399
2966
|
const embed = createEmbed({
|
|
2400
2967
|
config, win: window, DSL: window.MorphDSL, registry, store,
|
|
2401
|
-
bar: window.Morph.bar, extractContext: window.Morph.extractContext,
|
|
2402
|
-
fetchFn: doFetch,
|
|
2968
|
+
bar: window.Morph.bar, ui, extractContext: window.Morph.extractContext,
|
|
2969
|
+
fetchFn: doFetch, compose: window.MorphStorage.composeEdits,
|
|
2403
2970
|
});
|
|
2404
2971
|
embed.init();
|
|
2972
|
+
ui.mount({
|
|
2973
|
+
onPrimary: () => window.Morph.bar.show(),
|
|
2974
|
+
onPanel: () => { embed.openPanel().catch(() => {}); },
|
|
2975
|
+
});
|
|
2405
2976
|
// Hotkey (default alt+m) + a programmatic opener for vendor-built buttons.
|
|
2977
|
+
// Physical-key matching (e.code) so macOS Option+M — which types 'µ' —
|
|
2978
|
+
// still fires; see matchesTrigger above.
|
|
2406
2979
|
const trigger = typeof config.trigger === 'string' ? config.trigger.toLowerCase() : 'alt+m';
|
|
2407
2980
|
if (trigger !== 'none') {
|
|
2408
|
-
|
|
2981
|
+
// capture:true so host-page handlers can't swallow the shortcut;
|
|
2982
|
+
// typing guard so we never steal keystrokes from the host's inputs.
|
|
2409
2983
|
window.addEventListener('keydown', (e) => {
|
|
2410
|
-
if (
|
|
2411
|
-
|
|
2984
|
+
if (isTypingTarget(e)) return;
|
|
2985
|
+
if (matchesTrigger(e, trigger)) { e.preventDefault(); e.stopPropagation(); window.Morph.bar.toggle(); }
|
|
2986
|
+
}, { capture: true });
|
|
2412
2987
|
}
|
|
2413
2988
|
window.Morph.open = () => window.Morph.bar.show();
|
|
2414
2989
|
embed.applySaved().catch(() => {});
|
|
@@ -2416,5 +2991,5 @@
|
|
|
2416
2991
|
};
|
|
2417
2992
|
}
|
|
2418
2993
|
|
|
2419
|
-
return { createEmbed };
|
|
2994
|
+
return { createEmbed, matchesTrigger, isTypingTarget };
|
|
2420
2995
|
});
|