Skip to content

Commit 6a9015b

Browse files
committed
Predictable stores and remove file extensions
1 parent cc72325 commit 6a9015b

18 files changed

+109
-81
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ const router = new Router({
4848
routes: [
4949
createRoute({
5050
requireContext: require.context('./..', true, /.demo.vue$/),
51-
path: '/demo'
51+
path: '/demo',
52+
hideFileExtensions: true, // optional, hides file extensions in list.
5253
}),
5354
]
5455
})
@@ -73,7 +74,7 @@ if (process.env.NODE_ENV !== 'production') {
7374
children: [
7475
createRoute({
7576
requireContext: require.context('./..', true, /.demo.vue$/),
76-
path: '/demo'
77+
path: '/demo',
7778
}),
7879
],
7980
})

demo/routes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ const router = new Router({
1010
createRoute({
1111
requireContext: require.context('./tree', true, /.vue$/),
1212
path: '/demo',
13+
hideFileExtensions: true,
1314
}),
1415
createRoute({
1516
requireContext: require.context('./../src', true, /.demo.vue$/),
1617
path: '/src',
18+
hideFileExtensions: true,
1719
}),
1820
],
1921
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-book",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"description": "Tree view for your demo components",
55
"main": "index.js",
66
"private": false,

src/app.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import VueBookRouteFactory from './classes/Main/VueBookRouteFactory'
22
import './scss/app.scss'
33
import './font-awesome-config'
4-
import { VueBookConfig } from '@/classes/Main/VueBookConfig'
4+
import { VueBookConfig } from './classes/Main/VueBookConfig'
55

66
export function createRoute (options: Partial<VueBookConfig>) {
77
const config = new VueBookConfig(options)
88

99
return VueBookRouteFactory.create(config)
1010
}
1111

12-
/** @deprecated use VueBook.createRoute instead */
12+
/** @deprecated use createRoute instead */
1313
export default function (requireContext: any, path: string | RegExp) {
1414
return createRoute(new VueBookConfig({
1515
requireContext,
1616
path,
17+
hideFileExtensions: true,
1718
}))
1819
}

src/classes/Factory/DemoFolderFactory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import DemoFolder from '../Main/DemoFolder'
1+
import { DemoFolder } from '../Main/DemoFolder'
22
import DemoFileFactory from './DemoFileFactory'
33

44
export default class DemoFolderFactory {

src/classes/Main/DemoFile.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import DemoFolder from './DemoFolder'
1+
import { DemoFolder } from './DemoFolder'
22

33
export default class DemoFile {
44
path: string = ''
@@ -18,6 +18,10 @@ export default class DemoFile {
1818
return this.path.split('/').pop() || ''
1919
}
2020

21+
getFilenameWithoutExtension(): string {
22+
return this.getFilename().split('.')[0]
23+
}
24+
2125
getParentFolderPath (): string {
2226
return this.path.split('/').slice(0, -1).join('/')
2327
}

src/classes/Main/DemoFolder.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import DemoFile from './DemoFile'
22
import DemoFolderMapper from '../Mapper/DemoFolderMapper'
3+
import DemoFileCollection from '@/classes/Main/DemoFileCollection'
34

4-
export default class DemoFolder {
5+
export class DemoFolder {
56
name: string = ''
67
isOpen: boolean = false
78
folders: DemoFolder[] = []
@@ -12,6 +13,24 @@ export default class DemoFolder {
1213
Object.assign(this, data)
1314
}
1415

16+
static createFromDemoFileCollection (demoFileCollection: DemoFileCollection) {
17+
const folderTemporary = new DemoFolder()
18+
19+
// Add all files to temporary folder.
20+
const files = demoFileCollection.demoFiles
21+
files.forEach((node: any) => folderTemporary.addDemoFile(node))
22+
23+
const rootFolder = folderTemporary.folders[0]
24+
console.log('rootFolder', rootFolder)
25+
26+
// Bind nodes to folders.
27+
rootFolder.fillParents()
28+
// Open topmost folder for convenience.
29+
rootFolder.open()
30+
31+
return rootFolder
32+
}
33+
1534
addFile (node: DemoFile, relativePath: string): void {
1635
if (relativePath) {
1736
const folderNameArray = relativePath.split('/')

src/classes/Main/VueBookConfig.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
export class VueBookConfig {
22
requireContext: any
3-
path: string | RegExp
3+
path!: string | RegExp
44
hideFileExtensions: boolean = false
55

66
constructor (data: Partial<VueBookConfig> = {}) {
7+
if (!data.requireContext) {
8+
throw(`'requireContext' is not defined on VueBookConfig`)
9+
}
10+
if (!data.path) {
11+
throw(`'path' is not defined on VueBookConfig`)
12+
}
713
Object.assign(this, data)
814
}
915
}

src/classes/Main/VueBookRouteFactory.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import DemoFile from './DemoFile'
33
import VueBookDemoPage from '../../components/DemoPage/VueBookDemoPage.vue'
44
import DemoFileCollection from './DemoFileCollection'
55
import { RouteConfig } from 'vue-router'
6-
import { VueBookConfig } from '@/classes/Main/VueBookConfig'
6+
import { VueBookConfig } from './VueBookConfig'
7+
import { DemoFolder } from './DemoFolder'
78

89
/**
910
* Creates route for vue-router with all necessary boilerplate.
@@ -13,7 +14,7 @@ export default class VueBookRouteFactory {
1314
const requireContext = vueBookConfig.requireContext
1415
const path = vueBookConfig.path
1516

16-
const demoFilesCollection = new DemoFileCollection({
17+
const demoFileCollection = new DemoFileCollection({
1718
demoFiles: requireContext.keys().map((key: string) => {
1819
return new DemoFile({
1920
path: path + key.substr(1),
@@ -26,9 +27,11 @@ export default class VueBookRouteFactory {
2627
path: path + '*',
2728
component: VueBookDemoPage,
2829
meta: {
29-
demoFilesCollection,
30+
demoFolder: DemoFolder.createFromDemoFileCollection(demoFileCollection),
31+
demoFileCollection,
3032
hideFileExtensions: vueBookConfig.hideFileExtensions,
3133
},
3234
}
3335
}
36+
3437
}

src/classes/Mapper/DemoFolderMapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import DemoFolder from '../Main/DemoFolder'
1+
import { DemoFolder } from '../Main/DemoFolder'
22
import DemoFileMapper from './DemoFileMapper'
33

44
export default class DemoFolderMapper {

src/components/DemoPage/VueBookDemoPage.vue

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
<tree-demo-file-list
3535
v-if="config.mode === DemoPageMode.Tree"
36-
:folder="tree"
36+
:folder="demoFolder"
3737
:listCursor="listCursor"
3838
/>
3939
</div>
@@ -55,11 +55,11 @@ import { configStoreInstance } from '../../store/configStore'
5555
import BookComponentListFolder from '../FileTree/BookComponentListFolder.vue'
5656
import BookComponentListItem from '../FileTree/BookComponentListItem.vue'
5757
58-
import DemoFolder from '../../classes/Main/DemoFolder'
58+
import { DemoFolder } from '../../classes/Main/DemoFolder'
5959
import DemoFile from '../../classes/Main/DemoFile'
6060
import SearchableDemoFileList from '../FileTree/SearchableDemoFileList.vue'
6161
import VueBookResizeLine from '../Service/VueBookResizeLine.vue'
62-
import { DemoPageMode } from './DemoPageConfig'
62+
import DemoPageConfig, { DemoPageMode } from './DemoPageConfig'
6363
import VueBookMenu from './VueBookMenu.vue'
6464
6565
import VueBookInput from './ComInput/VueBookInput.vue'
@@ -69,7 +69,6 @@ import DemoFileCollection from '../../classes/Main/DemoFileCollection'
6969
7070
let lastUpdateTimestamp = 0
7171
72-
7372
export default {
7473
components: {
7574
TreeDemoFileList,
@@ -82,36 +81,30 @@ export default {
8281
},
8382
data () {
8483
return {
84+
demoFolder: null,
8585
listCursor: new ListCursor(),
8686
configStoreInstance: configStoreInstance,
87-
tree: null,
8887
foldersStoreInstance: foldersStoreInstance,
8988
}
9089
},
9190
watch: {
9291
'config': {
93-
handler (value: DemoFolder, oldValue: DemoFolder) {
94-
if (value !== oldValue) {
95-
// Updated from local storage.
96-
return
97-
}
92+
handler (value: DemoPageConfig) {
9893
const update = () => {
9994
if (lastUpdateTimestamp < Math.floor(Date.now()) - 200) {
100-
this.configStoreInstance.setConfig(value)
95+
configStoreInstance.config = value
96+
configStoreInstance.save()
10197
lastUpdateTimestamp = Math.floor(Date.now())
10298
}
103-
}
99+
}
104100
update()
105101
},
106102
deep: true,
107103
},
108-
'tree': {
109-
handler (value: DemoFolder, oldValue: DemoFolder) {
110-
if (value !== oldValue) {
111-
// Updated from local storage.
112-
return
113-
}
104+
'demoFolder': {
105+
handler (value: DemoFolder) {
114106
foldersStoreInstance.openFolders = value.getOpenFolders()
107+
foldersStoreInstance.save()
115108
},
116109
deep: true,
117110
},
@@ -141,16 +134,16 @@ export default {
141134
},
142135
143136
files () {
144-
return this.demoFilesCollection.demoFiles
137+
return this.demoFileCollection.demoFiles
145138
},
146139
147-
demoFilesCollection (): DemoFileCollection {
148-
const demoFilesCollection = this.$route.meta.demoFilesCollection
149-
if (!(demoFilesCollection instanceof DemoFileCollection)) {
150-
throw new Error('No DemoFileCollection found for current route')
140+
demoFileCollection (): DemoFileCollection {
141+
const demoFileCollection = this.$route.meta.demoFileCollection
142+
if (!(demoFileCollection instanceof DemoFileCollection)) {
143+
throw new Error('No demoFileCollection found for current route')
151144
}
152145
153-
return demoFilesCollection
146+
return demoFileCollection
154147
},
155148
filteredFiles (): DemoFile[] {
156149
if (!this.config.searchText) {
@@ -160,6 +153,14 @@ export default {
160153
},
161154
},
162155
methods: {
156+
getDemoFolder (): DemoFolder {
157+
const demoFolder = this.$route.meta.demoFolder
158+
if (!(demoFolder instanceof DemoFolder)) {
159+
throw new Error('No demoFolder found for current route')
160+
}
161+
162+
return demoFolder
163+
},
163164
fileSelected (file: DemoFile): boolean {
164165
const path = file.path.toUpperCase()
165166
const text = this.config.searchText.toUpperCase()
@@ -170,15 +171,6 @@ export default {
170171
const upperCaseLetters = file.getFilename().replace(/[a-z.]/g, '')
171172
return upperCaseLetters.includes(this.config.searchText)
172173
},
173-
174-
renderTree (): DemoFolder {
175-
const tree = new DemoFolder()
176-
177-
const files = this.demoFilesCollection.demoFiles
178-
files.forEach((node: any) => tree.addDemoFile(node))
179-
return tree.folders[0]
180-
},
181-
182174
moveCursor (delta: number) {
183175
const files = this.filteredFiles
184176
@@ -203,7 +195,6 @@ export default {
203195
}
204196
this.listCursor.preSelectedItem = files[nextFileIndex]
205197
},
206-
207198
selectFileUnderCursor () {
208199
const fileUnderCursor = this.listCursor.preSelectedItem
209200
@@ -221,12 +212,12 @@ export default {
221212
const input = this.$refs.searchInput
222213
input && input.$el.focus()
223214
},
224-
225215
created () {
226-
this.tree = this.renderTree()
227-
this.tree.fillParents()
228-
this.tree.open()
229-
this.tree.mergeWithFolders(foldersStoreInstance.openFolders)
216+
foldersStoreInstance.load()
217+
configStoreInstance.load()
218+
const demoFolder = this.getDemoFolder()
219+
demoFolder.mergeWithFolders(foldersStoreInstance.openFolders)
220+
this.demoFolder = demoFolder
230221
},
231222
}
232223
</script>

src/components/FileTree/BookComponentListFolder.demo.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<script>
2727
import BookComponentListFolder from './BookComponentListFolder.vue'
2828
import DemoNode from '../../classes/Main/DemoFile'
29-
import DemoFolder from '../../classes/Main/DemoFolder'
29+
import { DemoFolder } from '../../classes/Main/DemoFolder'
3030
import DemoFolderFactory from '../../classes/Factory/DemoFolderFactory'
3131
3232
export default {

src/components/FileTree/BookComponentListFolder.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<script lang="ts">
4343
import vmFile from './BookComponentListItem.vue'
4444
45-
import DemoFolder from '../../classes/Main/DemoFolder'
45+
import { DemoFolder } from '../../classes/Main/DemoFolder'
4646
import { ObjectHelpers } from 'asva-helpers'
4747
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
4848
import {
@@ -58,7 +58,6 @@ export default {
5858
FontAwesomeIcon,
5959
vmFile,
6060
},
61-
inject: ['foldersStoreInstance'],
6261
props: {
6362
folder: {
6463
type: DemoFolder,

src/components/FileTree/BookComponentListItem.vue

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
:class="isActive && 'book-component-list-item--active'"
66
:to="file.path"
77
>
8-
<font-awesome-icon class="book-component-list-item__icon"
9-
:icon="faFile"/>
10-
<span class="book-component-list-item__file-name">{{ file.getFilename()
11-
}}</span>
8+
<font-awesome-icon
9+
class="book-component-list-item__icon"
10+
:icon="faFile"
11+
/>
12+
<span class="book-component-list-item__file-name">
13+
{{ name }}
14+
</span>
1215
</router-link>
1316
</template>
1417

@@ -28,6 +31,12 @@ export default {
2831
},
2932
},
3033
computed: {
34+
name () {
35+
if (this.$route.meta.hideFileExtensions) {
36+
return this.file.getFilenameWithoutExtension()
37+
}
38+
return this.file.getFilename()
39+
},
3140
faFile () {
3241
return faFile
3342
},

0 commit comments

Comments
 (0)