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

Reorder sidebar org list by dragging #1059

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions app/renderer/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ body {
outline: 1px solid rgba(169, 169, 169, 1.000);
}


/* Class for the dragging element of sidebar used in sortablejs */
.sortable-drag {
visibility: hidden;
}

@font-face {
font-family: 'Material Icons';
font-style: normal;
Expand Down
62 changes: 52 additions & 10 deletions app/renderer/js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {ipcRenderer, remote, clipboard} from 'electron';
import path from 'path';

import isDev from 'electron-is-dev';
import Sortable from 'sortablejs';

import * as Messages from '../../resources/messages';

Expand Down Expand Up @@ -76,6 +77,7 @@ class ServerManagerView {
$settingsButton: HTMLButtonElement;
$webviewsContainer: Element;
$backButton: HTMLButtonElement;
$orgsList: HTMLElement;
$dndButton: HTMLButtonElement;
$addServerTooltip: HTMLElement;
$reloadTooltip: HTMLElement;
Expand All @@ -87,8 +89,11 @@ class ServerManagerView {
$sidebar: Element;
$fullscreenPopup: Element;
$fullscreenEscapeKey: string;
$sortableList: Sortable;
isReorderDragging: boolean;
loading: Set<string>;
activeTabIndex: number;
servers: DomainUtil.ServerConf[];
tabs: ServerOrFunctionalTab[];
functionalTabs: Map<string, number>;
tabIndex: number;
Expand Down Expand Up @@ -120,6 +125,7 @@ class ServerManagerView {
this.$dndTooltip = $actionsContainer.querySelector('#dnd-tooltip');

this.$sidebar = document.querySelector('#sidebar');
this.$orgsList = document.querySelector('#tabs-container');

this.$fullscreenPopup = document.querySelector('#fullscreen-popup');
this.$fullscreenEscapeKey = process.platform === 'darwin' ? '^⌘F' : 'F11';
Expand All @@ -128,9 +134,11 @@ class ServerManagerView {
this.loading = new Set();
this.activeTabIndex = -1;
this.tabs = [];
this.servers = DomainUtil.getDomains();
this.presetOrgs = [];
this.functionalTabs = new Map();
this.tabIndex = 0;
this.isReorderDragging = false;
}

async init(): Promise<void> {
Expand Down Expand Up @@ -232,9 +240,35 @@ class ServerManagerView {
}
}

async onEnd(): Promise<void> {
this.isReorderDragging = false;
const newServers: DomainUtil.ServerConf[] = [];
const tabElements = document.querySelectorAll('#tabs-container .tab');
tabElements.forEach((element, index) => {
const oldIndex = Number(element.getAttribute('data-tab-id')) % this.servers.length;
newServers.push(this.servers[oldIndex]);
(element as HTMLElement).dataset.tabId = index.toString();
});
this.servers = newServers;
DomainUtil.batchUpdateDomain(this.servers);
await this.reloadView(false);
}

onStart(evt: Sortable.SortableEvent): void {
this.isReorderDragging = true;
this.$serverIconTooltip[evt.oldIndex].removeAttribute('style');
}

initSidebar(): void {
const showSidebar = ConfigUtil.getConfigItem('showSidebar', true);
this.toggleSidebar(showSidebar);
this.$sortableList = Sortable.create(this.$orgsList, {
dataIdAttr: 'data-sortable-id',
direction: 'vertical',
onStart: this.onStart.bind(this),
onEnd: this.onEnd.bind(this),
forceFallback: true
});
}

// Remove the stale UA string from the disk if the app is not freshly
Expand Down Expand Up @@ -311,23 +345,26 @@ class ServerManagerView {
}
}

async initTabs(): Promise<void> {
const servers = DomainUtil.getDomains();
if (servers.length > 0) {
for (const [i, server] of servers.entries()) {
async initTabs(refresh = true): Promise<void> {
if (refresh) {
this.servers = DomainUtil.getDomains();
}

if (this.servers.length > 0) {
for (const [i, server] of this.servers.entries()) {
this.initServer(server, i);
}

// Open last active tab
let lastActiveTab = ConfigUtil.getConfigItem('lastActiveTab');
if (lastActiveTab >= servers.length) {
if (lastActiveTab >= this.servers.length) {
lastActiveTab = 0;
}

// `checkDomain()` and `webview.load()` for lastActiveTab before the others
await DomainUtil.updateSavedServer(servers[lastActiveTab].url, lastActiveTab);
await DomainUtil.updateSavedServer(this.servers[lastActiveTab].url, lastActiveTab);
this.activateTab(lastActiveTab);
await Promise.all(servers.map(async (server, i) => {
await Promise.all(this.servers.map(async (server, i) => {
// After the lastActiveTab is activated, we load the others in the background
// without activating them, to prevent flashing of server icons
if (i === lastActiveTab) {
Expand Down Expand Up @@ -493,6 +530,11 @@ class ServerManagerView {
}

onHover(index: number): void {
// We don't to show tooltip when dragging is going on in sidebar to reorder
if (this.isReorderDragging) {
return;
}

// `this.$serverIconTooltip[index].textContent` already has realm name, so we are just
// removing the style.
this.$serverIconTooltip[index].removeAttribute('style');
Expand Down Expand Up @@ -682,14 +724,14 @@ class ServerManagerView {
this.$webviewsContainer.textContent = '';
}

async reloadView(): Promise<void> {
async reloadView(refresh = true): Promise<void> {
// Save and remember the index of last active tab so that we can use it later
const lastActiveTab = this.tabs[this.activeTabIndex].props.index;
ConfigUtil.setConfigItem('lastActiveTab', lastActiveTab);

// Destroy the current view and re-initiate it
this.destroyView();
await this.initTabs();
await this.initTabs(refresh);
this.initServerActions();
}

Expand Down Expand Up @@ -850,7 +892,7 @@ class ServerManagerView {
await LinkUtil.openBrowser(new URL('https://zulip.com/help/'));
});

ipcRenderer.on('reload-viewer', this.reloadView.bind(this, this.tabs[this.activeTabIndex].props.index));
ipcRenderer.on('reload-viewer', this.reloadView.bind(this, true));

ipcRenderer.on('reload-current-viewer', this.reloadCurrentView.bind(this));

Expand Down
7 changes: 7 additions & 0 deletions app/renderer/js/utils/domain-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ export function updateDomain(index: number, server: ServerConf): void {
db.push(`/domains[${index}]`, server, true);
}

export function batchUpdateDomain(servers: ServerConf[]): void {
db.delete('/domains');
for (const server of servers) {
db.push('/domains[]', server, true);
}
}

export async function addDomain(server: ServerConf): Promise<void> {
if (server.icon) {
const localIconUrl = await saveServerIcon(server);
Expand Down
Loading