Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<TAB> key press inserts '\t' character in code editor as indentation. #2779

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
<TAB> key press inserts '\t' character in code editor as indentation.
Signed-off-by: Onesimus Wiafe <onesimus.wiafe@turntabl.io>
  • Loading branch information
onesimus-wiafe authored and texodus committed Oct 17, 2024
commit df7101fd8d2b21291ec4896ec3fed72089f3eb2a
22 changes: 22 additions & 0 deletions rust/perspective-viewer/src/rust/components/form/code_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ fn on_keydown(event: KeyboardEvent, deps: &(UseStateSetter<u32>, Callback<()>))
event.prevent_default();
deps.1.emit(())
}

// handle the tab key press
if event.key() == "Tab" {
event.prevent_default();

let caret_pos = elem.selection_start().unwrap().unwrap_or_default() as usize;

let mut initial_text = elem.value();

initial_text.insert(caret_pos, '\t');

elem.set_value(&initial_text);

let input_event = web_sys::InputEvent::new("input").unwrap();
let _ = elem.dispatch_event(&input_event).unwrap();

// place caret after inserted tab
let new_caret_pos = (caret_pos + 1) as u32;
let _ = elem.set_selection_range(new_caret_pos, new_caret_pos);

elem.focus().unwrap();
}
}

/// Scrolling callback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,39 @@ test.describe("Attributes Tab", () => {
view.columnSettingsSidebar.attributesTab.saveBtn
).toBeDisabled();
});
test("Tab Button Click enters 4 spaces.", async ({ page }) => {
let view = new PageView(page);
await view.restore({
expressions: { expr: "12345" },
columns: ["expr", "Row ID"],
settings: true,
});
let expr = await view.settingsPanel.activeColumns.getColumnByName(
"expr"
);
await expr.editBtn.click();
await view.columnSettingsSidebar.openTab("Attributes");

let sidebar = view.columnSettingsSidebar;
let attributesTab = sidebar.attributesTab;

let textarea = attributesTab.expressionEditor.textarea;

await textarea.type("foo", { delay: 100 });
expect(await textarea.evaluate((input) => input!.value)).toStrictEqual(
"foo12345"
);

await textarea.type("\t", { delay: 100 });
const expected = await textarea.evaluate((input) => input!.value);

expect(expected).toContain("\t");

const caretPosition = await textarea.evaluate(
(input) => input!.selectionStart
);
expect(caretPosition).toBe(4); // length of foo + length of '\t' = 4;
});
test("Reset button", async ({ page }) => {
let view = new PageView(page);
await view.restore({
Expand Down