Skip to content

Commit

Permalink
feat: Add c c to copy focused/selected tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
xcv58 committed Apr 14, 2020
1 parent 6f293a3 commit 266204f
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 73 deletions.
6 changes: 3 additions & 3 deletions src/js/components/Toolbar/Close.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { useTheme } from '@material-ui/styles'
import { TOOLTIP_DELAY } from 'libs'
import { useStore } from 'components/StoreContext'

export default observer((props) => {
export default observer(() => {
const theme = useTheme()
const { tabStore } = useStore()
const { remove, tabDescription, hasFocusedOrSelectedTab } = tabStore
const { tabStore, hasFocusedOrSelectedTab, remove } = useStore()
const { tabDescription } = tabStore
const style = {}
if (hasFocusedOrSelectedTab) {
style.color = theme.palette.secondary.main
Expand Down
3 changes: 1 addition & 2 deletions src/js/components/Toolbar/Reload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { TOOLTIP_DELAY } from 'libs'
import { useStore } from 'components/StoreContext'

export default observer(() => {
const { tabStore } = useStore()
const { reload, hasFocusedOrSelectedTab } = tabStore
const { hasFocusedOrSelectedTab, reload } = useStore()
return (
<Tooltip title='Reload select tab(s)' enterDelay={TOOLTIP_DELAY}>
<div>
Expand Down
2 changes: 2 additions & 0 deletions src/js/libs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,5 @@ export const getDomain = (url) => {
}

export const isProduction = () => process.env.NODE_ENV === 'production'

export const writeToClipboard = (text) => navigator.clipboard.writeText(text)
14 changes: 11 additions & 3 deletions src/js/stores/ShortcutStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default class ShortcutStore {
[
['d d'],
() => {
this.store.tabStore.remove()
this.store.remove()
},
'Close tab'
],
Expand All @@ -114,7 +114,7 @@ export default class ShortcutStore {
[
['r', 'ctrl+r'],
() => {
this.store.tabStore.reload()
this.store.reload()
},
'Reload tab'
],
Expand All @@ -129,7 +129,7 @@ export default class ShortcutStore {
['p', 'ctrl+p'],
(event) => {
preventDefault(event)
this.store.tabStore.togglePin()
this.store.togglePin()
},
'Toogle pin'
],
Expand Down Expand Up @@ -374,6 +374,14 @@ export default class ShortcutStore {
this.store.hiddenWindowStore.updateHideForAllWindows(false)
},
'Expand all windows'
],
[
'c c',
(event) => {
preventDefault(event)
this.store.copyURL()
},
'Copy URL (separated by new line)'
]
]

Expand Down
67 changes: 2 additions & 65 deletions src/js/stores/TabStore.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { action, computed, observable } from 'mobx'
import { activateTab, togglePinTabs } from 'libs'
import { activateTab } from 'libs'
import Store from 'stores'
import log from 'libs/log'
import Tab from './Tab'
import Window from './Window'

export default class TabStore {
store: Store
Expand All @@ -27,12 +26,6 @@ export default class TabStore {
return 'selected tabs'
}

@computed
get hasFocusedOrSelectedTab () {
const { focusedItem } = this.store.focusStore
return this.selection.size > 0 || focusedItem !== null
}

@action
select = (tab) => {
const { id } = tab
Expand Down Expand Up @@ -108,67 +101,11 @@ export default class TabStore {
})
}

// TODO: this abstraction may be useless
@action
activate = (tab) => {
activateTab(tab.id)
}

@action
remove = () => {
const { down, focusedTabId } = this.store.focusStore
const { tabs } = this.store.windowStore
let tabsToRemove = []
if (this.selection.size > 0) {
while (this.selection.has(this.store.focusStore.focusedTabId)) {
down()
if (focusedTabId === this.store.focusStore.focusedTabId) {
this.store.focusStore.defocus()
break
}
}
tabsToRemove = tabs.filter((x) => x.isSelected)
} else {
if (focusedTabId) {
tabsToRemove = tabs.filter((x) => x.isFocused)
down()
}
}
this.unselectAll()
tabsToRemove.forEach((x) => x.remove())
}

@action
reload = () => {
const { focusedItem } = this.store.focusStore
const { tabs } = this.store.windowStore
let tabsToReload = []
if (this.selection.size > 0) {
tabsToReload = tabs.filter((x) => x.isSelected)
} else {
if (focusedItem) {
tabsToReload = tabs.filter((x) => x.isFocused)
}
}
this.unselectAll()
tabsToReload.forEach((x) => x.reload())
}

@action
togglePin = async () => {
const { focusedItem } = this.store.focusStore
if (this.selection.size === 0 && focusedItem) {
log.debug('togglePin for focusedItem:', { focusedItem })
if (focusedItem instanceof Tab) {
await togglePinTabs([focusedItem])
}
if (focusedItem instanceof Window) {
await togglePinTabs(focusedItem.tabs)
}
} else {
await togglePinTabs([...this.selection.values()])
this.unselectAll()
}
}

isTabSelected = ({ id }) => this.selection.has(id)
}
98 changes: 98 additions & 0 deletions src/js/stores/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { action, computed } from 'mobx'
import { togglePinTabs, writeToClipboard } from 'libs'
import log from 'libs/log'
import WindowStore from 'stores/WindowStore'
import SearchStore from 'stores/SearchStore'
import TabStore from 'stores/TabStore'
Expand All @@ -9,6 +12,9 @@ import HoverStore from 'stores/HoverStore'
import HiddenWindowStore from 'stores/HiddenWindowStore'
import FocusStore from 'stores/FocusStore'

import Tab from './Tab'
import Window from './Window'

export default class Store {
windowStore: WindowStore

Expand Down Expand Up @@ -42,4 +48,96 @@ export default class Store {
this.hiddenWindowStore = new HiddenWindowStore(this)
this.focusStore = new FocusStore(this)
}

@action
remove = () => {
const { down, focusedTabId } = this.focusStore
const { tabs } = this.windowStore
let tabsToRemove = []
if (this.tabStore.selection.size > 0) {
while (this.tabStore.selection.has(this.focusStore.focusedTabId)) {
down()
if (focusedTabId === this.focusStore.focusedTabId) {
this.focusStore.defocus()
break
}
}
tabsToRemove = tabs.filter((x) => x.isSelected)
} else {
if (focusedTabId) {
tabsToRemove = tabs.filter((x) => x.isFocused)
down()
}
}
this.tabStore.unselectAll()
tabsToRemove.forEach((x) => x.remove())
}

@action
reload = () => {
const { focusedItem } = this.focusStore
const { tabs } = this.windowStore
const { selection, unselectAll } = this.tabStore
let tabsToReload = []
if (selection.size > 0) {
tabsToReload = tabs.filter((x) => x.isSelected)
} else {
if (focusedItem) {
tabsToReload = tabs.filter((x) => x.isFocused)
}
}
unselectAll()
tabsToReload.forEach((x) => x.reload())
}

@action
togglePin = async () => {
const { focusedItem } = this.focusStore
const { selection, unselectAll } = this.tabStore
if (selection.size === 0 && focusedItem) {
log.debug('togglePin for focusedItem:', { focusedItem })
if (focusedItem instanceof Tab) {
await togglePinTabs([focusedItem])
}
if (focusedItem instanceof Window) {
await togglePinTabs(focusedItem.tabs)
}
} else {
await togglePinTabs([...selection.values()])
unselectAll()
}
}

@computed
get hasFocusedOrSelectedTab () {
return this.tabStore.selection.size > 0 || !!this.focusStore.focusedItem
}

_getFocusedOrSelectedTab = () => {
const { sources } = this.tabStore
if (sources.length) {
return sources
}
const { focusedItem } = this.focusStore
if (!focusedItem) {
return []
}
if (focusedItem instanceof Tab) {
return [focusedItem]
}
if (focusedItem instanceof Window) {
return focusedItem.tabs
}
}

@action
copyURL = async () => {
const tabs = this._getFocusedOrSelectedTab()
if (!tabs || !tabs.length) {
return
}
const text = tabs.map((x) => x.url).join('\n')
await writeToClipboard(text)
this.tabStore.unselectAll()
}
}

0 comments on commit 266204f

Please sign in to comment.