@next2d/text 3.10.1 → 3.11.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next2d/text",
3
- "version": "3.10.1",
3
+ "version": "3.11.0",
4
4
  "description": "Next2D Text Package",
5
5
  "author": "Toshiyuki Ienaga<ienaga@next2d.app> (https://github.com/ienaga/)",
6
6
  "license": "MIT",
@@ -24,10 +24,10 @@
24
24
  "url": "git+https://github.com/Next2D/Player.git"
25
25
  },
26
26
  "dependencies": {
27
- "@next2d/display": "3.10.1",
28
- "@next2d/geom": "3.10.1",
29
- "@next2d/events": "3.10.1",
30
- "@next2d/ui": "3.10.1",
31
- "@next2d/cache": "3.10.1"
27
+ "@next2d/display": "3.11.0",
28
+ "@next2d/geom": "3.11.0",
29
+ "@next2d/events": "3.11.0",
30
+ "@next2d/ui": "3.11.0",
31
+ "@next2d/cache": "3.11.0"
32
32
  }
33
33
  }
@@ -0,0 +1,15 @@
1
+ import type { TextField } from "../../TextField";
2
+ /**
3
+ * @description maxChars に設定された最大文字数を超えないように、入力テキストを切り詰めて返します。
4
+ * maxChars が 0 の場合は制限しません。
5
+ * Truncates the input text so that it does not exceed the maximum number of
6
+ * characters set in maxChars. If maxChars is 0, no restriction is applied.
7
+ *
8
+ * @param {TextField} text_field
9
+ * @param {string} texts
10
+ * @param {number} current_length
11
+ * @return {string}
12
+ * @method
13
+ * @protected
14
+ */
15
+ export declare const execute: (text_field: TextField, texts: string, current_length: number) => string;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @description maxChars に設定された最大文字数を超えないように、入力テキストを切り詰めて返します。
3
+ * maxChars が 0 の場合は制限しません。
4
+ * Truncates the input text so that it does not exceed the maximum number of
5
+ * characters set in maxChars. If maxChars is 0, no restriction is applied.
6
+ *
7
+ * @param {TextField} text_field
8
+ * @param {string} texts
9
+ * @param {number} current_length
10
+ * @return {string}
11
+ * @method
12
+ * @protected
13
+ */
14
+ export const execute = (text_field, texts, current_length) => {
15
+ if (!text_field.maxChars || !texts) {
16
+ return texts;
17
+ }
18
+ const remaining = text_field.maxChars - current_length;
19
+ if (remaining <= 0) {
20
+ return "";
21
+ }
22
+ return remaining >= texts.length
23
+ ? texts
24
+ : texts.slice(0, remaining);
25
+ };
@@ -0,0 +1,17 @@
1
+ import type { TextField } from "../../TextField";
2
+ /**
3
+ * @description restrict に設定された制限文字列を解析して、入力可能な文字だけを返します。
4
+ * 許可文字の列挙(例 "abc")、範囲指定(例 "a-z")、"^" による除外指定、
5
+ * "\" によるエスケープに対応します。restrict が空文字列の場合は制限しません。
6
+ * Parses the restriction string set in restrict and returns only the characters
7
+ * that can be entered. Supports enumeration of allowed characters (e.g. "abc"),
8
+ * range specification (e.g. "a-z"), exclusion by "^", and escaping by "\".
9
+ * If restrict is an empty string, no restriction is applied.
10
+ *
11
+ * @param {TextField} text_field
12
+ * @param {string} texts
13
+ * @return {string}
14
+ * @method
15
+ * @protected
16
+ */
17
+ export declare const execute: (text_field: TextField, texts: string) => string;
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @description 指定の文字コードが範囲内に含まれるかを判定します。
3
+ * Determines whether the specified character code is included in the ranges.
4
+ *
5
+ * @param {IRange[]} ranges
6
+ * @param {number} code
7
+ * @return {boolean}
8
+ * @method
9
+ * @private
10
+ */
11
+ const $contains = (ranges, code) => {
12
+ for (let idx = 0; idx < ranges.length; ++idx) {
13
+ const range = ranges[idx];
14
+ if (code >= range.min && code <= range.max) {
15
+ return true;
16
+ }
17
+ }
18
+ return false;
19
+ };
20
+ /**
21
+ * @description restrict に設定された制限文字列を解析して、入力可能な文字だけを返します。
22
+ * 許可文字の列挙(例 "abc")、範囲指定(例 "a-z")、"^" による除外指定、
23
+ * "\" によるエスケープに対応します。restrict が空文字列の場合は制限しません。
24
+ * Parses the restriction string set in restrict and returns only the characters
25
+ * that can be entered. Supports enumeration of allowed characters (e.g. "abc"),
26
+ * range specification (e.g. "a-z"), exclusion by "^", and escaping by "\".
27
+ * If restrict is an empty string, no restriction is applied.
28
+ *
29
+ * @param {TextField} text_field
30
+ * @param {string} texts
31
+ * @return {string}
32
+ * @method
33
+ * @protected
34
+ */
35
+ export const execute = (text_field, texts) => {
36
+ const restrict = text_field.restrict;
37
+ if (!restrict || !texts) {
38
+ return texts;
39
+ }
40
+ const allowedRanges = [];
41
+ const deniedRanges = [];
42
+ let denied = false;
43
+ const length = restrict.length;
44
+ for (let idx = 0; idx < length;) {
45
+ let character = restrict[idx];
46
+ // "^" 以降は除外指定に切り替わる(再度 "^" が現れると許可指定に戻る)
47
+ if (character === "^") {
48
+ denied = !denied;
49
+ idx++;
50
+ continue;
51
+ }
52
+ if (character === "\\" && idx + 1 < length) {
53
+ idx++;
54
+ character = restrict[idx];
55
+ }
56
+ const min = character.charCodeAt(0);
57
+ let max = min;
58
+ if (idx + 2 < length && restrict[idx + 1] === "-") {
59
+ let endCharacter = restrict[idx + 2];
60
+ let step = 3;
61
+ if (endCharacter === "\\" && idx + 3 < length) {
62
+ endCharacter = restrict[idx + 3];
63
+ step = 4;
64
+ }
65
+ max = endCharacter.charCodeAt(0);
66
+ idx += step;
67
+ }
68
+ else {
69
+ idx++;
70
+ }
71
+ const ranges = denied ? deniedRanges : allowedRanges;
72
+ ranges.push({
73
+ "min": Math.min(min, max),
74
+ "max": Math.max(min, max)
75
+ });
76
+ }
77
+ let filteredTexts = "";
78
+ for (let idx = 0; idx < texts.length; ++idx) {
79
+ const code = texts.charCodeAt(idx);
80
+ if ($contains(deniedRanges, code)) {
81
+ continue;
82
+ }
83
+ // 許可指定が一つもない場合(除外指定のみ)は、除外以外のすべての文字を許可する
84
+ if (allowedRanges.length && !$contains(allowedRanges, code)) {
85
+ continue;
86
+ }
87
+ filteredTexts += texts[idx];
88
+ }
89
+ return filteredTexts;
90
+ };
@@ -1,5 +1,9 @@
1
1
  import { $textArea } from "../../TextUtil";
