forked from MichiSpebach/mammutmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorialAddRawDataFields.ts
More file actions
76 lines (64 loc) · 2.81 KB
/
tutorialAddRawDataFields.ts
File metadata and controls
76 lines (64 loc) · 2.81 KB
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
import { map } from '../dist/core/Map'
import { Box, MenuItemFile, applicationMenu, applicationSettings } from '../dist/pluginFacade'
// TODO make something like this possible:
/*import { plugin } from '../dist/pluginFacade'
import { thisPlugin } from '../dist/pluginFacade'
plugin.getName()
plugin.getMenuItemFolder()
applicationMenu.addMenuItemTo(plugin.getMenuItemFolder(), ...)
plugin.applicationMenuItemFolder.addItem(...)*/
applicationMenu.addMenuItemTo('tutorialAddRawDataFields.js', new MenuItemFile({label: 'setAnyBoxMapDataField', click: () => setAnyBoxMapDataField()}))
applicationMenu.addMenuItemTo('tutorialAddRawDataFields.js', new MenuItemFile({label: 'setAnyProjectSetting', click: () => setAnyProjectSetting()}))
applicationMenu.addMenuItemTo('tutorialAddRawDataFields.js', new MenuItemFile({label: 'setAnyApplicationSetting', click: () => setAnyApplicationSetting()}))
async function setAnyBoxMapDataField(): Promise<void> {
if (!map) {
console.warn('tutorialAddRawDataFields.js plugin: no map is loaded')
return
}
const box: Box = map.getRootFolder()
const fieldName = 'anyField'
let fieldValue: unknown = box.getMapData().getRawField(fieldName)
if (typeof fieldValue === 'number') {
console.log(`${fieldName} is a number and its value is ${fieldValue}`)
fieldValue++
} else {
console.log(`${fieldName} is ${fieldValue}`)
fieldValue = 0
}
box.getMapData().setRawField(fieldName, fieldValue)
await box.saveMapData() // don't forget to call box.saveMapData()
console.log(`set '${fieldName}' to '${fieldValue}' and saved to '${box.getMapDataFilePath()}'`)
}
/**
* saving fields to projectSettings is basically the same as saving them to the mapData of the rootFolder
* though projectSettings directly calls saveToFileSystem()
*/
async function setAnyProjectSetting(): Promise<void> {
if (!map) {
console.warn('addAnyDataFields.js plugin: no map(=project) is loaded')
return
}
const fieldName = 'anyField'
let fieldValue: unknown = map.getProjectSettings().getRawField(fieldName)
if (typeof fieldValue === 'number') {
console.log(`${fieldName} is a number and its value is ${fieldValue}`)
fieldValue++
} else {
console.log(`${fieldName} is ${fieldValue}`)
fieldValue = 0
}
await map.getProjectSettings().setRawFieldAndSave(fieldName, fieldValue)
console.log(`set '${fieldName}' to '${fieldValue}' and saved to '${map.getProjectSettings().getProjectSettingsFilePath()}'`)
}
async function setAnyApplicationSetting(): Promise<void> {
let anyValue: unknown = applicationSettings.getRawField('anyField')
if (typeof anyValue === 'number') {
console.log(`anyField is a number and its value is ${anyValue}`)
anyValue++
} else {
console.log(`anyField is ${anyValue}`)
anyValue = 0
}
await applicationSettings.setRawField('anyField', anyValue)
console.log(`set and saved anyField to ${anyValue}`)
}