Skip to content

Commit f295bd1

Browse files
committed
chore(refactor): ViewerComponent to script setup and ts
Signed-off-by: Max <max@nextcloud.com>
1 parent 907236a commit f295bd1

1 file changed

Lines changed: 54 additions & 105 deletions

File tree

src/components/ViewerComponent.vue

Lines changed: 54 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -21,120 +21,69 @@
2121
:mime
2222
:source
2323
v-bind="$attrs"
24-
@loaded="onLoaded"
24+
@loaded="onLoadedHandler"
2525
@edit="toggleEdit" />
2626
</template>
2727

28-
<script>
28+
<script setup lang="ts">
2929
import { getSharingToken } from '@nextcloud/sharing/public'
30-
import { defineComponent } from 'vue'
30+
import { computed, onMounted, provide, ref, useAttrs } from 'vue'
3131
import EditorReloader from './EditorReloader.vue'
3232
import SourceView from './SourceView.vue'
3333
34-
export default defineComponent({
35-
name: 'ViewerComponent',
36-
components: {
37-
SourceView,
38-
EditorReloader,
39-
},
40-
41-
provide() {
42-
return {
43-
isEmbedded: this.isEmbedded,
44-
}
45-
},
46-
34+
defineOptions({
4735
inheritAttrs: false,
48-
props: {
49-
filename: {
50-
type: String,
51-
default: null,
52-
},
53-
54-
fileid: {
55-
type: Number,
56-
default: null,
57-
},
58-
59-
active: {
60-
type: Boolean,
61-
default: false,
62-
},
63-
64-
autofocus: {
65-
type: Boolean,
66-
// This is a public interface for Viewer we cannot change for now.
67-
// eslint-disable-next-line vue/no-boolean-default
68-
default: true,
69-
},
70-
71-
shareToken: {
72-
type: String,
73-
default: () => getSharingToken(),
74-
},
75-
76-
mime: {
77-
type: String,
78-
default: null,
79-
},
80-
81-
source: {
82-
type: String,
83-
default: undefined,
84-
},
85-
86-
isEmbedded: {
87-
type: Boolean,
88-
default: false,
89-
},
90-
91-
onLoadedHandler: {
92-
type: Function,
93-
default: () => {},
94-
},
95-
},
96-
97-
data() {
98-
return {
99-
hasToggledInteractiveEmbedding: false,
100-
}
101-
},
102-
103-
computed: {
104-
/** @return {boolean} */
105-
useSourceView() {
106-
return (
107-
this.source
108-
&& (!this.fileid
109-
|| this.isEmbedded
110-
|| this.isEncrypted)
111-
&& !this.hasToggledInteractiveEmbedding
112-
)
113-
},
114-
115-
isEncrypted() {
116-
return this.$attrs.e2EeIsEncrypted || false
117-
},
118-
},
119-
120-
mounted() {
121-
if (!this.useSourceView) {
122-
this.onLoaded()
123-
}
124-
},
125-
126-
methods: {
127-
async onLoaded() {
128-
this.onLoadedHandler()
129-
},
130-
131-
toggleEdit() {
132-
this.hasToggledInteractiveEmbedding = true
133-
},
134-
135-
t,
136-
},
13736
})
37+
38+
const {
39+
filename = undefined,
40+
fileid = undefined,
41+
// This is a public interface for Viewer we cannot change for now.
42+
// eslint-disable-next-line vue/no-boolean-default
43+
autofocus = true,
44+
shareToken = getSharingToken(),
45+
mime = undefined,
46+
source = undefined,
47+
onLoadedHandler = () => {},
48+
...props
49+
} = defineProps <{
50+
filename?: string | undefined
51+
fileid?: number | undefined
52+
active: boolean
53+
autofocus?: boolean
54+
shareToken?: string
55+
mime?: string | undefined
56+
source?: string | undefined
57+
isEmbedded: boolean
58+
onLoadedHandler?: () => void
59+
}>()
60+
61+
provide('isEmbedded', props.isEmbedded)
62+
63+
const hasToggledInteractiveEmbedding = ref(false)
64+
65+
const attrs = useAttrs()
66+
const isEncrypted = computed(() => Boolean(attrs.e2EeIsEncrypted))
67+
68+
const useSourceView = computed(() => source
69+
&& (!fileid
70+
|| props.isEmbedded
71+
|| isEncrypted.value)
72+
&& !hasToggledInteractiveEmbedding.value)
73+
74+
onMounted(() => {
75+
if (!useSourceView.value) {
76+
onLoadedHandler()
77+
}
78+
})
79+
80+
/**
81+
* Toggle interactive editing
82+
*/
83+
function toggleEdit() {
84+
hasToggledInteractiveEmbedding.value = true
85+
}
86+
13887
</script>
13988

14089
<style lang="scss" scoped>

0 commit comments

Comments
 (0)