@kubohiroya/turbowarp-plus-editor-code-views 0.4.0 → 0.5.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
@@ -19,6 +19,7 @@ TurboWarp Plus Editor のコードの画面。1つの作品と1つの取り消
19
19
  | `./palette-category-reorder`・`./palette-category-search`・`./palette-dropdown-resize` | パレットのカテゴリの並べ替えと検索、パレットのドロップダウンの大きさの変更 |
20
20
  | `./palette-category-visibility`・`./palette-visibility-dialog` | スプライト・背景ごとに、パレットに出すブロックグループを選ぶ。設定はエディターの画面状態として保存し、選ぶ画面を持つ |
21
21
  | `./block-argument-text` | 長いテキストの引数を短く表示し、ダイアログで編集する |
22
+ | `./block-number-input` | 数値の引数に入力した全角の数字・記号を半角にする |
22
23
  | `./asset-edit-session`・`./dockview-unsaved` | 素材の編集のセッションと、Dockview のタブの未保存の印。ホストが GUI のエディターとレイアウトにつなぐ |
23
24
  | `./styles.css` | 上のすべてのスタイルシート |
24
25
 
package/README.md CHANGED
@@ -19,6 +19,7 @@ The code views of TurboWarp Plus Editor: code in tabs and windows that share one
19
19
  | `./palette-category-visibility`, `./palette-visibility-dialog` | Choose which block groups the palette shows for each sprite and the stage. The setting is saved with the editor's screen state, with a screen to choose |
20
20
  | `./palette-category-reorder`, `./palette-category-search`, `./palette-dropdown-resize` | Reordering and searching palette categories, and resizing palette dropdowns |
21
21
  | `./block-argument-text` | Shortens long text arguments and edits them in a dialog |
22
+ | `./block-number-input` | Converts full-width digits and signs typed into number arguments to half-width |
22
23
  | `./asset-edit-session`, `./dockview-unsaved` | The asset edit session and the unsaved marks on Dockview tabs, which the host connects to the GUI's editors and layout |
23
24
  | `./styles.css` | The stylesheet for all of the above |