2
2
  import { execute as textFieldGetTextDataUseCase } from "../../TextField/usecase/TextFieldGetTextDataUseCase";
3
+ import { execute as textFieldDeleteTextUseCase } from "../../TextField/usecase/TextFieldDeleteTextUseCase";
4
+ import { execute as textFieldInsertTextUseCase } from "../../TextField/usecase/TextFieldInsertTextUseCase";
5
+ import { execute as textFieldGetRestrictTextsService } from "../service/TextFieldGetRestrictTextsService";
6
+ import { execute as textFieldGetMaxCharsTextsService } from "../service/TextFieldGetMaxCharsTextsService";
3
7
  /**
4
8
  * @description テキストフィールドのコンポジションエンドイベントを処理します。
5
9
  * Processes the composition end event of the text field.
@@ -11,15 +15,45 @@ import { execute as textFieldGetTextDataUseCase } from "../../TextField/usecase/
11
15
  */
12
16
  export const execute = (text_field) => {
13
17
  if (text_field.compositionEndIndex > -1) {
18
+ const startIndex = text_field.compositionStartIndex;
19
+ const endIndex = text_field.compositionEndIndex;
14
20
  const textData = textFieldGetTextDataUseCase(text_field);
15
- for (let idx = text_field.compositionStartIndex; idx < text_field.compositionEndIndex; ++idx) {
21
+ // 確定した文字列を取得しつつ、変換中の下線を解除
22
+ let confirmedTexts = "";
23
+ for (let idx = startIndex; idx < endIndex; ++idx) {
16
24
  const textObject = textData.textTable[idx];
17
25
  if (!textObject) {
18
26
  continue;
19
27
  }
20
28
  textObject.textFormat.underline = false;
29
+ switch (textObject.mode) {
30
+ case "break":
31
+ confirmedTexts += "\n";
32
+ break;
33
+ case "text":
34
+ confirmedTexts += textObject.text;
35
+ break;
36
+ default:
37
+ continue;
38
+ }
39
+ }
40
+ let insertTexts = textFieldGetRestrictTextsService(text_field, confirmedTexts);
41
+ insertTexts = textFieldGetMaxCharsTextsService(text_field, insertTexts, text_field.text.length - confirmedTexts.length);
42
+ if (insertTexts === confirmedTexts) {
43
+ text_field.focusIndex = endIndex;
44
+ }
45
+ else {
46
+ // restrict、maxChars の制限で確定文字列が変化した場合は、
47
+ // 変換中の文字列を削除して制限後の文字列を挿入し直す
48
+ text_field.compositionStartIndex = -1;
49
+ text_field.compositionEndIndex = -1;
50
+ text_field.focusIndex = startIndex;
51
+ text_field.selectIndex = endIndex - 1;
52
+ textFieldDeleteTextUseCase(text_field);
53
+ if (insertTexts) {
54
+ textFieldInsertTextUseCase(text_field, insertTexts);
55
+ }
21
56
  }
22
- text_field.focusIndex = text_field.compositionEndIndex;
23
57
  }
24
58
  $textArea.blur();
25
59
  $textArea.value = "";
@@ -2,6 +2,8 @@ import { TextFormat } from "../../TextFormat";
2
2
  import { $textArea } from "../../TextUtil";
3
3
  import { execute as textFieldDeleteTextUseCase } from "../../TextField/usecase/TextFieldDeleteTextUseCase";
4
4
  import { execute as textFieldGetTextDataUseCase } from "../../TextField/usecase/TextFieldGetTextDataUseCase";
5
+ import { execute as textFieldGetRestrictTextsService } from "../service/TextFieldGetRestrictTextsService";
6
+ import { execute as textFieldGetMaxCharsTextsService } from "../service/TextFieldGetMaxCharsTextsService";
5
7
  /**
6
8
  * @description TextField にテキストを挿入します。
7
9
  * Inserts text into the TextField.
@@ -17,9 +19,19 @@ export const execute = (text_field, texts) => {
17
19
  || text_field.compositionStartIndex > -1) {
18
20
  return;
19
21
  }
22
+ texts = textFieldGetRestrictTextsService(text_field, texts);
23
+ if (!texts) {
24
+ $textArea.value = "";
25
+ return;
26
+ }
20
27
  if (text_field.selectIndex > -1) {
21
28
  textFieldDeleteTextUseCase(text_field);
22
29
  }
30
+ texts = textFieldGetMaxCharsTextsService(text_field, texts, text_field.text.length);
31
+ if (!texts) {
32
+ $textArea.value = "";
33
+ return;
34
+ }
23
35
  const textData = textFieldGetTextDataUseCase(text_field);
24
36
  if (2 > textData.textTable.length) {
25
37
  text_field.focusIndex = 2;
@@ -149,10 +149,14 @@ export declare class TextField extends InteractiveObject {
149
149
  maxChars: number;
150
150
  /**
151
151
  * @description ユーザーがテキストフィールドに入力できる文字のセットを指定します。
152
+ * 空文字列の場合は制限されません。"a-z" のような範囲指定、"^" による除外指定、
153
+ * "\\" によるエスケープに対応します。
152
154
  * Indicates the set of characters that a user can enter into the text field.
155
+ * If it is an empty string, no restriction is applied. Supports range
156
+ * specification such as "a-z", exclusion by "^", and escaping by "\\".
153
157
  *
154
158
  * @member {string}
155
- * @default null
159
+ * @default ""
156
160
  * @public
157
161
  */
158
162
  restrict: string;
package/src/TextField.js CHANGED
@@ -247,10 +247,14 @@ export class TextField extends InteractiveObject {
247
247
  });
248
248
  /**
249
249
  * @description ユーザーがテキストフィールドに入力できる文字のセットを指定します。
250
+ * 空文字列の場合は制限されません。"a-z" のような範囲指定、"^" による除外指定、
251
+ * "\\" によるエスケープに対応します。
250
252
  * Indicates the set of characters that a user can enter into the text field.
253
+ * If it is an empty string, no restriction is applied. Supports range
254
+ * specification such as "a-z", exclusion by "^", and escaping by "\\".
251
255
  *
252
256
  * @member {string}
253
- * @default null
257
+ * @default ""
254
258
  * @public
255
259
  */
256
260
  Object.defineProperty(this, "restrict", {