Compare commits

...

1 Commits

Author SHA1 Message Date
ZaneYork ecebf360b3 Add text-plain mode 2020-10-16 15:32:07 +08:00
2 changed files with 79 additions and 46 deletions

View File

@ -9,6 +9,7 @@ if (window.ace) {
// load required Ace plugins // load required Ace plugins
require('ace-builds/src-noconflict/mode-json') require('ace-builds/src-noconflict/mode-json')
require('ace-builds/src-noconflict/mode-text')
require('ace-builds/src-noconflict/ext-searchbox') require('ace-builds/src-noconflict/ext-searchbox')
// embed Ace json worker // embed Ace json worker

View File

@ -68,7 +68,7 @@ textmode.create = function (container, options = {}) {
// TODO: make the option options.ace deprecated, it's not needed anymore (see #309) // TODO: make the option options.ace deprecated, it's not needed anymore (see #309)
// determine mode // determine mode
this.mode = (options.mode === 'code') ? 'code' : 'text' this.mode = (options.mode === 'code') ? 'code' : ((options.mode === 'text') ? 'text' : 'text-plain')
if (this.mode === 'code') { if (this.mode === 'code') {
// verify whether Ace editor is available and supported // verify whether Ace editor is available and supported
if (typeof _ace === 'undefined') { if (typeof _ace === 'undefined') {
@ -132,37 +132,41 @@ textmode.create = function (container, options = {}) {
this.frame.appendChild(this.menu) this.frame.appendChild(this.menu)
// create format button // create format button
const buttonFormat = document.createElement('button') if(this.mode !== 'text-plain') {
buttonFormat.type = 'button' const buttonFormat = document.createElement('button')
buttonFormat.className = 'jsoneditor-format' buttonFormat.type = 'button'
buttonFormat.title = translate('formatTitle') buttonFormat.className = 'jsoneditor-format'
this.menu.appendChild(buttonFormat) buttonFormat.title = translate('formatTitle')
buttonFormat.onclick = () => { this.menu.appendChild(buttonFormat)
try { buttonFormat.onclick = () => {
me.format() try {
me._onChange() me.format()
} catch (err) { me._onChange()
me._onError(err) } catch (err) {
me._onError(err)
}
} }
} }
// create compact button // create compact button
const buttonCompact = document.createElement('button') if(this.mode !== 'text-plain') {
buttonCompact.type = 'button' const buttonCompact = document.createElement('button')
buttonCompact.className = 'jsoneditor-compact' buttonCompact.type = 'button'
buttonCompact.title = translate('compactTitle') buttonCompact.className = 'jsoneditor-compact'
this.menu.appendChild(buttonCompact) buttonCompact.title = translate('compactTitle')
buttonCompact.onclick = () => { this.menu.appendChild(buttonCompact)
try { buttonCompact.onclick = () => {
me.compact() try {
me._onChange() me.compact()
} catch (err) { me._onChange()
me._onError(err) } catch (err) {
me._onError(err)
}
} }
} }
// create sort button // create sort button
if (this.options.enableSort) { if (this.options.enableSort && this.mode !== 'text-plain') {
const sort = document.createElement('button') const sort = document.createElement('button')
sort.type = 'button' sort.type = 'button'
sort.className = 'jsoneditor-sort' sort.className = 'jsoneditor-sort'
@ -174,7 +178,7 @@ textmode.create = function (container, options = {}) {
} }
// create transform button // create transform button
if (this.options.enableTransform) { if (this.options.enableTransform && this.mode !== 'text-plain') {
const transform = document.createElement('button') const transform = document.createElement('button')
transform.type = 'button' transform.type = 'button'
transform.title = translate('transformTitleShort') transform.title = translate('transformTitleShort')
@ -186,22 +190,24 @@ textmode.create = function (container, options = {}) {
} }
// create repair button // create repair button
const buttonRepair = document.createElement('button') if(this.mode !== 'text-plain') {
buttonRepair.type = 'button' const buttonRepair = document.createElement('button')
buttonRepair.className = 'jsoneditor-repair' buttonRepair.type = 'button'
buttonRepair.title = translate('repairTitle') buttonRepair.className = 'jsoneditor-repair'
this.menu.appendChild(buttonRepair) buttonRepair.title = translate('repairTitle')
buttonRepair.onclick = () => { this.menu.appendChild(buttonRepair)
try { buttonRepair.onclick = () => {
me.repair() try {
me._onChange() me.repair()
} catch (err) { me._onChange()
me._onError(err) } catch (err) {
me._onError(err)
}
} }
} }
// create undo/redo buttons // create undo/redo buttons
if (this.mode === 'code') { if (this.mode === 'code' || this.mode === 'text-plain') {
// create undo button // create undo button
const undo = document.createElement('button') const undo = document.createElement('button')
undo.type = 'button' undo.type = 'button'
@ -234,7 +240,7 @@ textmode.create = function (container, options = {}) {
}) })
} }
if (this.mode === 'code') { if (this.mode === 'code' || this.mode === 'text-plain') {
const poweredBy = document.createElement('a') const poweredBy = document.createElement('a')
poweredBy.appendChild(document.createTextNode('powered by ace')) poweredBy.appendChild(document.createTextNode('powered by ace'))
poweredBy.href = 'https://ace.c9.io/' poweredBy.href = 'https://ace.c9.io/'
@ -258,7 +264,7 @@ textmode.create = function (container, options = {}) {
this.frame.appendChild(this.content) this.frame.appendChild(this.content)
this.container.appendChild(this.frame) this.container.appendChild(this.frame)
if (this.mode === 'code') { if (this.mode === 'code' || this.mode === 'text-plain') {
this.editorDom = document.createElement('div') this.editorDom = document.createElement('div')
this.editorDom.style.height = '100%' // TODO: move to css this.editorDom.style.height = '100%' // TODO: move to css
this.editorDom.style.width = '100%' // TODO: move to css this.editorDom.style.width = '100%' // TODO: move to css
@ -271,7 +277,12 @@ textmode.create = function (container, options = {}) {
aceEditor.setOptions({ readOnly: isReadOnly }) aceEditor.setOptions({ readOnly: isReadOnly })
aceEditor.setShowPrintMargin(false) aceEditor.setShowPrintMargin(false)
aceEditor.setFontSize('13px') aceEditor.setFontSize('13px')
aceSession.setMode('ace/mode/json') if(this.mode === 'text-plain') {
aceSession.setMode('ace/mode/text')
}
else{
aceSession.setMode('ace/mode/json')
}
aceSession.setTabSize(this.indentation) aceSession.setTabSize(this.indentation)
aceSession.setUseSoftTabs(true) aceSession.setUseSoftTabs(true)
aceSession.setUseWrapMode(true) aceSession.setUseWrapMode(true)
@ -691,17 +702,24 @@ textmode.compact = function () {
*/ */
textmode.format = function () { textmode.format = function () {
const json = this.get() const json = this.get()
const text = JSON.stringify(json, null, this.indentation) if(this.mode !== 'text-plain') {
this.updateText(text) const text = JSON.stringify(json, null, this.indentation)
this.updateText(text)
}
else {
this.updateText(json)
}
} }
/** /**
* Repair the code in the text editor * Repair the code in the text editor
*/ */
textmode.repair = function () { textmode.repair = function () {
const text = this.getText() if(this.mode !== 'text-plain') {
const repairedText = repair(text) const text = this.getText()
this.updateText(repairedText) const repairedText = repair(text)
this.updateText(repairedText)
}
} }
/** /**
@ -731,7 +749,12 @@ textmode.resize = function () {
* @param {*} json * @param {*} json
*/ */
textmode.set = function (json) { textmode.set = function (json) {
this.setText(JSON.stringify(json, null, this.indentation)) if(this.mode !== 'text-plain') {
this.setText(JSON.stringify(json, null, this.indentation))
}
else{
this.setText(json)
}
} }
/** /**
@ -829,6 +852,9 @@ textmode.updateText = function (jsonText) {
* Throws an exception when no JSON schema is configured * Throws an exception when no JSON schema is configured
*/ */
textmode.validate = function () { textmode.validate = function () {
if(this.mode === 'text-plain') {
return
}
let schemaErrors = [] let schemaErrors = []
let parseErrors = [] let parseErrors = []
let json let json
@ -1063,5 +1089,11 @@ export const textModeMixins = [
mixin: textmode, mixin: textmode,
data: 'text', data: 'text',
load: load load: load
},
{
mode: 'text-plain',
mixin: textmode,
data: 'text',
load: load
} }
] ]