Skip to content

Commit

Permalink
selectable action
Browse files Browse the repository at this point in the history
  • Loading branch information
kleber-swf committed Sep 3, 2021
1 parent a00a1f9 commit 47fec0f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
1 change: 0 additions & 1 deletion TODO.todo
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ BASIC:
Actions:
[x] actions handler
[x] undo
[ ] toggle grid
[ ] toggle gizmos
[ ] toggle enabled
[ ] show reference
Expand Down
12 changes: 8 additions & 4 deletions src/data/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ export interface Action {
id: string;
label?: string;
icon?: string;
command: () => void;
shortcut?: string;
toggle?: boolean;
command: () => void;
state?: () => any;
}

class ActionsClass {
Expand All @@ -17,9 +19,11 @@ class ActionsClass {

public add(...actions: Action[]) {
actions.forEach(action => {
if (action.shortcut in this.actions)
throw new Error(`There is already an action with shortcut ${action.shortcut}: ${this.actions[action.shortcut].label}`);
this.actions[action.shortcut] = action;
if (action.shortcut) {
if (action.shortcut in this.actions)
throw new Error(`There is already an action with shortcut ${action.shortcut}: ${this.actions[action.shortcut].label}`);
this.actions[action.shortcut] = action;
}
this.actionMap[action.id] = action;
});
}
Expand Down
11 changes: 9 additions & 2 deletions src/editor/actions/button/action-button.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ phed-action-button {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 30px;
min-width: 35px;
border-radius: 4px;
color: $button-text-color;
cursor: pointer;
margin: 0 4px;

&:hover {
background-color: $primary-color;
color: $on-primary;
box-shadow: 0 1px 2px 2px $shadow-color;
padding-bottom: 1px;
}

&:active {
padding-top: 2px;
}

&.selected {
background-color: lighten($primary-color, 10%);
color: $on-primary;
}

.label {
font-size: 12px;
Expand Down
21 changes: 18 additions & 3 deletions src/editor/actions/button/action-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import './action-button.scss';

export class ActionButton extends HTMLElement {
public static readonly tagName: string = 'phed-action-button';

public connectedCallback() { }
private action: Action;

public setAction(action: Action) {
this.action = action;
if (action.icon) {
const icon = this.appendChild(document.createElement('i'));
icon.classList.add('fas', action.icon);
Expand All @@ -16,7 +16,22 @@ export class ActionButton extends HTMLElement {
text.classList.add('label');
text.textContent = action.label;
}
this.onclick = () => action.command();
if (!action.toggle) {
this.onclick = () => action.command();
return;
}
this.onclick = this.toggleSelected.bind(this);
this.updateState();
}

private toggleSelected() {
this.action.command();
this.updateState();
}

private updateState() {
if (this.action.state()) this.classList.add('selected');
else this.classList.remove('selected');
}
}

Expand Down

0 comments on commit 47fec0f

Please sign in to comment.