Skip to content

Commit 09f15f0

Browse files
committed
fix(ui5-tokenizer): cut only selected tokens when selection exists
When both selected and focused tokens are present, cut now deletes only the selected tokens. Focused token deletion is the fallback when no selection exists.
1 parent cd76202 commit 09f15f0

2 files changed

Lines changed: 62 additions & 9 deletions

File tree

packages/main/cypress/specs/Tokenizer.cy.tsx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,62 @@ describe("Clipboard Operations", () => {
18701870
cy.get("@clipboardWrite").should("have.been.calledOnceWith", "ReadonlyToken");
18711871
cy.get("[ui5-token]").should("have.length", 2);
18721872
});
1873+
1874+
it("should cut only selected tokens when there are both selected and focused tokens", () => {
1875+
cy.mount(
1876+
<Tokenizer onTokenDelete={onTokenDelete}>
1877+
<Token text="Selected1" selected></Token>
1878+
<Token text="Selected2" selected></Token>
1879+
<Token text="NotSelected"></Token>
1880+
</Tokenizer>
1881+
);
1882+
1883+
// Focus the third token via keyboard without selecting it
1884+
cy.realPress("Tab");
1885+
cy.realPress("ArrowRight");
1886+
cy.realPress("ArrowRight");
1887+
cy.get("[ui5-token]").eq(2).should("have.prop", "focused", true);
1888+
cy.get("[ui5-token]").eq(2).should("have.prop", "selected", false);
1889+
// First two remain selected
1890+
cy.get("[ui5-token]").eq(0).should("have.prop", "selected", true);
1891+
cy.get("[ui5-token]").eq(1).should("have.prop", "selected", true);
1892+
1893+
cy.window().then((win) => {
1894+
cy.stub(win.navigator.clipboard, "writeText").as("clipboardWrite");
1895+
Object.defineProperty(win, "isSecureContext", { value: true, writable: true });
1896+
});
1897+
1898+
cy.realPress(["Control", "x"]);
1899+
1900+
// Should cut the selected tokens, not the focused one
1901+
cy.get("@clipboardWrite").should("have.been.calledOnceWith", "Selected1\r\nSelected2");
1902+
cy.get("[ui5-token]").should("have.length", 1);
1903+
cy.get("[ui5-token]").eq(0).should("have.prop", "text", "NotSelected");
1904+
});
1905+
1906+
it("should cut focused token when no tokens are selected", () => {
1907+
cy.mount(
1908+
<Tokenizer onTokenDelete={onTokenDelete}>
1909+
<Token text="Focused"></Token>
1910+
<Token text="Other"></Token>
1911+
</Tokenizer>
1912+
);
1913+
1914+
cy.realPress("Tab");
1915+
cy.get("[ui5-token]").eq(0).should("have.prop", "focused", true);
1916+
cy.get("[ui5-token]").eq(0).should("have.prop", "selected", false);
1917+
1918+
cy.window().then((win) => {
1919+
cy.stub(win.navigator.clipboard, "writeText").as("clipboardWrite");
1920+
Object.defineProperty(win, "isSecureContext", { value: true, writable: true });
1921+
});
1922+
1923+
cy.realPress(["Control", "x"]);
1924+
1925+
cy.get("@clipboardWrite").should("have.been.calledOnceWith", "Focused");
1926+
cy.get("[ui5-token]").should("have.length", 1);
1927+
cy.get("[ui5-token]").eq(0).should("have.prop", "text", "Other");
1928+
});
18731929
});
18741930

18751931
describe("Tokenizer - getFocusDomRef Method", () => {

packages/main/src/Tokenizer.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -720,22 +720,19 @@ class Tokenizer extends UI5Element implements IFormInputElement {
720720
e.preventDefault();
721721

722722
const isCut = e.key.toLowerCase() === "x" || isDeleteShift(e);
723-
let selectedTokens = this._tokens.filter(token => token.selected);
723+
const selectedTokens = this._tokens.filter(token => token.selected);
724724
const focusedToken = this._tokens.find(token => token.focused);
725+
const tokensToCopy = selectedTokens.length ? selectedTokens : (focusedToken ? [focusedToken] : []);
725726

726-
if (!selectedTokens.length && focusedToken) {
727-
selectedTokens = [focusedToken];
728-
}
729-
730-
if (isCut && !this.readonly) {
731-
const cutResult = this._fillClipboard(ClipboardDataOperation.cut, selectedTokens);
727+
if (isCut && !this.readonly && tokensToCopy.length) {
728+
const cutResult = this._fillClipboard(ClipboardDataOperation.cut, tokensToCopy);
732729

733-
focusedToken && this.deleteToken(focusedToken);
730+
this.deleteToken(tokensToCopy[0]);
734731

735732
return cutResult;
736733
}
737734

738-
return this._fillClipboard(ClipboardDataOperation.copy, selectedTokens);
735+
return this._fillClipboard(ClipboardDataOperation.copy, tokensToCopy);
739736
}
740737

741738
if (isCtrl && e.key.toLowerCase() === "i" && this._tokens.length > 0) {

0 commit comments

Comments
 (0)