@kubohiroya/turbowarp-text-lines 0.4.0 → 0.6.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.ja.md CHANGED
@@ -4,10 +4,11 @@
4
4
 
5
5
  [English](README.md) | 日本語
6
6
 
7
- TurboWarpのプロジェクトで複数行テキストを扱います。Text Linesは次の3つのblockを追加します。
7
+ TurboWarpのプロジェクトで複数行テキストを扱います。Text Linesは次の4つのblockを追加します。
8
8
 
9
9
  - テキストの行数を数える。
10
10
  - 位置を指定して1行を取り出す。
11
+ - 全行をJSON配列として取り出す。
11
12
  - テキストの全行でScratchのリストを置き換える。
12
13
 
13
14
  **ユーザーガイド:** [English](https://kubohiroya.github.io/turbowarp-text-lines/) · [日本語](https://kubohiroya.github.io/turbowarp-text-lines/ja/)
@@ -23,6 +24,18 @@ TurboWarpのプロジェクトで複数行テキストを扱います。Text Lin
23
24
 
24
25
  そのまま使えるJavaScriptファイルをこのリポジトリにcommitしてあります。インストールにNode.jsは要りません。
25
26
 
27
+ ## 実行できるサンプル
28
+
29
+ [**TurboWarp EditorでLines to Listを開く**](https://turbowarp.org/editor?project_url=https%3A%2F%2Fkubohiroya.github.io%2Fturbowarp-text-lines%2Fsamples%2Flines-to-list.sb3) · [SB3をダウンロード](https://kubohiroya.github.io/turbowarp-text-lines/samples/lines-to-list.sb3)
30
+
31
+ 拡張の確認画面で**Run without sandbox**を選んで**Allow**し、緑の旗を押します。
32
+ 行数`3`、2行目`Banana`、リスト`Apple, Banana, Cherry`を表示します。
33
+ 最初の`Input text`設定ブロックを編集して試せます。再実行時はリストを置き換えます。
34
+ 検証対象の拡張JSと背景をSB3へ同梱しており、追加のJS取得は不要です。
35
+
36
+ 元データは`samples/lines-to-list/`にあります。`pnpm run samples:build`で再生成し、
37
+ `pnpm run samples:check`で更新漏れを検査します。[保守方法](samples/README.md)も参照してください。
38
+
26
39
  ## 動かしてみる
27
40
 
28
41
  次のテキストを入力とします。
@@ -48,7 +61,7 @@ flowchart TD
48
61
  class count,line,list extension
49
62
  ```
50
63
 
51
- 角の丸い2つのreporter blockは値を返します。command blockは選択したリストをその場で書き換えます。
64
+ 角の丸いreporter blockは値を返します。command blockは選択したリストをその場で書き換えます。
52
65
 
53
66
  ## ブロック
54
67
 
@@ -58,6 +71,7 @@ flowchart TD
58
71
  |---|---|---|
59
72
  | 全行を数える | `number of lines in [TEXT]` | `3` |
60
73
  | 1行を読む | `line [LINE] of [TEXT]`(行に`2`を指定) | `Banana` |
74
+ | 全行をJSONで取り出す | `lines of [TEXT] as JSON` | `["Apple","Banana","Cherry"]` |
61
75
  | テキストをリストの項目にする | `put lines of [TEXT] into list [LIST]` | `Apple`、`Banana`、`Cherry` |
62
76
 
63
77
  ### block定義
@@ -68,6 +82,7 @@ flowchart TD
68
82
  |---|---|---|---|
69
83
  | `number of lines in [TEXT]` | Reporter | `lineCount` | 渡されたテキストの行数を返します。 |
70
84
  | `line [LINE] of [TEXT]` | Reporter | `lineAt` | 1始まりの行番号で1行を返します。範囲外の行番号は空文字列を返します。 |
85
+ | `lines of [TEXT] as JSON` | Reporter | `linesJson` | 全行を文字列のJSON配列で返します。項目数は行数と常に一致します。 |
71
86
  | `put lines of [TEXT] into list [LIST]` | Command | `writeLinesToList` | 指定したScratchリストの内容を、渡されたテキストの各行で置き換えます。 |
72
87
 
73
88
  `TEXT`の既定値は`first line\nsecond line`、`LINE`の既定値は`1`、`LIST`は`LIST_MENU`から選びます。
@@ -81,11 +96,25 @@ flowchart TD
81
96
  | 空のテキスト | 空行1行として扱います |
82
97
  | 改行で終わるテキスト | 末尾に空行を1つ加えます |
83
98
  | リストのcommand | 選択したリストの既存項目をすべて置き換えます |
99
+ | JSONのblock | 他のblockと同じ規則で分割するため、配列の項目数は`number of lines in [TEXT]`と常に一致します |
84
100
  | 改行コード | LF(`\n`)、CRLF(`\r\n`)、CR(`\r`)を受け付けます |
85
101
 
86
102
  > [!WARNING]
87
103
  > リストのcommandを実行する前に、残したい内容は控えておいてください。
88
104
 
105
+ ## TurboWarp Structured Dataと組み合わせる
106
+
107
+ `lines of [TEXT] as JSON`の結果は、[TurboWarp Structured Data](https://github.com/kubohiroya/turbowarp-structured-data)がそのまま読めるJSONです。
108
+ Structured Dataの`for each`で行を順に処理できます。
109
+
110
+ ```text
111
+ parse JSON (lines of [TEXT] as JSON) as [lines]
112
+ for each in [lines] at path [$] max [100]
113
+ ```
114
+
115
+ 1回のloopで扱えるのは1,000件まで、Structured Dataが受け付けるJSONは256 KiBまでです。それより長いテキストは、
116
+ `put lines of [TEXT] into list [LIST]`でリストへ入れ、Scratchのリストのblockで扱ってください。
117
+
89
118
  図入りの例と日本語版は[ユーザーガイド](https://kubohiroya.github.io/turbowarp-text-lines/)にあります。
90
119
 
91
120
  ## 開発
package/README.md CHANGED
@@ -4,10 +4,11 @@
4
4
 
5
5
  English | [日本語](README.ja.md)
6
6
 
7
- Use multiline text in TurboWarp projects. Text Lines adds three blocks that can:
7
+ Use multiline text in TurboWarp projects. Text Lines adds four blocks that can:
8
8
 
9
9
  - count the lines in some text;
10
- - return one line by its position; and
10
+ - return one line by its position;
11
+ - return every line as a JSON array; and
11
12
  - replace a Scratch list with every line from the text.
12
13
 
13
14
  **User guide:** [English](https://kubohiroya.github.io/turbowarp-text-lines/) · [日本語](https://kubohiroya.github.io/turbowarp-text-lines/ja/)
@@ -16,13 +17,31 @@ Use multiline text in TurboWarp projects. Text Lines adds three blocks that can:
16
17
 
17
18
  1. Download [`dist/text-lines.js`](dist/text-lines.js?raw=1).
18
19
  2. In **TurboWarp Desktop**, open **Extensions**, choose **Custom Extension**, and load the downloaded file.
19
- 3. Enable **Run extension without sandbox** when TurboWarp asks.
20
+ 3. Enable **Run without sandbox** when TurboWarp asks.
20
21
 
21
22
  > [!IMPORTANT]
22
23
  > Text Lines needs to run without the sandbox because its command block writes directly to Scratch lists.
23
24
 
24
25
  The ready-to-use JavaScript file is committed to this repository. You do not need Node.js to install the extension.
25
26
 
27
+ ## Runnable sample
28
+
29
+ [**Open Lines to List in TurboWarp Editor**](https://turbowarp.org/editor?project_url=https%3A%2F%2Fkubohiroya.github.io%2Fturbowarp-text-lines%2Fsamples%2Flines-to-list.sb3) · [Download the SB3](https://kubohiroya.github.io/turbowarp-text-lines/samples/lines-to-list.sb3)
30
+
31
+ Allow the custom extension to load, choose **Run without sandbox**, and
32
+ click the green flag. The stage shows **Line count: 3**, **Second line: Banana**,
33
+ and a **Lines** list containing Apple, Banana, and Cherry. Running it again replaces
34
+ the list. The sample uses three Text Lines blocks and no other extensions.
35
+
36
+ The SB3 includes the tested Text Lines JavaScript so TurboWarp can offer the
37
+ unsandboxed permission checkbox. Reopening the downloaded SB3 needs no extra JS download.
38
+ Edit the first `set Input text` block to try other text.
39
+
40
+ Sources: [`samples/lines-to-list/project.json`](samples/lines-to-list/project.json).
41
+ Rebuild with `pnpm run samples:build`; check committed archives with
42
+ `pnpm run samples:check`. `pnpm run pages:build` stages documentation, `dist/`, and
43
+ samples under `_site/` for GitHub Pages. See [sample maintenance](samples/README.md).
44
+
26
45
  ## See it in action
27
46
 
28
47
  Start with this text:
@@ -48,7 +67,7 @@ flowchart TD
48
67
  class count,line,list extension
49
68
  ```
50
69
 
51
- The two rounded reporter blocks return a value. The command block changes the selected list immediately.
70
+ The rounded reporter blocks return a value. The command block changes the selected list immediately.
52
71
 
53
72
  ## Block reference
54
73
 
@@ -58,6 +77,7 @@ The two rounded reporter blocks return a value. The command block changes the se
58
77
  |---|---|---|
59
78
  | Count all lines | `number of lines in [TEXT]` | `3` |
60
79
  | Read one line | `line [LINE] of [TEXT]` with line `2` | `Banana` |
80
+ | Get every line as JSON | `lines of [TEXT] as JSON` | `["Apple","Banana","Cherry"]` |
61
81
  | Turn the text into list items | `put lines of [TEXT] into list [LIST]` | `Apple`, `Banana`, `Cherry` |
62
82
 
63
83
  The exact block definitions are generated from [`src/block-definitions.json`](src/block-definitions.json):
@@ -95,6 +115,21 @@ Returns one line using a one-based line number. Invalid line numbers return an e
95
115
 
96
116
  </details>
97
117
 
118
+ ### `lines of [TEXT] as JSON`
119
+
120
+ Returns every line as a JSON array of strings, with as many items as the line count.
121
+
122
+ <details>
123
+ <summary>Block metadata</summary>
124
+
125
+ | Property | Value |
126
+ |---|---|
127
+ | Type | Reporter |
128
+ | Opcode | `linesJson` |
129
+ | `TEXT` | String, default: `first line\nsecond line` |
130
+
131
+ </details>
132
+
98
133
  ### `put lines of [TEXT] into list [LIST]`
99
134
 
100
135
  Replaces the contents of the named Scratch list with the lines of the supplied text.
@@ -122,11 +157,25 @@ Replaces the contents of the named Scratch list with the lines of the supplied t
122
157
  | Empty text | Treats it as one empty line |
123
158
  | Text ending with a line break | Adds an empty final line |
124
159
  | List command | Replaces all existing items in the selected list |
160
+ | JSON block | Splits exactly like the other blocks, so the array always has as many items as `number of lines in [TEXT]` |
125
161
  | Line endings | Accepts LF (`\n`), CRLF (`\r\n`), and CR (`\r`) |
126
162
 
127
163
  > [!WARNING]
128
164
  > Copy any list contents you need to keep before running the list command.
129
165
 
166
+ ## Use with TurboWarp Structured Data
167
+
168
+ `lines of [TEXT] as JSON` returns text that [TurboWarp Structured Data](https://github.com/kubohiroya/turbowarp-structured-data)
169
+ can read directly, so its `for each` block can walk the lines:
170
+
171
+ ```text
172
+ parse JSON (lines of [TEXT] as JSON) as [lines]
173
+ for each in [lines] at path [$] max [100]
174
+ ```
175
+
176
+ One loop handles up to 1,000 items, and Structured Data accepts JSON text up to 256 KiB. For longer text,
177
+ use `put lines of [TEXT] into list [LIST]` and Scratch's own list blocks instead.
178
+
130
179
  For illustrated examples and the Japanese version, open the [complete user guide](https://kubohiroya.github.io/turbowarp-text-lines/).
131
180
 
132
181
  ## Development
@@ -21,7 +21,7 @@ var extensionConfig = {
21
21
  blockIconURI: `data:image/svg+xml,${encodeURIComponent("<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 48 48\"><g fill=\"#4C97FF\"><circle cx=\"11\" cy=\"16\" r=\"2.6\"/><circle cx=\"11\" cy=\"24\" r=\"2.6\"/><circle cx=\"11\" cy=\"32\" r=\"2.6\"/></g><path d=\"M19 16h20M19 24h20M19 32h13\" fill=\"none\" stroke=\"#59C059\" stroke-width=\"3\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/></svg>")}`
22
22
  };
23
23
  //#endregion
24
- //#region node_modules/.pnpm/@kubohiroya+turbowarp-extension-data-editor@0.4.0_react@18.3.1/node_modules/@kubohiroya/turbowarp-extension-data-editor/dist/fixed-input-schema.js
24
+ //#region node_modules/.pnpm/@kubohiroya+turbowarp-extension-data-editor@0.5.0_react@18.3.1/node_modules/@kubohiroya/turbowarp-extension-data-editor/dist/fixed-input-schema.js
25
25
  var numberToken = /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?$/u;
26
26
  var invalidNumber = Object.freeze({
27
27
  kind: "invalid",
@@ -255,6 +255,16 @@ var block_definitions_default = {
255
255
  }
256
256
  }
257
257
  },
258
+ {
259
+ "opcode": "linesJson",
260
+ "blockType": "REPORTER",
261
+ "text": "lines of [TEXT] as JSON",
262
+ "description": "Returns every line as a JSON array of strings, with as many items as the line count.",
263
+ "arguments": { "TEXT": {
264
+ "type": "STRING",
265
+ "defaultValue": "first line\nsecond line"
266
+ } }
267
+ },
258
268
  {
259
269
  "opcode": "writeLinesToList",
260
270
  "blockType": "COMMAND",
@@ -357,6 +367,9 @@ var TextLinesExtension = class {
357
367
  if (!Number.isFinite(line) || line < 1 || line > lines.length) return "";
358
368
  return lines[line - 1] ?? "";
359
369
  }
370
+ linesJson(args) {
371
+ return JSON.stringify(splitLines(Scratch.Cast.toString(args.TEXT)));
372
+ }
360
373
  writeLinesToList(args, util) {
361
374
  const listIdOrName = Scratch.Cast.toString(args.LIST);
362
375
  const stage = Scratch.vm.runtime.getTargetForStage();
package/package.json CHANGED
@@ -1,8 +1,12 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-text-lines",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Use multiline text in TurboWarp projects with line counting, lookup, and list-writing blocks.",
5
- "author": "Hiroya Kubo <hiroya@cuc.ac.jp>",
5
+ "author": {
6
+ "name": "Hiroya Kubo",
7
+ "email": "hiroya@cuc.ac.jp",
8
+ "url": "https://github.com/kubohiroya"
9
+ },
6
10
  "license": "MPL-2.0",
7
11
  "homepage": "https://github.com/kubohiroya/turbowarp-text-lines#readme",
8
12
  "repository": {
@@ -47,15 +51,18 @@
47
51
  "check:dist": "pnpm run build && git diff --exit-code -- dist",
48
52
  "repo:check": "node scripts/check-repo.ts",
49
53
  "pack:check": "npm pack --dry-run --ignore-scripts",
50
- "check": "pnpm run typecheck && pnpm run test && pnpm run build && pnpm run docs:check && pnpm run check:dist && pnpm run repo:check && pnpm run pack:check"
54
+ "check": "pnpm run typecheck && pnpm run test && pnpm run build && pnpm run docs:check && pnpm run check:dist && pnpm run repo:check && pnpm run pack:check && pnpm run samples:check",
55
+ "samples:build": "node scripts/build-samples.mjs",
56
+ "samples:check": "node scripts/build-samples.mjs --check && node --test scripts/verify-samples.mjs",
57
+ "pages:build": "node scripts/prepare-pages.mjs"
51
58
  },
52
59
  "devDependencies": {
53
- "@kubohiroya/vite-plugin-turbowarp-extension": "^0.9.0",
60
+ "@kubohiroya/vite-plugin-turbowarp-extension": "^0.10.0",
54
61
  "@types/node": "^22.20.1",
55
62
  "typescript": "^5.9.3",
56
63
  "vite": "^8.2.2",
57
64
  "vitest": "^4.1.10",
58
- "@kubohiroya/turbowarp-extension-data-editor": "^0.4.0"
65
+ "@kubohiroya/turbowarp-extension-data-editor": "^0.5.0"
59
66
  },
60
67
  "gitHead": "86365d0f44d92bcf6ad828be8d165e4852abd7f4",
61
68
  "x-turbowarp-source": {