This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
107 lines (91 loc) · 2.74 KB
/
main.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
/* Copyright (c) 2013-present The TagSpaces Authors.
* Use of this source code is governed by the MIT license which can be found in the LICENSE.txt file. */
/* globals JSONEditor, marked, sendMessageToHost, initI18N, $, getParameterByName, handleLinks */
sendMessageToHost({ command: 'loadDefaultTextContent' });
let jsonEditor;
let isViewer = true;
let filePath;
const $document = $(document);
$document.ready(() => {
const locale = getParameterByName('locale');
initI18N(locale, 'ns.editorJSON.json');
if (isViewer) {
$document.dblclick(() => {
sendMessageToHost({ command: 'editDocument' });
});
}
$('#markdownHelpModal').off();
$('#markdownHelpModal').on('show.bs.modal', () => {
$.ajax({
url: 'libs/jsoneditor/docs/shortcut_keys.md',
type: 'GET'
})
.done(jsonData => {
if (marked) {
const modalBody = $('#markdownHelpModal .modal-body');
modalBody.html(marked(jsonData, { sanitize: true }));
handleLinks(modalBody);
} else {
console.log('markdown to html transformer not found');
}
})
.fail(data => {
console.warn('Loading file failed ' + data);
});
});
$('#jsonHelpButton').off();
$('#jsonHelpButton').on('click', () => {
$('#markdownHelpModal').modal({ show: true });
});
});
function contentChanged() {
sendMessageToHost({ command: 'contentChangedInEditor', filepath: filePath });
}
function getContent() {
if (jsonEditor) {
return JSON.stringify(jsonEditor.get());
}
}
function setContent(jsonContentParam, filePathParam, isViewMode) {
const UTF8_BOM = '\ufeff';
let jsonContent = jsonContentParam;
if (jsonContent.indexOf(UTF8_BOM) === 0) {
jsonContent = jsonContent.substring(1, jsonContent.length);
}
filePath = filePathParam;
try {
jsonContent = JSON.parse(jsonContent);
} catch (e) {
console.log('Error parsing JSON document. ' + e);
return false;
}
const options = {
search: true,
history: true,
mode: isViewer ? 'view' : 'tree',
// modes: ['code' , 'form' , 'text' , 'tree' , 'view'] , // allowed modes
onError: err => {
console.warn(err.toString());
},
onChange: contentChanged
};
const container = document.getElementById('jsonEditor');
if (
!!Object.keys(jsonContent) &&
(typeof jsonContent !== 'function' || jsonContent === null)
) {
// console.debug(Object.keys(jsonContent));
jsonEditor = new JSONEditor(container, options, jsonContent);
} else {
throw new TypeError('Object.keys called on non-object');
}
viewerMode(isViewMode);
}
function viewerMode(isViewerMode) {
isViewer = isViewerMode;
if (isViewerMode) {
jsonEditor.setMode('view');
} else {
jsonEditor.setMode('tree');
}
}