-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathTextureAppChrome.js
170 lines (149 loc) · 4.19 KB
/
TextureAppChrome.js
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
import { Component, DefaultDOMElement, platform, Router, domHelpers } from 'substance'
import { throwMethodIsAbstract } from './kit'
import Texture from './Texture'
export default class TextureAppChrome extends Component {
constructor (...args) {
super(...args)
if (this.props.enableRouting) {
this._router = new Router()
}
// TODO: rethink how configuration is loaded
this._config = Texture.getConfiguration()
}
getChildContext () {
return this._childContext || {}
}
getInitialState () {
return {
archive: undefined,
error: undefined
}
}
didMount () {
this._init(err => {
// if debug is turned on do not 'forward' to an error display and instead
// leave the app in its failed state
if (err) {
console.error(err)
// TODO: we need to make sure that we disable 'debug' when bundling the release version
if (!this.props.debug) {
this.setState({ error: err })
}
}
})
// Note: adding global handlers causes problems in the test suite
if (!platform.test) {
DefaultDOMElement.getBrowserWindow().on('keydown', this._keyDown, this)
DefaultDOMElement.getBrowserWindow().on('drop', domHelpers.stopAndPrevent, this)
DefaultDOMElement.getBrowserWindow().on('dragover', domHelpers.stopAndPrevent, this)
}
if (this._router) {
this._router.start()
}
this.handleActions({
'save': this._handleSave
})
}
dispose () {
DefaultDOMElement.getBrowserWindow().off(this)
}
_getBuffer () {
throwMethodIsAbstract()
}
_getStorage () {
throwMethodIsAbstract()
}
_loadArchive (archiveId, context, cb) {
const ArchiveClass = this._getArchiveClass()
let storage = this._getStorage()
let buffer = this._getBuffer()
let archive = new ArchiveClass(storage, buffer, context, this._config)
// HACK: this should be done earlier in the lifecycle (after first didMount)
// and later disposed properly. However we can accept this for now as
// the app lives as a singleton atm.
archive.on('archive:changed', this._archiveChanged, this)
// Don't catch exception in debug mode
const _afterLoad = (err, archive) => {
if (err) return cb(err)
if (this.props.isReadOnly) {
archive.isReadOnly = true
}
cb(null, archive)
}
if (this.props.debug) {
archive.load(archiveId, _afterLoad)
} else {
try {
archive.load(archiveId, _afterLoad)
} catch (err) {
this.setState({
error: err
})
console.error(err)
}
}
}
_init (cb) {
if (!cb) cb = (err) => { if (err) throw err }
this._setupChildContext((err, context) => {
if (err) return cb(err)
this._initContext(context, (err, context) => {
if (err) return cb(err)
this._loadArchive(this.props.archiveId, context, (err, archive) => {
if (err) return cb(err)
this._initArchive(archive, context, (err, archive) => {
if (err) return cb(err)
this._childContext = context
this.setState({ archive })
this._afterInit()
this.emit('archive:ready')
})
})
})
})
}
_setupChildContext (cb) {
cb(null, { router: this._router })
}
_initContext (context, cb) {
cb(null, context)
}
_initArchive (archive, context, cb) {
cb(null, archive)
}
_afterInit () {
// Update window title after archive loading to display title
this._updateTitle()
}
_archiveChanged () {
this._updateTitle()
}
_handleSave () {
this._save((err) => {
if (err) console.error(err)
})
}
_save (cb) {
this.state.archive.save((err, update) => {
if (err) return cb(err)
this._updateTitle()
cb(null, update)
})
}
_updateTitle () {}
_keyDown (event) {
// TODO: should this really be suppressed here?
if (event.key === 'Dead') return
if (this._handleKeydown) {
this._handleKeydown(event)
}
}
_handleKeydown (event) {
let handled = false
handled = this.refs.texture._handleKeydown(event)
if (handled) {
event.preventDefault()
event.stopPropagation()
}
}
}