-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathDirectEditing.vue
182 lines (165 loc) Β· 3.96 KB
/
DirectEditing.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<!--
- SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<div id="direct-editor" :class="{'icon-loading': saving}">
<Editor ref="editor"
:initial-session="initialSession"
:file-id="initial.fileId"
:active="true"
:mime="initial.mimetype"
:is-direct-editing="true"
@ready="loaded">
<template v-if="isMobile" #header>
<button class="icon-share" @click="share" />
<button class="icon-close" @click="close" />
</template>
</Editor>
</div>
</template>
<script>
import Vue from 'vue'
import Editor from '../components/Editor.vue'
import { logger } from '../helpers/logger.js'
const log = Vue.observable({
messages: [],
mtime: 0,
})
const callMobileMessage = (messageName, attributes) => {
logger.debug(`callMobileMessage ${messageName}`, { attributes })
let message = messageName
if (typeof attributes !== 'undefined') {
message = {
MessageName: messageName,
Values: attributes,
}
}
let attributesString = null
try {
attributesString = JSON.stringify(attributes)
} catch (e) {
attributesString = null
}
// Forward to mobile handler
if (window.DirectEditingMobileInterface && typeof window.DirectEditingMobileInterface[messageName] === 'function') {
if (attributesString === null || typeof attributesString === 'undefined') {
window.DirectEditingMobileInterface[messageName]()
} else {
window.DirectEditingMobileInterface[messageName](attributesString)
}
}
// iOS webkit fallback
if (window.webkit
&& window.webkit.messageHandlers
&& window.webkit.messageHandlers.DirectEditingMobileInterface) {
window.webkit.messageHandlers.DirectEditingMobileInterface.postMessage(message)
}
window.postMessage(message)
}
window.addEventListener('message', function(message) {
log.messages.push(message.data)
logger.debug('postMessage', { message })
})
export default {
name: 'DirectEditing',
components: { Editor },
data() {
return {
initial: OCP.InitialState.loadState('text', 'file'),
messages: log.messages,
log,
saving: false,
}
},
computed: {
initialSession() {
return JSON.parse(this.initial.session) || null
},
isMobile() {
return (window.DirectEditingMobileInterface || (window.webkit
&& window.webkit.messageHandlers
&& window.webkit.messageHandlers.DirectEditingMobileInterface))
},
},
beforeMount() {
callMobileMessage('loading')
},
mounted() {
document.querySelector('meta[name="viewport"]').setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0')
this.$refs.editor.$on('push:forbidden', () => {
logger.warn('push was forbidden due to invalidated session')
this.reload()
})
},
methods: {
async close() {
this.saving = true
setTimeout(async () => {
await this.$refs.editor.$destroy()
callMobileMessage('close')
}, 0)
},
share() {
callMobileMessage('share')
},
loaded() {
callMobileMessage('loaded')
},
reload() {
callMobileMessage('reload')
},
},
}
</script>
<style lang="scss">
body {
position: fixed;
background-color: var(--color-main-background);
}
#content[class=app-public] {
margin: 0;
margin-top: 0;
}
</style>
<style scoped lang="scss">
#direct-editor {
width: 100%;
height: 100%;
position: fixed;
overflow: auto;
&:deep(.text-editor) {
height: 100%;
top: 0;
}
&:deep(.text-editor__wrapper div.ProseMirror) {
margin-top: 0;
}
}
pre {
width: 100%;
max-width: 700px;
margin: auto;
background-color: var(--color-background-dark);
}
button {
width: var(--default-clickable-area);
height: var(--default-clickable-area);
margin: 0;
background-size: 16px;
border: 0;
background-color: transparent;
opacity: .5;
color: var(--color-main-text);
background-position: center center;
vertical-align: top;
&:hover, &:focus, &:active {
background-color: var(--color-background-dark);
}
&.is-active,
&:hover,
&:focus {
opacity: 1;
}
}
</style>