-
Notifications
You must be signed in to change notification settings - Fork 877
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
259 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<template> | ||
<Tabs :list="tabs" :value="current" @remove="removeTabs" @switch="switchTab" @change-list="setTabs"></Tabs> | ||
</template> | ||
|
||
<script> | ||
import { mapState } from 'vuex' | ||
import Tabs from './Tabs' | ||
import File from '@/lib/file' | ||
const blankUri = File.toUri(null) | ||
export default { | ||
name: 'file-tabs', | ||
components: { Tabs }, | ||
data () { | ||
return { | ||
list: [], | ||
current: blankUri | ||
} | ||
}, | ||
methods: { | ||
setTabs (list) { | ||
this.$store.commit('app/setTabs', list) | ||
}, | ||
switchTab (item) { | ||
this.switchFile(item.payload.file) | ||
}, | ||
removeTabs (items) { | ||
const keys = items.map(x => x.key) | ||
const tabs = this.tabs.filter(x => keys.indexOf(x.key) === -1) | ||
this.setTabs(tabs) | ||
}, | ||
addTab (item) { | ||
const tab = this.tabs.find(x => item.key === x.key) | ||
// 没有打开此 Tab,新建一个 | ||
if (!tab) { | ||
this.setTabs(this.tabs.concat([item])) | ||
} | ||
this.current = item.key | ||
}, | ||
switchFile (file) { | ||
this.$store.commit('app/setCurrentFile', file) | ||
} | ||
}, | ||
computed: { | ||
...mapState('app', ['currentFile', 'tabs']) | ||
}, | ||
watch: { | ||
currentFile: { | ||
immediate: true, | ||
handler (file) { | ||
const uri = File.toUri(file) | ||
const item = { | ||
key: uri, | ||
label: file ? file.name : '空白页', | ||
description: file ? file.path : '空白页', | ||
payload: { file }, | ||
} | ||
this.addTab(item) | ||
} | ||
}, | ||
tabs (list) { | ||
if (list.length < 1) { | ||
this.addTab({ | ||
key: blankUri, | ||
label: '空白页', | ||
description: '空白页', | ||
payload: { file: null } | ||
}) | ||
} | ||
const tab = list.find(x => x.key === this.current) | ||
if (!tab) { | ||
const currentFile = list.length > 0 ? list[list.length - 1].payload.file : null | ||
this.switchFile(currentFile) | ||
} | ||
} | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<template> | ||
<div ref="tabs" class="tabs"> | ||
<div | ||
v-for="item in list" | ||
:key="item.key" | ||
:class="{tab: true, current: item.key === value}" | ||
:title="item.description" | ||
:data-key="item.key" | ||
@contextmenu.exact.prevent.stop="showContextMenu(item)" | ||
@click="switchTab(item)"> | ||
<div class="label">{{item.label}}</div> | ||
<div class="close" @click.prevent.stop="removeTabs([item])"> | ||
<y-icon class="close-icon" name="times" title="关闭"></y-icon> | ||
</div> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import Sortable from 'sortablejs' | ||
import 'vue-awesome/icons/times' | ||
export default { | ||
name: 'tabs', | ||
props: { | ||
value: String, | ||
list: Array, | ||
}, | ||
mounted () { | ||
Sortable.create(this.$refs.tabs, { | ||
animation: 250, | ||
ghostClass: 'on-sort', | ||
direction: 'horizontal', | ||
onEnd: ({ oldIndex, newIndex }) => { | ||
this.swapTab(oldIndex, newIndex) | ||
} | ||
}) | ||
}, | ||
methods: { | ||
showContextMenu (item) { | ||
this.$contextMenu.show([ | ||
{ id: 'close', label: '关闭', onClick: () => this.removeTabs([item]) }, | ||
{ id: 'close-other', label: '关闭其他', onClick: () => this.removeOther(item) }, | ||
{ id: 'close-right', label: '关闭到右侧', onClick: () => this.removeRight(item) }, | ||
{ id: 'close-left', label: '关闭到左侧', onClick: () => this.removeLeft(item) }, | ||
{ id: 'close-all', label: '全部关闭', onClick: () => this.removeAll(item) }, | ||
]) | ||
}, | ||
switchTab (item) { | ||
if (item.key !== this.value) { | ||
this.$emit('input', item.key) | ||
this.$emit('switch', item) | ||
} | ||
}, | ||
removeOther (item) { | ||
this.removeTabs(this.list.filter(x => x.key !== item.key)) | ||
}, | ||
removeRight (item) { | ||
this.removeTabs(this.list.slice(this.list.findIndex(x => x.key === item.key) + 1)) | ||
}, | ||
removeLeft (item) { | ||
this.removeTabs(this.list.slice(0, this.list.findIndex(x => x.key === item.key))) | ||
}, | ||
removeAll () { | ||
this.removeTabs(this.list) | ||
}, | ||
removeTabs (items) { | ||
this.$emit('remove', items) | ||
}, | ||
swapTab (oldIndex, newIndex) { | ||
const list = this.list | ||
const tmp = list[oldIndex] | ||
list[oldIndex] = list[newIndex] | ||
list[newIndex] = tmp | ||
this.$emit('change-list', list) | ||
} | ||
}, | ||
} | ||
</script> | ||
|
||
<style scoped> | ||
.tabs { | ||
flex: none; | ||
height: 30px; | ||
background: #333; | ||
display: flex; | ||
} | ||
.tab { | ||
width: 100%; | ||
max-width: 150px; | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
margin-right: 1px; | ||
color: #888; | ||
cursor: default; | ||
font-size: 12px; | ||
background: #282828; | ||
overflow: hidden; | ||
} | ||
.label { | ||
padding: 5px; | ||
white-space: nowrap; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
} | ||
.tab.current { | ||
color: #eee; | ||
background: #1d1f21; | ||
} | ||
.close { | ||
color: #999; | ||
height: 18px; | ||
width: 18px; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
border-radius: 50%; | ||
margin-right: 5px; | ||
} | ||
.close:hover { | ||
color: #aaa; | ||
background: #444; | ||
} | ||
.close-icon { | ||
zoom: .6; | ||
} | ||
.tab.on-sort { | ||
background: rgb(112, 112, 112); | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.