Skip to content

Commit

Permalink
#154 TextFieldのテキスト選択を実装(WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
ienaga committed Nov 29, 2024
1 parent e7e68b9 commit ade234f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
1 change: 0 additions & 1 deletion packages/text/src/TextField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,6 @@ export class TextField extends InteractiveObject
if (this.scrollY) {
this.scrollY = 0;
}
return ;
}

if (text === this._$text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe("TextFieldDeleteTextUseCase.js test", () =>
const textField = new TextField();

textField.text = "あいうえおかきくけこさしすせそ";
textField.compositionStartIndex = -1;
textField.focusIndex = 0;
textField.selectIndex = 15;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import type { TextField } from "../../TextField";
import { execute as textFieldGetTextDataUseCase } from "./TextFieldGetTextDataUseCase";

/**
* @description テキストフィールドの選択中のテキスト位置にスクロールを移動します。
* Move the scroll to the selected text position in the text field.
*
* @param {TextField} text_field
* @return {void}
* @method
* @protected
*/
export const execute = (text_field: TextField): void =>
{
if (text_field.selectIndex === -1) {
return ;
}

const textData = textFieldGetTextDataUseCase(text_field);
if (2 > textData.textTable.length) {
return ;
}

const textObject = textData.textTable[text_field.selectIndex];
if (!textObject) {
return ;
}

const line = textObject.mode === "text"
? textObject.line
: textObject.line - 1;

const width = text_field.width;
const scaleX = (text_field.textWidth - width) / width;

let textWidth = 2;
let limitWidth = text_field.scrollX * scaleX - 2 + width;
for (let idx = 1; text_field.selectIndex >= idx; ++idx) {

const textObject = textData.textTable[idx];
if (!textObject || textObject.line > line) {
break;
}

if (textObject.line !== line) {
continue;
}

if (text_field.selectIndex !== idx) {
textWidth += textObject.w;
}

limitWidth -= textObject.w;
if (limitWidth > 0) {
continue;
}

if (text_field.yScrollShape.hasLocalVariable("job")) {
text_field.yScrollShape.deleteLocalVariable("job");
}
text_field.scrollX += textObject.w / scaleX;

break;
}

const scrollWidth = text_field.scrollX * scaleX - 2;
if (scrollWidth > textWidth) {
if (text_field.yScrollShape.hasLocalVariable("job")) {
text_field.yScrollShape.deleteLocalVariable("job");
}

text_field.scrollX = text_field.width * ((textWidth - 2) / text_field.textWidth);

}
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { execute as textFieldGetTextDataUseCase } from "./TextFieldGetTextDataUs
import { execute as textFieldBlinkingUseCase } from "./TextFieldBlinkingUseCase";
import { execute as textFieldApplyChangesService } from "../service/TextFieldApplyChangesService";
import { execute as textFieldBlinkingClearTimeoutService } from "../service/TextFieldBlinkingClearTimeoutService";
import { execute as textFieldSelectedFocusMoveUseCase } from "./TextFieldSelectedFocusMoveUseCase";
import { $getBlinkingTimerId } from "../../TextUtil";

/**
Expand Down Expand Up @@ -85,6 +86,8 @@ export const execute = (
text_field.focusVisible = false;
textFieldBlinkingClearTimeoutService();
}

textFieldSelectedFocusMoveUseCase(text_field);
textFieldApplyChangesService(text_field);
}
} else {
Expand Down Expand Up @@ -142,6 +145,7 @@ export const execute = (
textFieldBlinkingClearTimeoutService();
}

textFieldSelectedFocusMoveUseCase(text_field);
textFieldApplyChangesService(text_field);
}
} else {
Expand Down

0 comments on commit ade234f

Please sign in to comment.