1
- <template >
2
- <Editor
3
- ref =" editable"
4
- :appearance =" appearance"
5
- :doc =" doc"
6
- :key =" doc.id"
7
- :initialFocus =" initialFocus"
8
- :initialSelections =" initialSelections"
9
- :ro =" ro"
10
- :settings =" settings"
11
- @input =" input"
12
- />
13
- </template >
14
-
15
- <script >
16
- import { defineComponent , inject } from ' vue'
1
+ <script lang="ts">
2
+ import { type Ref , defineComponent , inject } from ' vue'
17
3
import { useStore } from ' vuex'
18
4
import Editor from ' /components/Editor.vue'
19
5
import Doc from ' /src/models/doc'
20
6
import { EDIT_DOCUMENT } from ' /src/store/actions'
21
7
import { useRecentDocs } from ' /src/stores/useRecentDocs'
22
8
23
- const formatTags = (tags , delimiter = ' , ' ) => {
9
+ const formatTags = (tags : string [] , delimiter = ' , ' ) => {
24
10
return tags .map ((tag ) => ` #${tag } ` ).join (delimiter )
25
11
}
26
12
@@ -33,7 +19,7 @@ export default defineComponent({
33
19
initialFocus: {
34
20
type: String ,
35
21
default : () => ' any' ,
36
- validator : (position ) => [' any' , ' start' , ' end' ].includes (position),
22
+ validator : (position : string ) => [' any' , ' start' , ' end' ].includes (position ),
37
23
},
38
24
initialSelections: {
39
25
type: Array ,
@@ -43,75 +29,73 @@ export default defineComponent({
43
29
type: Boolean ,
44
30
},
45
31
},
46
- data () {
47
- return {
48
- editor: null ,
49
- placeholder: new Doc ({ text: formatTags (this .$store .state .context .tags , ' ' ) }),
50
- }
51
- },
52
32
setup(props ) {
53
- const appearance = inject (' appearance' )
33
+ const appearance = inject <Ref <string >>(' appearance' , ref (' auto' ))
34
+ const editor = ref ()
54
35
const router = useRouter ()
55
36
const store = useStore ()
37
+ const placeholder = new Doc ({ text: formatTags (store .state .context .tags , ' ' ) })
56
38
const settings = computed (() => store .state .settings .editor )
57
39
const recentDocs = useRecentDocs ()
58
- const docId = computed (() => props .docId || router .currentRoute .value .params .docId )
59
-
60
- // Todo: Keep a centralized list of docId exclusions.
61
- if (docId .value && docId .value !== ' new' ) {
62
- recentDocs .add (docId .value )
63
- }
40
+ const docId = computed (() => props .docId || router .currentRoute .value .params .docId as string )
41
+ const doc = computed (() => store .getters .decrypted .find ((d : Doc ) => d .id === docId .value ) || placeholder )
42
+ const tags = computed (() => doc .value .tags )
43
+ const header = computed (() => doc .value .headers [0 ])
64
44
65
- return {
66
- appearance: appearance .value === ' october' ? ' dark' : appearance .value ,
67
- settings,
68
- }
69
- },
70
- watch: {
71
- tags: {
72
- deep: true ,
73
- handler () {
74
- this .updateTitle ()
75
- },
76
- },
77
- header () {
78
- this .updateTitle ()
79
- },
80
- },
81
- computed: {
82
- doc () {
83
- return this .$store .getters .decrypted .find ((doc ) => doc .id === (this .docId || this .$route .params .docId )) || this .placeholder
84
- },
85
- tags () {
86
- return this .doc .tags
87
- },
88
- header () {
89
- return this .doc .headers [0 ]
90
- },
91
- },
92
- methods: {
93
- input (text ) {
94
- if (! this .ro ) {
95
- this .$store .commit (EDIT_DOCUMENT , new Doc ({ ... this .doc , text }))
45
+ const onInput = async (text : string ) => {
46
+ if (! props .ro ) {
47
+ store .commit (EDIT_DOCUMENT , new Doc ({ ... doc .value , text }))
96
48
97
- if (! this . docId ) {
98
- this . $ router .replace ({
99
- path: ` /docs/${ this . doc .id } ` ,
49
+ if (! docId . value || docId . value === ' new ' ) {
50
+ await router .replace ({
51
+ path: ` /docs/${doc . value .id } ` ,
100
52
query: {
101
- p: true ,
53
+ p: ' 1 ' ,
102
54
},
103
55
})
56
+
57
+ recentDocs .add (docId .value )
104
58
}
105
59
}
106
- },
107
- updateTitle () {
60
+ }
61
+
62
+ onMounted (() => {
63
+ // Todo: Keep a centralized list of docId exclusions.
64
+ if (docId .value && docId .value !== ' new' ) {
65
+ recentDocs .add (docId .value )
66
+ }
67
+ })
68
+
69
+ watchEffect (() => {
108
70
useHead ({
109
- title: this . header || formatTags (this . doc . tags ) || ' Build your second brain' ,
71
+ title: header . value || formatTags (tags . value ) || ' Build your second brain' ,
110
72
})
111
- },
112
- },
113
- async mounted () {
114
- this .updateTitle ()
73
+ })
74
+
75
+ return {
76
+ appearance: appearance .value === ' october' ? ' dark' : appearance .value ,
77
+ doc ,
78
+ editor ,
79
+ header ,
80
+ onInput ,
81
+ placeholder ,
82
+ settings ,
83
+ tags ,
84
+ }
115
85
},
116
86
})
117
87
</script >
88
+
89
+ <template >
90
+ <Editor
91
+ ref =" editable"
92
+ :appearance =" appearance"
93
+ :doc =" doc"
94
+ :key =" doc.id"
95
+ :initialFocus =" initialFocus"
96
+ :initialSelections =" initialSelections"
97
+ :ro =" ro"
98
+ :settings =" settings"
99
+ @input =" onInput"
100
+ />
101
+ </template >
0 commit comments