This repository has been archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathjsonEditorPlugin.js
155 lines (141 loc) · 4.29 KB
/
jsonEditorPlugin.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
/*******************************************************************************
* @license
* Copyright (c) 2011, 2012 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*eslint-env browser, amd*/
define([
'orion/plugin',
'edit/content/jsonExplorer',
'orion/Deferred',
'orion/commandsProxy',
'orion/keyBinding',
'orion/webui/littlelib',
'i18n!orion/nls/messages'
], function(PluginProvider, mJSONExplorer, Deferred, mCommandsProxy, mKeyBinding, lib, messages) {
var json, showItem, expandItem, model, explorer, commandsProxy = new mCommandsProxy.CommandsProxy();
var EDITOR_ID = "orion.editor.json"; //$NON-NLS-0$
function updateModel(item, expand) {
if (model) {
showItem = item;
expandItem = expand;
return model.setText(JSON.stringify(json, null, "\t")); //$NON-NLS-0$
}
return new Deferred().reject();
}
function updateExplorer() {
if (model) {
model.getText().then(function(text) {
json = {};
if (!explorer) {
document.body.id = "JSONEditor";
explorer = new mJSONExplorer.JSONExplorer({parentId: document.body.id, update: updateModel});
}
try {
json = JSON.parse(text);
} catch (e) {
explorer.emptyMessage = "Error parsing JSON text: " + e.message;
}
explorer.display(json);
if (showItem) {
if (expandItem) {
explorer.expandItem(showItem).then(function() {
explorer.reveal(showItem);
});
} else {
explorer.reveal(showItem);
}
}
});
}
}
var headers = {
name: "Orion JSON Editor Plugin",
version: "1.0", //$NON-NLS-0$
description: "This plug-in provides an editor for JSON files."
};
var provider = new PluginProvider(headers);
provider.registerService("orion.edit.editor", { //$NON-NLS-0$
setTextModel: function(textModel) {
model = textModel;
updateExplorer();
//TODO the commands proxy should not be the text model
commandsProxy.setProxy(model);
// hold on to the text model
return new Deferred();
},
onChanged: function(evt) {
updateExplorer();
},
registerKeys: function(keys) {
var bindings = [];
keys.forEach(function(key) {
bindings.push(new mKeyBinding.KeyBinding(key.keyCode, key.mod1, key.mod2, key.mod3, key.mod4));
});
commandsProxy.setKeyBindings(bindings);
}
}, {
id: EDITOR_ID,
name: messages["Orion JSON Editor"], //$NON-NLS-0$
nls: "orion/nls/messages", //$NON-NLS-0$
uriTemplate: "../edit/edit.html#{,Location,params*},editor=" + EDITOR_ID //$NON-NLS-0$
});
provider.registerService("orion.navigate.openWith", {}, { //$NON-NLS-0$
editor: EDITOR_ID,
contentType: ["application/json"] //$NON-NLS-0$
});
provider.registerService("orion.edit.command", { //$NON-NLS-0$
run : function(text) {
var key = "NewKey";
var newValue = "NewValue", value;
var item = explorer.getNavHandler().currentModel() || explorer.model._root;
while (item) {
value = item.value;
if (Array.isArray(value)) {
key = value.length + "";
value.push(newValue);
} else if (typeof value === "object") { //$NON-NLS-0$
value[key] = newValue;
} else {
item = item.parent;
continue;
}
updateModel({key: key, value: newValue, id: item.id + "-" + key, parent: item}); //$NON-NLS-0$
break;
}
return null;
}
}, {
name : "Add",
contentTypes: ["application/json"], //$NON-NLS-0$
editor: EDITOR_ID,
key : [ "a", true ] //$NON-NLS-0$
});
provider.registerService("orion.edit.command", { //$NON-NLS-0$
run : function(text) {
var item = explorer.getNavHandler().currentModel();
if (item) {
var value = item.parent.value;
if (Array.isArray(value)) {
value.splice(item.key, 1);
} else {
delete value[item.key];
}
updateModel(item.parent, true);
}
return null;
}
}, {
name : "Delete",
contentTypes: ["application/json"], //$NON-NLS-0$
editor: EDITOR_ID,
key : [ 46 ] //$NON-NLS-0$
});
provider.connect();
});