24
25
 
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Full-width characters in number arguments.
3
+ *
4
+ * Every argument that takes a number (`math_number`, `math_integer`, `math_angle`, the list index
5
+ * of `data_listindexall` and so on) edits through a `FieldTextInput` subclass that was given a
6
+ * restrictor, a pattern of the characters it accepts. scratch-blocks tests that pattern only on
7
+ * `keypress`, and text an IME commits arrives through `input` alone, so `10` passes and the VM
8
+ * reads it as 0. This converts full-width digits and signs to their half-width forms and drops
9
+ * what the restrictor would have refused, for every field with a restrictor and no other.
10
+ */
11
+ /**
12
+ * Half-width forms of full-width ASCII (U+FF01–U+FF5E) and of the signs above.
13
+ * @param {string} text The value.
14
+ * @returns {string} The value with those characters replaced.
15
+ */
16
+ export declare const toHalfWidth: (text: string) => string;
17
+ /**
18
+ * A number field's value with full-width characters converted and refused characters dropped.
19
+ * @param {string} text The value.
20
+ * @param {RegExp} restrictor The field's restrictor, which tests one character.
21
+ * @returns {string} The value the field accepts.
22
+ */
23
+ export declare const normalizeNumberText: (text: string, restrictor: RegExp) => string;
24
+ /**
25
+ * Normalize number fields as they are typed.
26
+ * Only fields with a restrictor change: text arguments, which have none, keep full-width text.
27
+ * While an IME is composing the text is left alone, and it is normalized once the composition ends.
28
+ * @param {object} ScratchBlocks The loaded ScratchBlocks.
29
+ */
30
+ export declare const installBlockNumberInput: (ScratchBlocks: any) => void;
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Full-width characters in number arguments.
3
+ *
4
+ * Every argument that takes a number (`math_number`, `math_integer`, `math_angle`, the list index
5
+ * of `data_listindexall` and so on) edits through a `FieldTextInput` subclass that was given a
6
+ * restrictor, a pattern of the characters it accepts. scratch-blocks tests that pattern only on
7
+ * `keypress`, and text an IME commits arrives through `input` alone, so `10` passes and the VM
8
+ * reads it as 0. This converts full-width digits and signs to their half-width forms and drops
9
+ * what the restrictor would have refused, for every field with a restrictor and no other.
10
+ */
11
+ /** Characters an IME commits for a sign or a decimal point, beyond the full-width ASCII block. */
12
+ const signs = {
13
+ 'ー': '-', // KATAKANA-HIRAGANA PROLONGED SOUND MARK, what the minus key gives in kana mode
14
+ '−': '-', // MINUS SIGN
15
+ '‐': '-', // HYPHEN
16
+ '‑': '-', // NON-BREAKING HYPHEN
17
+ '–': '-', // EN DASH
18
+ '。': '.' // IDEOGRAPHIC FULL STOP, what the period key gives in kana mode
19
+ };
20
+ const fullWidth = /[!-~]/g;
21
+ /**
22
+ * Half-width forms of full-width ASCII (U+FF01–U+FF5E) and of the signs above.
23
+ * @param {string} text The value.
24
+ * @returns {string} The value with those characters replaced.
25
+ */
26
+ export const toHalfWidth = (text) => {
27
+ let result = '';
28
+ for (const character of text.replace(fullWidth, c => String.fromCharCode(c.charCodeAt(0) - 0xFEE0))) {
29
+ result += signs[character] ?? character;
30
+ }
31
+ return result;
32
+ };
33
+ /**
34
+ * A number field's value with full-width characters converted and refused characters dropped.
35
+ * @param {string} text The value.
36
+ * @param {RegExp} restrictor The field's restrictor, which tests one character.
37
+ * @returns {string} The value the field accepts.
38
+ */
39
+ export const normalizeNumberText = (text, restrictor) => {
40
+ let result = '';
41
+ for (const character of toHalfWidth(text)) {
42
+ restrictor.lastIndex = 0;
43
+ if (restrictor.test(character))
44
+ result += character;
45
+ }
46
+ return result;
47
+ };
48
+ const installed = new WeakSet();
49
+ const compositionHandlers = new WeakMap();
50
+ /**
51
+ * Rewrite the editor's value in place, keeping the caret after the same characters.
52
+ * @param {HTMLInputElement} input The inline editor.
53
+ * @param {RegExp} restrictor The field's restrictor.
54
+ */
55
+ const normalizeInput = (input, restrictor) => {
56
+ const { value } = input;
57
+ const normalized = normalizeNumberText(value, restrictor);
58
+ if (normalized === value)
59
+ return;
60
+ let caret = null;
61
+ try {
62
+ if (input.selectionEnd !== null)
63
+ caret = normalizeNumberText(value.slice(0, input.selectionEnd), restrictor).length;
64
+ }
65
+ catch {
66
+ // Some input types have no selection.
67
+ }
68
+ input.value = normalized;
69
+ if (caret !== null) {
70
+ try {
71
+ input.setSelectionRange(caret, caret);
72
+ }
73
+ catch {
74
+ // Same as above.
75
+ }
76
+ }
77
+ };
78
+ /**
79
+ * Normalize number fields as they are typed.
80
+ * Only fields with a restrictor change: text arguments, which have none, keep full-width text.
81
+ * While an IME is composing the text is left alone, and it is normalized once the composition ends.
82
+ * @param {object} ScratchBlocks The loaded ScratchBlocks.
83
+ */
84
+ export const installBlockNumberInput = ScratchBlocks => {
85
+ const FieldTextInput = ScratchBlocks?.FieldTextInput;
86
+ const field = FieldTextInput?.prototype;
87
+ if (!field?.onHtmlInputChange_ || !field.bindEvents_ || !field.unbindEvents_ || installed.has(field))
88
+ return;
89
+ installed.add(field);
90
+ const { onHtmlInputChange_: onChange, bindEvents_: bindEvents, unbindEvents_: unbindEvents } = field;
91
+ field.onHtmlInputChange_ = function (event, ...rest) {
92
+ const input = FieldTextInput.htmlInput_;
93
+ // A keypress is still filtered by the inherited code, before the character is inserted.
94
+ if (this.restrictor_ instanceof RegExp && input && event?.type !== 'keypress' && !event?.isComposing) {
95
+ normalizeInput(input, this.restrictor_);
96
+ }
97
+ return onChange.call(this, event, ...rest);
98
+ };
99
+ field.bindEvents_ = function (htmlInput, ...rest) {
100
+ const result = bindEvents.call(this, htmlInput, ...rest);
101
+ if (this.restrictor_ instanceof RegExp && htmlInput?.addEventListener) {
102
+ // Chrome sends the last `input` of a composition while it is still composing.
103
+ const handler = (event) => this.onHtmlInputChange_(event);
104
+ compositionHandlers.set(htmlInput, handler);
105
+ htmlInput.addEventListener('compositionend', handler);
106
+ }
107
+ return result;
108
+ };
109
+ field.unbindEvents_ = function (htmlInput, ...rest) {
110
+ const handler = htmlInput && compositionHandlers.get(htmlInput);
111
+ if (handler) {
112
+ htmlInput.removeEventListener('compositionend', handler);
113
+ compositionHandlers.delete(htmlInput);
114
+ }
115
+ return unbindEvents.call(this, htmlInput, ...rest);
116
+ };
117
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-plus-editor-code-views",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Code views for TurboWarp Plus Editor: code in tabs and windows, the detached block palette, script layout and block field editing",
5
5
  "type": "module",
6
6
  "license": "MPL-2.0",
@@ -62,6 +62,10 @@
62
62
  "types": "./dist/block-argument-text.d.ts",
63
63
  "default": "./dist/block-argument-text.js"
64
64
  },
