feat: Remove Set Default & Undefault buttons

This commit is contained in:
Aman Harwara
2021-10-01 11:46:56 +05:30
parent a98409a95f
commit 1a92fffcd4
2 changed files with 15 additions and 59 deletions

View File

@@ -4,34 +4,30 @@ import { SNComponent, SNItem, ComponentArea } from '@standardnotes/snjs';
import { isDesktopApplication } from '@/utils'; import { isDesktopApplication } from '@/utils';
import template from '%/directives/editor-menu.pug'; import template from '%/directives/editor-menu.pug';
import { PureViewCtrl } from '@Views/abstract/pure_view_ctrl'; import { PureViewCtrl } from '@Views/abstract/pure_view_ctrl';
import { ComponentMutator } from '@standardnotes/snjs';
interface EditorMenuScope { interface EditorMenuScope {
callback: (component: SNComponent) => void callback: (component: SNComponent) => void;
selectedEditorUuid: string selectedEditorUuid: string;
currentItem: SNItem currentItem: SNItem;
application: WebApplication application: WebApplication;
} }
class EditorMenuCtrl extends PureViewCtrl implements EditorMenuScope { class EditorMenuCtrl extends PureViewCtrl implements EditorMenuScope {
callback!: () => (component: SNComponent) => void;
callback!: () => (component: SNComponent) => void selectedEditorUuid!: string;
selectedEditorUuid!: string currentItem!: SNItem;
currentItem!: SNItem application!: WebApplication;
application!: WebApplication
/* @ngInject */ /* @ngInject */
constructor( constructor($timeout: ng.ITimeoutService) {
$timeout: ng.ITimeoutService,
) {
super($timeout); super($timeout);
this.state = { this.state = {
isDesktop: isDesktopApplication() isDesktop: isDesktopApplication(),
}; };
} }
public isEditorSelected(editor: SNComponent) { public isEditorSelected(editor: SNComponent) {
if(!this.selectedEditorUuid) { if (!this.selectedEditorUuid) {
return false; return false;
} }
return this.selectedEditorUuid === editor.uuid; return this.selectedEditorUuid === editor.uuid;
@@ -43,14 +39,15 @@ class EditorMenuCtrl extends PureViewCtrl implements EditorMenuScope {
$onInit() { $onInit() {
super.$onInit(); super.$onInit();
const editors = this.application.componentManager!.componentsForArea(ComponentArea.Editor) const editors = this.application
.componentManager!.componentsForArea(ComponentArea.Editor)
.sort((a, b) => { .sort((a, b) => {
return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1; return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1;
}); });
const defaultEditor = editors.filter((e) => e.isDefaultEditor())[0]; const defaultEditor = editors.filter((e) => e.isDefaultEditor())[0];
this.setState({ this.setState({
editors: editors, editors: editors,
defaultEditor: defaultEditor defaultEditor: defaultEditor,
}); });
} }
@@ -67,46 +64,9 @@ class EditorMenuCtrl extends PureViewCtrl implements EditorMenuScope {
}); });
} }
toggleDefaultForEditor(editor: SNComponent) {
if (this.state.defaultEditor === editor) {
this.removeEditorDefault(editor);
} else {
this.makeEditorDefault(editor);
}
}
offlineAvailableForComponent(component: SNComponent) { offlineAvailableForComponent(component: SNComponent) {
return component.local_url && this.state.isDesktop; return component.local_url && this.state.isDesktop;
} }
makeEditorDefault(component: SNComponent) {
const currentDefault = this.application.componentManager!
.componentsForArea(ComponentArea.Editor)
.filter((e) => e.isDefaultEditor())[0];
if (currentDefault) {
this.application.changeItem(currentDefault.uuid, (m) => {
const mutator = m as ComponentMutator;
mutator.defaultEditor = false;
});
}
this.application.changeAndSaveItem(component.uuid, (m) => {
const mutator = m as ComponentMutator;
mutator.defaultEditor = true;
});
this.setState({
defaultEditor: component
});
}
removeEditorDefault(component: SNComponent) {
this.application.changeAndSaveItem(component.uuid, (m) => {
const mutator = m as ComponentMutator;
mutator.defaultEditor = false;
});
this.setState({
defaultEditor: null
});
}
} }
export class EditorMenu extends WebDirective { export class EditorMenu extends WebDirective {
@@ -121,7 +81,7 @@ export class EditorMenu extends WebDirective {
callback: '&', callback: '&',
selectedEditorUuid: '=', selectedEditorUuid: '=',
currentItem: '=', currentItem: '=',
application: '=' application: '=',
}; };
} }
} }

View File

@@ -11,11 +11,7 @@
menu-row( menu-row(
ng-repeat='editor in self.state.editors track by editor.uuid' ng-repeat='editor in self.state.editors track by editor.uuid'
action='self.selectComponent(editor)', action='self.selectComponent(editor)',
button-action='self.toggleDefaultForEditor(editor)',
button-class="self.isEditorSelected(editor) ? 'warning' : 'info'",
button-text="self.isEditorDefault(editor) ? 'Undefault' : 'Set Default'",
circle="self.isEditorSelected(editor) && 'success'", circle="self.isEditorSelected(editor) && 'success'",
has-button='self.isEditorSelected(editor) || isEditorDefault(editor)',
label='editor.name', label='editor.name',
subtitle="self.isEditorSelected(editor) && 'Version ' + editor.package_info.version", subtitle="self.isEditorSelected(editor) && 'Version ' + editor.package_info.version",
) )