jsmdcui 0.7.0 → 0.9.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/CHANGELOG.md +127 -0
- package/README.md +189 -19
- package/cdp-maze.js +141 -0
- package/demos/maze.md +149 -0
- package/demos/todo-zh.md +98 -0
- package/demos/todo.md +98 -0
- package/edit +9 -0
- package/llm-maze.txt +94 -0
- package/package.json +1 -1
- package/runmd.mjs +44 -2
- package/runtime/help/cdp.md +5 -0
- package/runtime/help/help.md +189 -19
- package/runtime/jsplugins/cdp/cdp-server.js +26 -2
- package/runtime/jsplugins/cdp/cdp.js +60 -51
- package/runtime/jsplugins/chapter/chapter.js +6 -3
- package/runtime/jsplugins/example/example.js +12 -7
- package/runtime/syntax/markdown.yaml +1 -0
- package/single-exe/README.md +223 -45
- package/single-exe/compiled.js +6 -3
- package/single-exe/entry.mjs +4 -3
- package/single-exe/packAssets.sh +1 -1
- package/src/cui/fence-events.mjs +106 -0
- package/src/cui/id-collision.mjs +10 -28
- package/src/cui/kitty-debug.mjs +1 -1
- package/src/cui/kitty-images.mjs +11 -1
- package/src/cui/rpc.mjs +6 -3
- package/src/cui/server.mjs +13 -2
- package/src/index.js +425 -72
- package/src/lua/engine.js +1 -4
- package/src/platform/terminal.js +5 -0
- package/src/plugins/js-bridge.js +157 -8
- package/tests/compiled-runtime.test.js +8 -0
- package/tests/demo.test.js +40 -0
- package/tests/fence-events.test.js +405 -0
- package/tests/id-collision.test.js +11 -0
- package/tests/js-plugin-prompts.test.js +46 -0
- package/tests/key-event.md +10 -0
- package/tests/kitty-demo.md +1 -1
- package/tests/kitty-images.test.js +22 -0
- package/tests/platform-terminal.test.js +8 -0
- package/tests/textarea.md +8 -0
- package/tests/wui.test.js +16 -1
- package/demo.resized.jpg +0 -0
- package/src/runtime/compiled.js +0 -25
package/demos/maze.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
```text#character
|
|
2
|
+
🧱😀🧱🧱🧱🧱🧱🧱🧱🧱🧱🧱
|
|
3
|
+
🧱 🧱 🧱
|
|
4
|
+
🧱 🧱🧱🧱 🧱 🧱🧱🧱🧱
|
|
5
|
+
🧱 🧱 🧱 🧱
|
|
6
|
+
🧱🧱🧱 🧱🧱🧱🧱🧱 🧱🧱
|
|
7
|
+
🧱 🧱 🧱 🧱
|
|
8
|
+
🧱 🧱🧱🧱 🧱 🧱 🧱🧱
|
|
9
|
+
🧱 🧱 🧱 🧱
|
|
10
|
+
🧱 🧱🧱🧱🧱🧱🧱🧱 🧱🧱
|
|
11
|
+
🧱 🧱 🧱
|
|
12
|
+
🧱 🧱 🧱🧱🧱 🧱🧱🧱🧱
|
|
13
|
+
🧱 🧱 🧱
|
|
14
|
+
🧱🧱🧱🧱🧱🧱🧱🧱🧱 🧱🧱
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```text#controls @keydown.prevent="handle(event)"
|
|
18
|
+
Put the cursor here, then press:
|
|
19
|
+
將遊標放在這裡,然後按:
|
|
20
|
+
|
|
21
|
+
L't half of QWERTY ← move left
|
|
22
|
+
R't half of QWERTY → move right
|
|
23
|
+
T / Y ↑ move up
|
|
24
|
+
Space ↓ move down
|
|
25
|
+
|
|
26
|
+
鍵盤左半邊 ← 向左移動
|
|
27
|
+
鍵盤右半邊 → 向右移動
|
|
28
|
+
T / Y 向上移動
|
|
29
|
+
空白鍵向下移動
|
|
30
|
+
|
|
31
|
+
Arrow keys ←↑→↓ / 正常方向移動
|
|
32
|
+
Ctrl-R Reset maze / 重新開始
|
|
33
|
+
|
|
34
|
+
Do not hit 🧱 / 不能撞牆
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
```text#last-key
|
|
38
|
+
Waiting for input / 等待輸入
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
[🔄 Reset maze / 重新開始](javascript:reset())
|
|
42
|
+
|
|
43
|
+
```js front
|
|
44
|
+
const LEFT_KEYS = new Set([
|
|
45
|
+
...'`12345qwerasdfgzxcvb',
|
|
46
|
+
'escape', 'tab',
|
|
47
|
+
]);
|
|
48
|
+
const RIGHT_KEYS = new Set([
|
|
49
|
+
...'67890-=uiop[]\\hjkl;\'nm,./',
|
|
50
|
+
'backspace', 'enter', 'delete',
|
|
51
|
+
'home', 'end', 'pageup', 'pagedown',
|
|
52
|
+
]);
|
|
53
|
+
const MAZE = [
|
|
54
|
+
'# ##########',
|
|
55
|
+
'# # #',
|
|
56
|
+
'# ### # ####',
|
|
57
|
+
'# # # #',
|
|
58
|
+
'### ##### ##',
|
|
59
|
+
'# # # #',
|
|
60
|
+
'# ### # # ##',
|
|
61
|
+
'# # # #',
|
|
62
|
+
'# ####### ##',
|
|
63
|
+
'# # #',
|
|
64
|
+
'# # ### ####',
|
|
65
|
+
'# # #',
|
|
66
|
+
'######### ##',
|
|
67
|
+
];
|
|
68
|
+
const GOAL = { row: MAZE.length - 1, col: 9 };
|
|
69
|
+
const player = { row: 0, col: 1 };
|
|
70
|
+
let completed = false;
|
|
71
|
+
|
|
72
|
+
function drawMaze() {
|
|
73
|
+
const screen = MAZE.map((line, row) => [...line].map((cell, col) => {
|
|
74
|
+
if (row === player.row && col === player.col) return '😀';
|
|
75
|
+
return cell === '#' ? '🧱' : ' ';
|
|
76
|
+
}).join('')).join('\n');
|
|
77
|
+
$('#character').val(screen);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function reset() {
|
|
81
|
+
player.row = 0;
|
|
82
|
+
player.col = 1;
|
|
83
|
+
completed = false;
|
|
84
|
+
drawMaze();
|
|
85
|
+
$('#last-key').val('Waiting for input / 等待輸入');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function handle(event) {
|
|
89
|
+
const key = String(event.key ?? '').toLowerCase();
|
|
90
|
+
if (event.ctrlKey && key === 'r') {
|
|
91
|
+
reset();
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (completed) return;
|
|
95
|
+
|
|
96
|
+
let row = player.row;
|
|
97
|
+
let col = player.col;
|
|
98
|
+
let direction;
|
|
99
|
+
|
|
100
|
+
if (key === 't' || key === 'y' || key === 'arrowup') {
|
|
101
|
+
row--;
|
|
102
|
+
direction = 'up / 上';
|
|
103
|
+
} else if (key === ' ' || key === 'arrowdown') {
|
|
104
|
+
row++;
|
|
105
|
+
direction = 'down / 下';
|
|
106
|
+
} else if (key === 'arrowleft' || LEFT_KEYS.has(key)) {
|
|
107
|
+
col--;
|
|
108
|
+
direction = 'left / 左';
|
|
109
|
+
} else if (key === 'arrowright' || RIGHT_KEYS.has(key)) {
|
|
110
|
+
col++;
|
|
111
|
+
direction = 'right / 右';
|
|
112
|
+
} else {
|
|
113
|
+
$('#last-key').val(`Ignored / 忽略:${event.key}`);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (MAZE[row]?.[col] !== ' ') {
|
|
118
|
+
$('#last-key').val(`${event.key === ' ' ? 'Space' : event.key} → 🧱 Wall / 撞牆`);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
player.row = row;
|
|
123
|
+
player.col = col;
|
|
124
|
+
drawMaze();
|
|
125
|
+
|
|
126
|
+
if (row === GOAL.row && col === GOAL.col) {
|
|
127
|
+
completed = true;
|
|
128
|
+
$('#last-key').val('🎉🏆🌟 Escaped the maze! / 成功走出迷宮!🌟🏆🎉');
|
|
129
|
+
$('#character').val([
|
|
130
|
+
'',
|
|
131
|
+
'🎆🎇🎉 恭 喜 過 關 ! 🎉🎇🎆',
|
|
132
|
+
'',
|
|
133
|
+
'🥳 你成功逃出迷宮了! 🥳',
|
|
134
|
+
'👑 🏆 ⭐ 🌟 💫 🌈 🦄',
|
|
135
|
+
'👏👏👏 AMAZING! 👏👏👏',
|
|
136
|
+
'🎈 🎊 🎁 🪩 🎁 🎊 🎈',
|
|
137
|
+
'',
|
|
138
|
+
'🚀 ESCAPED THE MAZE! 🚀',
|
|
139
|
+
'',
|
|
140
|
+
'Press Ctrl-R to reset',
|
|
141
|
+
'按 Ctrl-R 重新開始',
|
|
142
|
+
''
|
|
143
|
+
].join('\n'));
|
|
144
|
+
} else {
|
|
145
|
+
const label = event.key === ' ' ? 'Space' : event.key;
|
|
146
|
+
$('#last-key').val(`${label} → ${direction} (${row}, ${col})`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
```
|
package/demos/todo-zh.md
CHANGED
|
@@ -18,10 +18,25 @@ Fly to the moon
|
|
|
18
18
|
在上方輸入 Todo 文字,再選擇新增或移除。移除會刪除第一個文字完全相同的項目。
|
|
19
19
|
|
|
20
20
|
- [新增 Todo](javascript:addTodo())
|
|
21
|
+
- .
|
|
21
22
|
- [移除 Todo](javascript:removeTodo())
|
|
23
|
+
- .
|
|
22
24
|
- [顯示已完成](javascript:showCompleted())
|
|
25
|
+
- .
|
|
23
26
|
- [顯示未完成](javascript:showPending())
|
|
24
27
|
|
|
28
|
+
## 存檔/讀檔
|
|
29
|
+
|
|
30
|
+
```text#todo-file
|
|
31
|
+
todo_list.json
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
在上方輸入 JSON 檔案路徑。存檔會寫入人類可讀的 JSON;讀檔會在確認後取代目前的 Todo 清單。
|
|
35
|
+
|
|
36
|
+
- [儲存 Todos](javascript:saveTodos())
|
|
37
|
+
- .
|
|
38
|
+
- [讀取 Todos](javascript:loadTodos())
|
|
39
|
+
|
|
25
40
|
## 操作結果:
|
|
26
41
|
|
|
27
42
|
```text#todo-status
|
|
@@ -51,6 +66,14 @@ function showItems(title, checked) {
|
|
|
51
66
|
$('#todo-status').val(`找到 ${items.length} 個${title}項目`);
|
|
52
67
|
}
|
|
53
68
|
|
|
69
|
+
function todoFile() {
|
|
70
|
+
return $('#todo-file').val().trim();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function describeError(error) {
|
|
74
|
+
return error?.message || String(error);
|
|
75
|
+
}
|
|
76
|
+
|
|
54
77
|
export function addTodo() {
|
|
55
78
|
const value = todoText();
|
|
56
79
|
if (!value) {
|
|
@@ -89,4 +112,79 @@ export function showCompleted() {
|
|
|
89
112
|
export function showPending() {
|
|
90
113
|
showItems('未完成', false);
|
|
91
114
|
}
|
|
115
|
+
|
|
116
|
+
export async function saveTodos() {
|
|
117
|
+
const file = todoFile();
|
|
118
|
+
if (!file) {
|
|
119
|
+
$('#todo-status').val('存檔失敗:請先輸入 JSON 檔案路徑');
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
const obj = { todos: $('#todos').slice() };
|
|
125
|
+
const result = await rpc.saveTodoList(file, obj);
|
|
126
|
+
$('#todo-status').val(`已將 ${obj.todos.length} 個 Todo 儲存至 ${result.path}`);
|
|
127
|
+
} catch (error) {
|
|
128
|
+
$('#todo-status').val(`存檔失敗:${describeError(error)}`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export async function loadTodos() {
|
|
133
|
+
const file = todoFile();
|
|
134
|
+
if (!file) {
|
|
135
|
+
$('#todo-status').val('讀檔失敗:請先輸入 JSON 檔案路徑');
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
try {
|
|
139
|
+
const result = await rpc.loadTodoList(file);
|
|
140
|
+
const json = JSON.stringify(result.obj, null, 1);
|
|
141
|
+
if (!confirm(`要用「${file}」中的以下 JSON 取代目前的 Todo 清單嗎?\n\n${json}`)) {
|
|
142
|
+
$('#todo-status').val('已取消讀檔,目前的 Todos 未變更');
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const current = $('#todos').slice();
|
|
146
|
+
$('#todos').splice(0, current.length, ...result.obj.todos);
|
|
147
|
+
$('#todo-status').val(`已從 ${result.path} 讀取 ${result.obj.todos.length} 個 Todo`);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
$('#todo-status').val(`讀檔失敗:${describeError(error)}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
```js back
|
|
155
|
+
import { resolve } from 'node:path';
|
|
156
|
+
|
|
157
|
+
function todoPath(input) {
|
|
158
|
+
const value = String(input ?? '').trim();
|
|
159
|
+
if (!value) throw new Error('請先輸入 JSON 檔案路徑');
|
|
160
|
+
return resolve(value);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function validateTodoObject(obj) {
|
|
164
|
+
if (!obj || typeof obj !== 'object' || !Array.isArray(obj.todos))
|
|
165
|
+
throw new Error('Todo JSON 必須是包含 todos 陣列的物件');
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
todos: obj.todos.map((item, index) => {
|
|
169
|
+
if (!item || typeof item !== 'object' || typeof item.value !== 'string')
|
|
170
|
+
throw new Error(`第 ${index + 1} 個 Todo 項目必須包含字串 value`);
|
|
171
|
+
return { value: item.value, checked: Boolean(item.checked) };
|
|
172
|
+
}),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export async function saveTodoList(input, obj) {
|
|
177
|
+
const path = todoPath(input);
|
|
178
|
+
const validated = validateTodoObject(obj);
|
|
179
|
+
await Bun.write(path, JSON.stringify(validated, null, 1) + '\n');
|
|
180
|
+
return { path };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export async function loadTodoList(input) {
|
|
184
|
+
const path = todoPath(input);
|
|
185
|
+
const file = Bun.file(path);
|
|
186
|
+
if (!await file.exists()) throw new Error(`找不到 Todo 檔案:${path}`);
|
|
187
|
+
const obj = validateTodoObject(JSON.parse(await file.text()));
|
|
188
|
+
return { path, obj };
|
|
189
|
+
}
|
|
92
190
|
```
|
package/demos/todo.md
CHANGED
|
@@ -18,10 +18,25 @@ Fly to the moon
|
|
|
18
18
|
Enter Todo text above, then choose Add or Remove. Remove deletes the first item whose text matches exactly.
|
|
19
19
|
|
|
20
20
|
- [Add Todo](javascript:addTodo())
|
|
21
|
+
- .
|
|
21
22
|
- [Remove Todo](javascript:removeTodo())
|
|
23
|
+
- .
|
|
22
24
|
- [Show Completed](javascript:showCompleted())
|
|
25
|
+
- .
|
|
23
26
|
- [Show Pending](javascript:showPending())
|
|
24
27
|
|
|
28
|
+
## Save / Load
|
|
29
|
+
|
|
30
|
+
```text#todo-file
|
|
31
|
+
todo_list.json
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Enter a JSON file path above. Saving writes human-readable JSON; loading replaces the current Todo list after confirmation.
|
|
35
|
+
|
|
36
|
+
- [Save Todos](javascript:saveTodos())
|
|
37
|
+
- .
|
|
38
|
+
- [Load Todos](javascript:loadTodos())
|
|
39
|
+
|
|
25
40
|
## Operation Result
|
|
26
41
|
|
|
27
42
|
```text#todo-status
|
|
@@ -52,6 +67,14 @@ function showItems(title, checked) {
|
|
|
52
67
|
$('#todo-status').val(`Found ${items.length} ${title.toLowerCase()} ${noun}`);
|
|
53
68
|
}
|
|
54
69
|
|
|
70
|
+
function todoFile() {
|
|
71
|
+
return $('#todo-file').val().trim();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function describeError(error) {
|
|
75
|
+
return error?.message || String(error);
|
|
76
|
+
}
|
|
77
|
+
|
|
55
78
|
export function addTodo() {
|
|
56
79
|
const value = todoText();
|
|
57
80
|
if (!value) {
|
|
@@ -90,4 +113,79 @@ export function showCompleted() {
|
|
|
90
113
|
export function showPending() {
|
|
91
114
|
showItems('Pending', false);
|
|
92
115
|
}
|
|
116
|
+
|
|
117
|
+
export async function saveTodos() {
|
|
118
|
+
const file = todoFile();
|
|
119
|
+
if (!file) {
|
|
120
|
+
$('#todo-status').val('Save failed: enter a JSON file path first');
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
const obj = { todos: $('#todos').slice() };
|
|
126
|
+
const result = await rpc.saveTodoList(file, obj);
|
|
127
|
+
$('#todo-status').val(`Saved ${obj.todos.length} Todos to ${result.path}`);
|
|
128
|
+
} catch (error) {
|
|
129
|
+
$('#todo-status').val(`Save failed: ${describeError(error)}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export async function loadTodos() {
|
|
134
|
+
const file = todoFile();
|
|
135
|
+
if (!file) {
|
|
136
|
+
$('#todo-status').val('Load failed: enter a JSON file path first');
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
const result = await rpc.loadTodoList(file);
|
|
141
|
+
const json = JSON.stringify(result.obj, null, 1);
|
|
142
|
+
if (!confirm(`Replace the current Todo list with this JSON from “${file}”?\n\n${json}`)) {
|
|
143
|
+
$('#todo-status').val('Load cancelled; current Todos were not changed');
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const current = $('#todos').slice();
|
|
147
|
+
$('#todos').splice(0, current.length, ...result.obj.todos);
|
|
148
|
+
$('#todo-status').val(`Loaded ${result.obj.todos.length} Todos from ${result.path}`);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
$('#todo-status').val(`Load failed: ${describeError(error)}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
```js back
|
|
156
|
+
import { resolve } from 'node:path';
|
|
157
|
+
|
|
158
|
+
function todoPath(input) {
|
|
159
|
+
const value = String(input ?? '').trim();
|
|
160
|
+
if (!value) throw new Error('Enter a JSON file path first');
|
|
161
|
+
return resolve(value);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function validateTodoObject(obj) {
|
|
165
|
+
if (!obj || typeof obj !== 'object' || !Array.isArray(obj.todos))
|
|
166
|
+
throw new Error('Todo JSON must be an object containing a todos array');
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
todos: obj.todos.map((item, index) => {
|
|
170
|
+
if (!item || typeof item !== 'object' || typeof item.value !== 'string')
|
|
171
|
+
throw new Error(`Todo item ${index + 1} must contain a string value`);
|
|
172
|
+
return { value: item.value, checked: Boolean(item.checked) };
|
|
173
|
+
}),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export async function saveTodoList(input, obj) {
|
|
178
|
+
const path = todoPath(input);
|
|
179
|
+
const validated = validateTodoObject(obj);
|
|
180
|
+
await Bun.write(path, JSON.stringify(validated, null, 1) + '\n');
|
|
181
|
+
return { path };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export async function loadTodoList(input) {
|
|
185
|
+
const path = todoPath(input);
|
|
186
|
+
const file = Bun.file(path);
|
|
187
|
+
if (!await file.exists()) throw new Error(`Todo file not found: ${path}`);
|
|
188
|
+
const obj = validateTodoObject(JSON.parse(await file.text()));
|
|
189
|
+
return { path, obj };
|
|
190
|
+
}
|
|
93
191
|
```
|
package/edit
ADDED
package/llm-maze.txt
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
你正在透過 Bun.WebView 操作 jsmdcui TUI App。
|
|
2
|
+
|
|
3
|
+
CDP 連線位址:
|
|
4
|
+
|
|
5
|
+
ws://127.0.0.1:9222/devtools/browser/cdp-server
|
|
6
|
+
|
|
7
|
+
可用操作:
|
|
8
|
+
|
|
9
|
+
1. 取得完整純文字 UI:
|
|
10
|
+
|
|
11
|
+
await view.evaluate("micro.getAllText()")
|
|
12
|
+
|
|
13
|
+
2. 取得包含 ANSI 樣式與 OSC 8 link 的完整 UI:
|
|
14
|
+
|
|
15
|
+
await view.evaluate("micro.getAllAnsiText()")
|
|
16
|
+
|
|
17
|
+
3. 點擊 buffer 座標:
|
|
18
|
+
|
|
19
|
+
await view.click(column, line)
|
|
20
|
+
|
|
21
|
+
column 與 line 都是 1-based。
|
|
22
|
+
這是整份 buffer 的座標,不是螢幕 pixel。
|
|
23
|
+
不支援 selector click。
|
|
24
|
+
|
|
25
|
+
4. 發送真正的鍵盤事件:
|
|
26
|
+
|
|
27
|
+
await view.press("ArrowUp")
|
|
28
|
+
await view.press("ArrowDown")
|
|
29
|
+
await view.press("ArrowLeft")
|
|
30
|
+
await view.press("ArrowRight")
|
|
31
|
+
await view.press("r", { modifiers: ["Control"] })
|
|
32
|
+
|
|
33
|
+
5. 每次 click 或 press 後,重新取得 UI 快照確認結果。
|
|
34
|
+
|
|
35
|
+
Maze 操作方式:
|
|
36
|
+
|
|
37
|
+
先在 micro.getAllText() 中找到包含 "Put the cursor here" 的行,計算該文字的
|
|
38
|
+
1-based column/line,然後 click 該位置,讓 Maze controls 取得焦點。
|
|
39
|
+
|
|
40
|
+
Maze controls 取得焦點後:
|
|
41
|
+
|
|
42
|
+
- ArrowUp:上
|
|
43
|
+
- ArrowDown:下
|
|
44
|
+
- ArrowLeft:左
|
|
45
|
+
- ArrowRight:右
|
|
46
|
+
- Ctrl-R:重新開始
|
|
47
|
+
|
|
48
|
+
每次移動後可讀取狀態:
|
|
49
|
+
|
|
50
|
+
await view.evaluate("$('#last-key').val()")
|
|
51
|
+
|
|
52
|
+
取得完整迷宮畫面:
|
|
53
|
+
|
|
54
|
+
await view.evaluate("$('#character').val()")
|
|
55
|
+
|
|
56
|
+
看到狀態包含 "Escaped the maze" 代表成功。抵達終點時不會呼叫 alert;大型
|
|
57
|
+
慶祝內容會直接顯示在 character 區域,並提示使用 Ctrl-R 重新開始。
|
|
58
|
+
|
|
59
|
+
初始化與聚焦 controls 的範例:
|
|
60
|
+
|
|
61
|
+
const view = new Bun.WebView({
|
|
62
|
+
backend: {
|
|
63
|
+
type: "chrome",
|
|
64
|
+
url: "ws://127.0.0.1:9222/devtools/browser/cdp-server",
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const cell = await view.evaluate(`
|
|
69
|
+
(() => {
|
|
70
|
+
const lines = micro.getAllText().split("\\n");
|
|
71
|
+
const y = lines.findIndex(line =>
|
|
72
|
+
line.includes("Put the cursor here")
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
x: lines[y].indexOf("Put the cursor here") + 1,
|
|
77
|
+
y: y + 1,
|
|
78
|
+
};
|
|
79
|
+
})()
|
|
80
|
+
`);
|
|
81
|
+
|
|
82
|
+
await view.click(cell.x, cell.y);
|
|
83
|
+
await view.press("r", { modifiers: ["Control"] });
|
|
84
|
+
|
|
85
|
+
alert 注意事項:
|
|
86
|
+
|
|
87
|
+
Maze 抵達終點不會觸發 alert。不過 jsmdcui 其他操作若呼叫同步 alert(),CDP
|
|
88
|
+
operation 會等待使用者直接在實際 terminal 按 Enter 關閉;alert 阻塞期間,無法
|
|
89
|
+
再透過 view.press("Enter") 關閉它。無人值守自動化應避免觸發同步 alert,或預先
|
|
90
|
+
提供替代的 dialog handling。
|
|
91
|
+
|
|
92
|
+
操作完成後呼叫:
|
|
93
|
+
|
|
94
|
+
view.close();
|
package/package.json
CHANGED
package/runmd.mjs
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import path from 'node:path'
|
|
4
4
|
import { pathToFileURL } from 'node:url'
|
|
5
5
|
import { readInternalAssetText } from './src/runtime/assets.js'
|
|
6
|
+
import { fenceEventMap } from './src/cui/fence-events.mjs'
|
|
6
7
|
import { REPO_ROOT } from './single-exe/compiled.js'
|
|
7
8
|
|
|
8
9
|
const csl=console.log
|
|
@@ -220,7 +221,7 @@ function escapeHtmlAttribute(value)
|
|
|
220
221
|
.replaceAll(">", ">");
|
|
221
222
|
}
|
|
222
223
|
|
|
223
|
-
export function convertWuiTextareas(html)
|
|
224
|
+
export function convertWuiTextareas(html, eventsById = new Map())
|
|
224
225
|
{
|
|
225
226
|
return String(html).replace(
|
|
226
227
|
/<pre><code class="language-([^"]+)">([^]*?)<\/code><\/pre>/g,
|
|
@@ -230,10 +231,50 @@ export function convertWuiTextareas(html)
|
|
|
230
231
|
const value = content.replace(/\n$/, "");
|
|
231
232
|
const contentLines = value.split("\n");
|
|
232
233
|
const cols = Math.max(1, ...contentLines.map(line => [...line].length));
|
|
234
|
+
const declaration = identity.id && eventsById.get(identity.id)?.tag === identity.tag
|
|
235
|
+
? eventsById.get(identity.id)
|
|
236
|
+
: null;
|
|
237
|
+
const keydownHandler = declaration?.events.get("keydown");
|
|
238
|
+
const keydownCode = keydownHandler
|
|
239
|
+
? [
|
|
240
|
+
"const __mdcuiKeyCode=Number(event.keyCode||event.which||0);",
|
|
241
|
+
"const __mdcuiCodeLetter=/^Key[A-Z]$/.test(event.code||\"\")?event.code.charCodeAt(3):0;",
|
|
242
|
+
"const __mdcuiLetterCode=__mdcuiKeyCode>=65&&__mdcuiKeyCode<=90?__mdcuiKeyCode:__mdcuiCodeLetter;",
|
|
243
|
+
"const __mdcuiAltGraph=!!event.getModifierState&&event.getModifierState(\"AltGraph\");",
|
|
244
|
+
"const __mdcuiLetter=!__mdcuiAltGraph&&(event.ctrlKey||event.altKey||event.metaKey)&&__mdcuiLetterCode>=65&&__mdcuiLetterCode<=90;",
|
|
245
|
+
"if(__mdcuiLetter)Object.defineProperty(event,\"key\",{configurable:true,value:String.fromCharCode(__mdcuiLetterCode+(event.shiftKey?0:32))});",
|
|
246
|
+
"this.__mdcuiIdentifiedKeydown=!!event.key&&event.key!==\"Unidentified\";",
|
|
247
|
+
"this.__mdcuiUnidentifiedKeydown=event.key===\"Unidentified\"?{keyCode:__mdcuiKeyCode,ctrlKey:!!event.ctrlKey,shiftKey:!!event.shiftKey,altKey:!!event.altKey,metaKey:!!event.metaKey,altGraph:__mdcuiAltGraph}:null;",
|
|
248
|
+
"clearTimeout(this.__mdcuiKeydownReset);",
|
|
249
|
+
"this.__mdcuiKeydownReset=setTimeout(()=>{this.__mdcuiIdentifiedKeydown=false;this.__mdcuiUnidentifiedKeydown=null},0);",
|
|
250
|
+
"if(event.key!==\"Unidentified\"){\n",
|
|
251
|
+
"Object.defineProperty(event,\"toJSON\",{configurable:true,value:function(){const t=this.target||{};return{type:String(this.type||\"\"),key:String(this.key||\"\"),code:String(this.code||\"\"),raw:String(this.raw||\"\"),ctrlKey:!!this.ctrlKey,shiftKey:!!this.shiftKey,altKey:!!this.altKey,metaKey:!!this.metaKey,repeat:!!this.repeat,defaultPrevented:!!this.defaultPrevented,target:{id:String(t.id||\"\"),tagName:String(t.tagName||\"\"),className:String(t.className||\"\"),value:String(t.value??\"\")}}}});",
|
|
252
|
+
keydownHandler.modifiers.includes("prevent")
|
|
253
|
+
? "event.preventDefault();"
|
|
254
|
+
: "",
|
|
255
|
+
keydownHandler.code,
|
|
256
|
+
"\n}",
|
|
257
|
+
].join("")
|
|
258
|
+
: "";
|
|
259
|
+
const beforeInputCode = keydownHandler
|
|
260
|
+
? [
|
|
261
|
+
"if(!this.__mdcuiIdentifiedKeydown&&event.data!=null&&event.data!==\"\"){",
|
|
262
|
+
"const m=this.__mdcuiUnidentifiedKeydown||{};",
|
|
263
|
+
"const letter=!m.altGraph&&(m.ctrlKey||m.altKey||m.metaKey)&&m.keyCode>=65&&m.keyCode<=90?String.fromCharCode(m.keyCode+(m.shiftKey?0:32)):String(event.data);",
|
|
264
|
+
"Object.defineProperties(event,{key:{configurable:true,value:letter},ctrlKey:{configurable:true,value:!!m.ctrlKey},shiftKey:{configurable:true,value:!!m.shiftKey},altKey:{configurable:true,value:!!m.altKey},metaKey:{configurable:true,value:!!m.metaKey}});",
|
|
265
|
+
"this.onkeydown(event)",
|
|
266
|
+
"}",
|
|
267
|
+
].join("")
|
|
268
|
+
: "";
|
|
269
|
+
const inlineEventAttrs = [
|
|
270
|
+
keydownCode ? `onkeydown="${escapeHtmlAttribute(keydownCode)}"` : "",
|
|
271
|
+
beforeInputCode ? `onbeforeinput="${escapeHtmlAttribute(beforeInputCode)}"` : "",
|
|
272
|
+
];
|
|
233
273
|
const attrs = [
|
|
234
274
|
`data-mdcui-tag="${identity.tag}"`,
|
|
235
275
|
`data-mdcui-language="${escapeHtmlAttribute(info)}"`,
|
|
236
276
|
identity.id ? `id="${escapeHtmlAttribute(identity.id)}"` : "",
|
|
277
|
+
...inlineEventAttrs,
|
|
237
278
|
`class="${escapeHtmlAttribute([
|
|
238
279
|
`language-${info}`,
|
|
239
280
|
...identity.classes,
|
|
@@ -306,6 +347,7 @@ export function wrapWuiHeadingSections(html)
|
|
|
306
347
|
|
|
307
348
|
export async function createWui(md,mdpath) // HTML
|
|
308
349
|
{
|
|
350
|
+
const eventsById = fenceEventMap(md)
|
|
309
351
|
|
|
310
352
|
const opts = {
|
|
311
353
|
headings: { ids: true }
|
|
@@ -335,7 +377,7 @@ export async function createWui(md,mdpath) // HTML
|
|
|
335
377
|
}
|
|
336
378
|
)
|
|
337
379
|
|
|
338
|
-
md = convertWuiTextareas(md)
|
|
380
|
+
md = convertWuiTextareas(md, eventsById)
|
|
339
381
|
md = wrapWuiHeadingSections(md)
|
|
340
382
|
|
|
341
383
|
const mdb = path.basename(mdpath);
|
package/runtime/help/cdp.md
CHANGED
|
@@ -22,6 +22,11 @@
|
|
|
22
22
|
// Once running, status bar shows: CDP@<port> server running
|
|
23
23
|
// Running cdp again shows: CDP@<port> already running
|
|
24
24
|
//
|
|
25
|
+
// ── Maze demo from the source tree ─────────────────────────────────────────
|
|
26
|
+
//
|
|
27
|
+
// Start the maze, local CDP server, and automatic solver together:
|
|
28
|
+
// bun src/index.js --cdp-maze
|
|
29
|
+
//
|
|
25
30
|
// ── Connecting ─────────────────────────────────────────────────────────────
|
|
26
31
|
//
|
|
27
32
|
// Bun.WebView:
|