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

Make @finos/perspective-workspace widget title editable by doubleclick #891

Merged
merged 1 commit into from
Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
73 changes: 65 additions & 8 deletions packages/perspective-workspace/src/js/workspace/tabbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import {ArrayExt} from "@phosphor/algorithm";
import {ElementExt} from "@phosphor/domutils";
import {TabBar} from "@phosphor/widgets";
import {TabBarActions} from "./tabbarrenderer";
import {TabBarItems, DEFAULT_TITLE} from "./tabbarrenderer";

export class PerspectiveTabBar extends TabBar {
constructor(options = {}) {
Expand All @@ -31,9 +31,20 @@ export class PerspectiveTabBar extends TabBar {
handleEvent(event) {
this.retargetEvent(event);
switch (event.type) {
case "contextmenu":
const widget = this.currentTitle.owner;
this.parent.parent.parent.showContextMenu(widget, event);
event.preventDefault();
break;

case "dblclick":
if (event.target.id === TabBarItems.Label) {
this.onTitleChangeRequest(event);
}
break;

case "mousedown":
const action = event.target.id;
if (action === TabBarActions.Config) {
if (event.target.id === TabBarItems.Config) {
const tabs = this.contentNode.children;

// Find the index of the released tab.
Expand All @@ -53,6 +64,55 @@ export class PerspectiveTabBar extends TabBar {
super.handleEvent(event);
}

onTitleChangeRequest(event) {
const oldValue = event.target.value;

const stopEvents = event => event.stopPropagation();

const onCancel = () => {
this.title.label = oldValue;
event.target.value = oldValue;
event.target.setAttribute("readonly", "");
removeEventListeners();
};

const onEnter = event => {
if (event.keyCode === 13) {
removeEventListeners();
this.currentTitle.label = event.target.value;
event.target.value = event.target.value || DEFAULT_TITLE;
event.target.setAttribute("readonly", "");
event.target.blur();
}
};

const listeners = {
mousemove: stopEvents,
mousedown: stopEvents,
mouseup: stopEvents,
keydown: onEnter,
blur: onCancel
};

const removeEventListeners = () => {
for (let eventName in listeners) {
event.target.removeEventListener(eventName, listeners[eventName]);
}
};

for (let eventName in listeners) {
event.target.addEventListener(eventName, listeners[eventName]);
}

event.target.removeAttribute("readonly");

if (oldValue === DEFAULT_TITLE) {
event.target.value = "";
}

event.target.focus();
}

onResize(msg) {
super.onResize(msg);
this.checkCondensed(msg);
Expand All @@ -79,10 +139,7 @@ export class PerspectiveTabBar extends TabBar {
}

_addEventListeners() {
this.node.addEventListener("contextmenu", event => {
const widget = this.currentTitle.owner;
this.parent.parent.parent.showContextMenu(widget, event);
event.preventDefault();
});
this.node.addEventListener("dblclick", this);
this.node.addEventListener("contextmenu", this);
}
}
13 changes: 10 additions & 3 deletions packages/perspective-workspace/src/js/workspace/tabbarrenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@
import {h} from "@phosphor/virtualdom";
import {TabBar} from "@phosphor/widgets";

export const TabBarActions = {
Config: "config"
export const TabBarItems = {
Config: "config",
Label: "label"
};

export const DEFAULT_TITLE = "[untitled]";

export class PerspectiveTabBarRenderer extends TabBar.Renderer {
constructor(maximized) {
super();
this.maximized = maximized;
}

renderLabel(data) {
return h.input({className: "p-TabBar-tabLabel", readonly: true, id: TabBarItems.Label, value: data.title.label || DEFAULT_TITLE});
}

renderTab(data) {
const title = data.title.caption;
const key = this.createTabKey(data);
Expand All @@ -38,6 +45,6 @@ export class PerspectiveTabBarRenderer extends TabBar.Renderer {
}

renderConfigIcon() {
return h.div({className: "p-TabBar-tabConfigIcon", id: TabBarActions.Config});
return h.div({className: "p-TabBar-tabConfigIcon", id: TabBarItems.Config});
}
}
1 change: 1 addition & 0 deletions packages/perspective-workspace/src/js/workspace/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class PerspectiveViewerWidget extends Widget {
save() {
return {
...this.viewer.save(),
name: this.title.label,
master: this.master,
table: this.tableName
};
Expand Down
16 changes: 16 additions & 0 deletions packages/perspective-workspace/src/less/tabbar.less
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@

@import "~@finos/perspective-viewer/src/less/variables.less";

.p-TabBar-tabLabel{
background-color: transparent;
border: none;
color: #666;
cursor: pointer;
}

.p-TabBar-tabLabel:focus{
outline: none;
color: #666 !important;
cursor: text;
}
.p-TabBar-tabLabel[value='[untitled]']{
color: #ddd
}

.p-TabBar-tab.p-mod-closable > .p-TabBar-tabCloseIcon:before {
content: var(--close-button--content, "\2715");
font-family: var(--settings--font-family, "Arial");
Expand Down
6 changes: 3 additions & 3 deletions packages/perspective-workspace/test/results/linux.docker.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"index_restore_workspace_with_detail_only": "944c3f5c4d8303e3d5c667f463709759",
"index_restore_workspace_with_detail_only": "1168e9bee0eec492dfd5f50b2e087d84",
"index_restore_workspace_with_master_only": "13ff739449bf9745d43f9af8bfc67c7c",
"index_restore_workspace_with_master_and_detail": "1ec20152c45e0fd7188996f74faac623",
"__GIT_COMMIT__": "12d6d9d08ccd26b8495eb52638bf55553d33d99b"
"index_restore_workspace_with_master_and_detail": "aafccad40bfe1a921c220da2a0e07a30",
"__GIT_COMMIT__": "5e68a41988f611e0c240e0032758cc6cd55ea932"
}