65
+ "./block-number-input": {
66
+ "types": "./dist/block-number-input.d.ts",
67
+ "default": "./dist/block-number-input.js"
68
+ },
65
69
  "./script-layout": {
66
70
  "types": "./dist/script-layout-view.d.ts",
67
71
  "default": "./dist/script-layout-view.js"
@@ -0,0 +1,124 @@
1
+ /**
2
+ * Full-width characters in number arguments.
3
+ *
4
+ * Every argument that takes a number (`math_number`, `math_integer`, `math_angle`, the list index
5
+ * of `data_listindexall` and so on) edits through a `FieldTextInput` subclass that was given a
6
+ * restrictor, a pattern of the characters it accepts. scratch-blocks tests that pattern only on
7
+ * `keypress`, and text an IME commits arrives through `input` alone, so `10` passes and the VM
8
+ * reads it as 0. This converts full-width digits and signs to their half-width forms and drops
9
+ * what the restrictor would have refused, for every field with a restrictor and no other.
10
+ */
11
+
12
+ /** Characters an IME commits for a sign or a decimal point, beyond the full-width ASCII block. */
13
+ const signs: Readonly<Record<string, string>> = {
14
+ 'ー': '-', // KATAKANA-HIRAGANA PROLONGED SOUND MARK, what the minus key gives in kana mode
15
+ '−': '-', // MINUS SIGN
16
+ '‐': '-', // HYPHEN
17
+ '‑': '-', // NON-BREAKING HYPHEN
18
+ '–': '-', // EN DASH
19
+ '。': '.' // IDEOGRAPHIC FULL STOP, what the period key gives in kana mode
20
+ };
21
+
22
+ const fullWidth = /[!-~]/g;
23
+
24
+ /**
25
+ * Half-width forms of full-width ASCII (U+FF01–U+FF5E) and of the signs above.
26
+ * @param {string} text The value.
27
+ * @returns {string} The value with those characters replaced.
28
+ */
29
+ export const toHalfWidth = (text: string): string => {
30
+ let result = '';
31
+ for (const character of text.replace(fullWidth, c => String.fromCharCode(c.charCodeAt(0) - 0xFEE0))) {
32
+ result += signs[character] ?? character;
33
+ }
34
+ return result;
35
+ };
36
+
37
+ /**
38
+ * A number field's value with full-width characters converted and refused characters dropped.
39
+ * @param {string} text The value.
40
+ * @param {RegExp} restrictor The field's restrictor, which tests one character.
41
+ * @returns {string} The value the field accepts.
42
+ */
43
+ export const normalizeNumberText = (text: string, restrictor: RegExp): string => {
44
+ let result = '';
45
+ for (const character of toHalfWidth(text)) {
46
+ restrictor.lastIndex = 0;
47
+ if (restrictor.test(character)) result += character;
48
+ }
49
+ return result;
50
+ };
51
+
52
+ type HtmlInput = HTMLInputElement & {oldValue_?: string};
53
+
54
+ const installed = new WeakSet<object>();
55
+ const compositionHandlers = new WeakMap<HtmlInput, EventListener>();
56
+
57
+ /**
58
+ * Rewrite the editor's value in place, keeping the caret after the same characters.
59
+ * @param {HTMLInputElement} input The inline editor.
60
+ * @param {RegExp} restrictor The field's restrictor.
61
+ */
62
+ const normalizeInput = (input: HtmlInput, restrictor: RegExp) => {
63
+ const {value} = input;
64
+ const normalized = normalizeNumberText(value, restrictor);
65
+ if (normalized === value) return;
66
+ let caret: number | null = null;
67
+ try {
68
+ if (input.selectionEnd !== null) caret = normalizeNumberText(value.slice(0, input.selectionEnd), restrictor).length;
69
+ } catch {
70
+ // Some input types have no selection.
71
+ }
72
+ input.value = normalized;
73
+ if (caret !== null) {
74
+ try {
75
+ input.setSelectionRange(caret, caret);
76
+ } catch {
77
+ // Same as above.
78
+ }
79
+ }
80
+ };
81
+
82
+ /**
83
+ * Normalize number fields as they are typed.
84
+ * Only fields with a restrictor change: text arguments, which have none, keep full-width text.
85
+ * While an IME is composing the text is left alone, and it is normalized once the composition ends.
86
+ * @param {object} ScratchBlocks The loaded ScratchBlocks.
87
+ */
88
+ export const installBlockNumberInput = ScratchBlocks => {
89
+ const FieldTextInput = ScratchBlocks?.FieldTextInput;
90
+ const field = FieldTextInput?.prototype;
91
+ if (!field?.onHtmlInputChange_ || !field.bindEvents_ || !field.unbindEvents_ || installed.has(field)) return;
92
+ installed.add(field);
93
+
94
+ const {onHtmlInputChange_: onChange, bindEvents_: bindEvents, unbindEvents_: unbindEvents} = field;
95
+
96
+ field.onHtmlInputChange_ = function (event, ...rest) {
97
+ const input: HtmlInput | undefined = FieldTextInput.htmlInput_;
98
+ // A keypress is still filtered by the inherited code, before the character is inserted.
99
+ if (this.restrictor_ instanceof RegExp && input && event?.type !== 'keypress' && !event?.isComposing) {
100
+ normalizeInput(input, this.restrictor_);
101
+ }
102
+ return onChange.call(this, event, ...rest);
103
+ };
104
+
105
+ field.bindEvents_ = function (htmlInput: HtmlInput, ...rest) {
106
+ const result = bindEvents.call(this, htmlInput, ...rest);
107
+ if (this.restrictor_ instanceof RegExp && htmlInput?.addEventListener) {
108
+ // Chrome sends the last `input` of a composition while it is still composing.
109
+ const handler = (event: Event) => this.onHtmlInputChange_(event);
110
+ compositionHandlers.set(htmlInput, handler);
111
+ htmlInput.addEventListener('compositionend', handler);
112
+ }
113
+ return result;
114
+ };
115
+
116
+ field.unbindEvents_ = function (htmlInput: HtmlInput, ...rest) {
117
+ const handler = htmlInput && compositionHandlers.get(htmlInput);
118
+ if (handler) {
119
+ htmlInput.removeEventListener('compositionend', handler);
120
+ compositionHandlers.delete(htmlInput);
121
+ }
122
+ return unbindEvents.call(this, htmlInput, ...rest);
123
+ };
124
+ };