A few more files refactored to ES modules
This commit is contained in:
parent
9ef44bda6b
commit
f321eb54ee
2
index.js
2
index.js
|
@ -1 +1 @@
|
||||||
module.exports = require('./src/js/JSONEditor')
|
module.exports = require('./src/js/JSONEditor').default
|
||||||
|
|
|
@ -1,19 +1,15 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
let Ajv
|
|
||||||
try {
|
|
||||||
Ajv = require('ajv')
|
|
||||||
} catch (err) {
|
|
||||||
// no problem... when we need Ajv we will throw a neat exception
|
|
||||||
}
|
|
||||||
|
|
||||||
const ace = require('./ace') // may be undefined in case of minimalist bundle
|
const ace = require('./ace') // may be undefined in case of minimalist bundle
|
||||||
const VanillaPicker = require('./vanilla-picker') // may be undefined in case of minimalist bundle
|
const VanillaPicker = require('./vanilla-picker') // may be undefined in case of minimalist bundle
|
||||||
|
|
||||||
const treemode = require('./treemode')
|
const treeModeMixins = require('./treemode').treeModeMixins
|
||||||
const textmode = require('./textmode')
|
const textModeMixins = require('./textmode').textModeMixins
|
||||||
const previewmode = require('./previewmode')
|
const previewModeMixins = require('./previewmode').previewModeMixins
|
||||||
const util = require('./util')
|
const util = require('./util')
|
||||||
|
const { tryRequireAjv } = require('./tryRequireAjv')
|
||||||
|
|
||||||
|
const Ajv = tryRequireAjv()
|
||||||
|
|
||||||
if (typeof Promise === 'undefined') {
|
if (typeof Promise === 'undefined') {
|
||||||
console.error('Promise undefined. Please load a Promise polyfill in the browser in order to use JSONEditor')
|
console.error('Promise undefined. Please load a Promise polyfill in the browser in order to use JSONEditor')
|
||||||
|
@ -464,9 +460,9 @@ JSONEditor.registerMode = mode => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// register tree, text, and preview modes
|
// register tree, text, and preview modes
|
||||||
JSONEditor.registerMode(treemode)
|
JSONEditor.registerMode(treeModeMixins)
|
||||||
JSONEditor.registerMode(textmode)
|
JSONEditor.registerMode(textModeMixins)
|
||||||
JSONEditor.registerMode(previewmode)
|
JSONEditor.registerMode(previewModeMixins)
|
||||||
|
|
||||||
// expose some of the libraries that can be used customized
|
// expose some of the libraries that can be used customized
|
||||||
JSONEditor.ace = ace
|
JSONEditor.ace = ace
|
||||||
|
|
|
@ -1,18 +1,29 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const jmespath = require('jmespath')
|
import jmespath from 'jmespath'
|
||||||
const translate = require('./i18n').translate
|
import { translate } from './i18n'
|
||||||
const ModeSwitcher = require('./ModeSwitcher')
|
import ModeSwitcher from './ModeSwitcher'
|
||||||
const ErrorTable = require('./ErrorTable')
|
import { ErrorTable } from './ErrorTable'
|
||||||
const textmode = require('./textmode')[0].mixin
|
import { showSortModal } from './showSortModal'
|
||||||
const showSortModal = require('./showSortModal').showSortModal
|
import { showTransformModal } from './showTransformModal'
|
||||||
const showTransformModal = require('./showTransformModal').showTransformModal
|
import { textModeMixins } from './textmode'
|
||||||
const MAX_PREVIEW_CHARACTERS = require('./constants').MAX_PREVIEW_CHARACTERS
|
import { DEFAULT_MODAL_ANCHOR, MAX_PREVIEW_CHARACTERS, PREVIEW_HISTORY_LIMIT, SIZE_LARGE } from './constants'
|
||||||
const DEFAULT_MODAL_ANCHOR = require('./constants').DEFAULT_MODAL_ANCHOR
|
import {
|
||||||
const SIZE_LARGE = require('./constants').SIZE_LARGE
|
addClassName,
|
||||||
const PREVIEW_HISTORY_LIMIT = require('./constants').PREVIEW_HISTORY_LIMIT
|
debounce,
|
||||||
const util = require('./util')
|
escapeUnicodeChars,
|
||||||
const History = require('./History').History
|
formatSize,
|
||||||
|
isObject,
|
||||||
|
limitCharacters,
|
||||||
|
parse,
|
||||||
|
removeClassName,
|
||||||
|
repair,
|
||||||
|
sort,
|
||||||
|
sortObjectKeys
|
||||||
|
} from './util'
|
||||||
|
import { History } from './History'
|
||||||
|
|
||||||
|
const textmode = textModeMixins[0].mixin
|
||||||
|
|
||||||
// create a mixin with the functions for text mode
|
// create a mixin with the functions for text mode
|
||||||
const previewmode = {}
|
const previewmode = {}
|
||||||
|
@ -55,7 +66,7 @@ previewmode.create = function (container, options = {}) {
|
||||||
// TODO: JSON Schema support
|
// TODO: JSON Schema support
|
||||||
|
|
||||||
// create a debounced validate function
|
// create a debounced validate function
|
||||||
this._debouncedValidate = util.debounce(this.validate.bind(this), this.DEBOUNCE_INTERVAL)
|
this._debouncedValidate = debounce(this.validate.bind(this), this.DEBOUNCE_INTERVAL)
|
||||||
|
|
||||||
this.width = container.clientWidth
|
this.width = container.clientWidth
|
||||||
this.height = container.clientHeight
|
this.height = container.clientHeight
|
||||||
|
@ -84,7 +95,7 @@ previewmode.create = function (container, options = {}) {
|
||||||
this.content.appendChild(this.dom.previewContent)
|
this.content.appendChild(this.dom.previewContent)
|
||||||
|
|
||||||
if (this.options.mainMenuBar) {
|
if (this.options.mainMenuBar) {
|
||||||
util.addClassName(this.content, 'has-main-menu-bar')
|
addClassName(this.content, 'has-main-menu-bar')
|
||||||
|
|
||||||
// create menu
|
// create menu
|
||||||
this.menu = document.createElement('div')
|
this.menu = document.createElement('div')
|
||||||
|
@ -240,7 +251,7 @@ previewmode.create = function (container, options = {}) {
|
||||||
this.container.appendChild(this.frame)
|
this.container.appendChild(this.frame)
|
||||||
|
|
||||||
if (options.statusBar) {
|
if (options.statusBar) {
|
||||||
util.addClassName(this.content, 'has-status-bar')
|
addClassName(this.content, 'has-status-bar')
|
||||||
|
|
||||||
const statusBar = document.createElement('div')
|
const statusBar = document.createElement('div')
|
||||||
this.dom.statusBar = statusBar
|
this.dom.statusBar = statusBar
|
||||||
|
@ -270,10 +281,10 @@ previewmode.create = function (container, options = {}) {
|
||||||
previewmode._renderPreview = function () {
|
previewmode._renderPreview = function () {
|
||||||
const text = this.getText()
|
const text = this.getText()
|
||||||
|
|
||||||
this.dom.previewText.nodeValue = util.limitCharacters(text, MAX_PREVIEW_CHARACTERS)
|
this.dom.previewText.nodeValue = limitCharacters(text, MAX_PREVIEW_CHARACTERS)
|
||||||
|
|
||||||
if (this.dom.fileSizeInfo) {
|
if (this.dom.fileSizeInfo) {
|
||||||
this.dom.fileSizeInfo.innerText = 'Size: ' + util.formatSize(text.length)
|
this.dom.fileSizeInfo.innerText = 'Size: ' + formatSize(text.length)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.dom.arrayInfo) {
|
if (this.dom.arrayInfo) {
|
||||||
|
@ -332,14 +343,14 @@ previewmode._showSortModal = function () {
|
||||||
|
|
||||||
function onSort (json, sortedBy) {
|
function onSort (json, sortedBy) {
|
||||||
if (Array.isArray(json)) {
|
if (Array.isArray(json)) {
|
||||||
const sortedArray = util.sort(json, sortedBy.path, sortedBy.direction)
|
const sortedArray = sort(json, sortedBy.path, sortedBy.direction)
|
||||||
|
|
||||||
me.sortedBy = sortedBy
|
me.sortedBy = sortedBy
|
||||||
me._setAndFireOnChange(sortedArray)
|
me._setAndFireOnChange(sortedArray)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (util.isObject(json)) {
|
if (isObject(json)) {
|
||||||
const sortedObject = util.sortObjectKeys(json, sortedBy.direction)
|
const sortedObject = sortObjectKeys(json, sortedBy.direction)
|
||||||
|
|
||||||
me.sortedBy = sortedBy
|
me.sortedBy = sortedBy
|
||||||
me._setAndFireOnChange(sortedObject)
|
me._setAndFireOnChange(sortedObject)
|
||||||
|
@ -426,7 +437,7 @@ previewmode.format = function () {
|
||||||
*/
|
*/
|
||||||
previewmode.repair = function () {
|
previewmode.repair = function () {
|
||||||
const text = this.getText()
|
const text = this.getText()
|
||||||
const repairedText = util.repair(text)
|
const repairedText = repair(text)
|
||||||
|
|
||||||
this._setTextAndFireOnChange(repairedText)
|
this._setTextAndFireOnChange(repairedText)
|
||||||
}
|
}
|
||||||
|
@ -489,7 +500,7 @@ previewmode.get = function () {
|
||||||
if (this.json === undefined) {
|
if (this.json === undefined) {
|
||||||
const text = this.getText()
|
const text = this.getText()
|
||||||
|
|
||||||
this.json = util.parse(text) // this can throw an error
|
this.json = parse(text) // this can throw an error
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.json
|
return this.json
|
||||||
|
@ -504,7 +515,7 @@ previewmode.getText = function () {
|
||||||
this.text = JSON.stringify(this.json, null, this.indentation)
|
this.text = JSON.stringify(this.json, null, this.indentation)
|
||||||
|
|
||||||
if (this.options.escapeUnicode === true) {
|
if (this.options.escapeUnicode === true) {
|
||||||
this.text = util.escapeUnicodeChars(this.text)
|
this.text = escapeUnicodeChars(this.text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -544,7 +555,7 @@ previewmode.updateText = function (jsonText) {
|
||||||
*/
|
*/
|
||||||
previewmode._setText = function (jsonText, json) {
|
previewmode._setText = function (jsonText, json) {
|
||||||
if (this.options.escapeUnicode === true) {
|
if (this.options.escapeUnicode === true) {
|
||||||
this.text = util.escapeUnicodeChars(jsonText)
|
this.text = escapeUnicodeChars(jsonText)
|
||||||
} else {
|
} else {
|
||||||
this.text = jsonText
|
this.text = jsonText
|
||||||
}
|
}
|
||||||
|
@ -624,12 +635,12 @@ previewmode.executeWithBusyMessage = function (fn, message) {
|
||||||
|
|
||||||
if (size > SIZE_LARGE) {
|
if (size > SIZE_LARGE) {
|
||||||
const me = this
|
const me = this
|
||||||
util.addClassName(me.frame, 'busy')
|
addClassName(me.frame, 'busy')
|
||||||
me.dom.busyContent.innerText = message
|
me.dom.busyContent.innerText = message
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
fn()
|
fn()
|
||||||
util.removeClassName(me.frame, 'busy')
|
removeClassName(me.frame, 'busy')
|
||||||
me.dom.busyContent.innerText = ''
|
me.dom.busyContent.innerText = ''
|
||||||
}, 100)
|
}, 100)
|
||||||
} else {
|
} else {
|
||||||
|
@ -642,7 +653,7 @@ previewmode.validate = textmode.validate
|
||||||
previewmode._renderErrors = textmode._renderErrors
|
previewmode._renderErrors = textmode._renderErrors
|
||||||
|
|
||||||
// define modes
|
// define modes
|
||||||
module.exports = [
|
export const previewModeMixins = [
|
||||||
{
|
{
|
||||||
mode: 'preview',
|
mode: 'preview',
|
||||||
mixin: previewmode,
|
mixin: previewmode,
|
||||||
|
|
|
@ -1,15 +1,29 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const ace = require('./ace')
|
import ace from './ace'
|
||||||
const jmespath = require('jmespath')
|
import jmespath from 'jmespath'
|
||||||
const translate = require('./i18n').translate
|
import { translate } from './i18n'
|
||||||
const ModeSwitcher = require('./ModeSwitcher')
|
import ModeSwitcher from './ModeSwitcher'
|
||||||
const ErrorTable = require('./ErrorTable').ErrorTable
|
import { ErrorTable } from './ErrorTable'
|
||||||
const validateCustom = require('./validationUtils').validateCustom
|
import { validateCustom } from './validationUtils'
|
||||||
const showSortModal = require('./showSortModal').showSortModal
|
import { showSortModal } from './showSortModal'
|
||||||
const showTransformModal = require('./showTransformModal').showTransformModal
|
import { showTransformModal } from './showTransformModal'
|
||||||
const util = require('./util')
|
import {
|
||||||
const DEFAULT_MODAL_ANCHOR = require('./constants').DEFAULT_MODAL_ANCHOR
|
addClassName,
|
||||||
|
debounce,
|
||||||
|
escapeUnicodeChars,
|
||||||
|
getIndexForPosition,
|
||||||
|
getInputSelection,
|
||||||
|
getPositionForPath,
|
||||||
|
improveSchemaError,
|
||||||
|
isObject,
|
||||||
|
parse,
|
||||||
|
repair,
|
||||||
|
sort,
|
||||||
|
sortObjectKeys
|
||||||
|
} from './util'
|
||||||
|
import { DEFAULT_MODAL_ANCHOR } from './constants'
|
||||||
|
import { tryRequireThemeJsonEditor } from './tryRequireThemeJsonEditor'
|
||||||
|
|
||||||
// create a mixin with the functions for text mode
|
// create a mixin with the functions for text mode
|
||||||
const textmode = {}
|
const textmode = {}
|
||||||
|
@ -22,10 +36,7 @@ const DEFAULT_THEME = 'ace/theme/jsoneditor'
|
||||||
* @param {Object} [options] Object with options. See docs for details.
|
* @param {Object} [options] Object with options. See docs for details.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
textmode.create = function (container, options) {
|
textmode.create = function (container, options = {}) {
|
||||||
// read options
|
|
||||||
options = options || {}
|
|
||||||
|
|
||||||
if (typeof options.statusBar === 'undefined') {
|
if (typeof options.statusBar === 'undefined') {
|
||||||
options.statusBar = true
|
options.statusBar = true
|
||||||
}
|
}
|
||||||
|
@ -61,11 +72,7 @@ textmode.create = function (container, options) {
|
||||||
// determine theme
|
// determine theme
|
||||||
this.theme = options.theme || DEFAULT_THEME
|
this.theme = options.theme || DEFAULT_THEME
|
||||||
if (this.theme === DEFAULT_THEME && _ace) {
|
if (this.theme === DEFAULT_THEME && _ace) {
|
||||||
try {
|
tryRequireThemeJsonEditor()
|
||||||
require('./ace/theme-jsoneditor')
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.onTextSelectionChange) {
|
if (options.onTextSelectionChange) {
|
||||||
|
@ -81,7 +88,7 @@ textmode.create = function (container, options) {
|
||||||
this.annotations = []
|
this.annotations = []
|
||||||
|
|
||||||
// create a debounced validate function
|
// create a debounced validate function
|
||||||
this._debouncedValidate = util.debounce(this.validate.bind(this), this.DEBOUNCE_INTERVAL)
|
this._debouncedValidate = debounce(this.validate.bind(this), this.DEBOUNCE_INTERVAL)
|
||||||
|
|
||||||
this.width = container.clientWidth
|
this.width = container.clientWidth
|
||||||
this.height = container.clientHeight
|
this.height = container.clientHeight
|
||||||
|
@ -100,7 +107,7 @@ textmode.create = function (container, options) {
|
||||||
this.content.className = 'jsoneditor-outer'
|
this.content.className = 'jsoneditor-outer'
|
||||||
|
|
||||||
if (this.options.mainMenuBar) {
|
if (this.options.mainMenuBar) {
|
||||||
util.addClassName(this.content, 'has-main-menu-bar')
|
addClassName(this.content, 'has-main-menu-bar')
|
||||||
|
|
||||||
// create menu
|
// create menu
|
||||||
this.menu = document.createElement('div')
|
this.menu = document.createElement('div')
|
||||||
|
@ -284,7 +291,7 @@ textmode.create = function (container, options) {
|
||||||
this.frame.appendChild(this.errorTable.getErrorTable())
|
this.frame.appendChild(this.errorTable.getErrorTable())
|
||||||
|
|
||||||
if (options.statusBar) {
|
if (options.statusBar) {
|
||||||
util.addClassName(this.content, 'has-status-bar')
|
addClassName(this.content, 'has-status-bar')
|
||||||
|
|
||||||
this.curserInfoElements = {}
|
this.curserInfoElements = {}
|
||||||
const statusBar = document.createElement('div')
|
const statusBar = document.createElement('div')
|
||||||
|
@ -385,14 +392,14 @@ textmode._showSortModal = function () {
|
||||||
|
|
||||||
function onSort (sortedBy) {
|
function onSort (sortedBy) {
|
||||||
if (Array.isArray(json)) {
|
if (Array.isArray(json)) {
|
||||||
const sortedJson = util.sort(json, sortedBy.path, sortedBy.direction)
|
const sortedJson = sort(json, sortedBy.path, sortedBy.direction)
|
||||||
|
|
||||||
me.sortedBy = sortedBy
|
me.sortedBy = sortedBy
|
||||||
me.set(sortedJson)
|
me.set(sortedJson)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (util.isObject(json)) {
|
if (isObject(json)) {
|
||||||
const sortedJson = util.sortObjectKeys(json, sortedBy.direction)
|
const sortedJson = sortObjectKeys(json, sortedBy.direction)
|
||||||
|
|
||||||
me.sortedBy = sortedBy
|
me.sortedBy = sortedBy
|
||||||
me.set(sortedJson)
|
me.set(sortedJson)
|
||||||
|
@ -490,7 +497,7 @@ textmode._updateCursorInfo = function () {
|
||||||
|
|
||||||
if (this.textarea) {
|
if (this.textarea) {
|
||||||
setTimeout(() => { // this to verify we get the most updated textarea cursor selection
|
setTimeout(() => { // this to verify we get the most updated textarea cursor selection
|
||||||
const selectionRange = util.getInputSelection(me.textarea)
|
const selectionRange = getInputSelection(me.textarea)
|
||||||
|
|
||||||
if (selectionRange.startIndex !== selectionRange.endIndex) {
|
if (selectionRange.startIndex !== selectionRange.endIndex) {
|
||||||
count = selectionRange.endIndex - selectionRange.startIndex
|
count = selectionRange.endIndex - selectionRange.startIndex
|
||||||
|
@ -617,7 +624,7 @@ textmode.format = function () {
|
||||||
*/
|
*/
|
||||||
textmode.repair = function () {
|
textmode.repair = function () {
|
||||||
const text = this.getText()
|
const text = this.getText()
|
||||||
const repairedText = util.repair(text)
|
const repairedText = repair(text)
|
||||||
this._setText(repairedText, false)
|
this._setText(repairedText, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -666,7 +673,7 @@ textmode.update = function (json) {
|
||||||
textmode.get = function () {
|
textmode.get = function () {
|
||||||
const text = this.getText()
|
const text = this.getText()
|
||||||
|
|
||||||
return util.parse(text) // this can throw an error
|
return parse(text) // this can throw an error
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -693,7 +700,7 @@ textmode._setText = function (jsonText, clearHistory) {
|
||||||
let text
|
let text
|
||||||
|
|
||||||
if (this.options.escapeUnicode === true) {
|
if (this.options.escapeUnicode === true) {
|
||||||
text = util.escapeUnicodeChars(jsonText)
|
text = escapeUnicodeChars(jsonText)
|
||||||
} else {
|
} else {
|
||||||
text = jsonText
|
text = jsonText
|
||||||
}
|
}
|
||||||
|
@ -759,7 +766,7 @@ textmode.validate = function () {
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
schemaErrors = this.validateSchema.errors.map(error => {
|
schemaErrors = this.validateSchema.errors.map(error => {
|
||||||
error.type = 'validation'
|
error.type = 'validation'
|
||||||
return util.improveSchemaError(error)
|
return improveSchemaError(error)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -808,7 +815,7 @@ textmode._renderErrors = function (errors) {
|
||||||
}
|
}
|
||||||
return acc
|
return acc
|
||||||
}, errorPaths)
|
}, errorPaths)
|
||||||
const errorLocations = util.getPositionForPath(jsonText, errorPaths)
|
const errorLocations = getPositionForPath(jsonText, errorPaths)
|
||||||
|
|
||||||
// render annotations in Ace Editor (if any)
|
// render annotations in Ace Editor (if any)
|
||||||
if (this.aceEditor) {
|
if (this.aceEditor) {
|
||||||
|
@ -847,7 +854,7 @@ textmode._renderErrors = function (errors) {
|
||||||
textmode.getTextSelection = function () {
|
textmode.getTextSelection = function () {
|
||||||
let selection = {}
|
let selection = {}
|
||||||
if (this.textarea) {
|
if (this.textarea) {
|
||||||
const selectionRange = util.getInputSelection(this.textarea)
|
const selectionRange = getInputSelection(this.textarea)
|
||||||
|
|
||||||
if (this.cursorInfo && this.cursorInfo.line === selectionRange.end.row && this.cursorInfo.column === selectionRange.end.column) {
|
if (this.cursorInfo && this.cursorInfo.line === selectionRange.end.row && this.cursorInfo.column === selectionRange.end.column) {
|
||||||
// selection direction is bottom => up
|
// selection direction is bottom => up
|
||||||
|
@ -900,7 +907,7 @@ textmode.getTextSelection = function () {
|
||||||
*/
|
*/
|
||||||
textmode.onTextSelectionChange = function (callback) {
|
textmode.onTextSelectionChange = function (callback) {
|
||||||
if (typeof callback === 'function') {
|
if (typeof callback === 'function') {
|
||||||
this._selectionChangedHandler = util.debounce(callback, this.DEBOUNCE_INTERVAL)
|
this._selectionChangedHandler = debounce(callback, this.DEBOUNCE_INTERVAL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -913,8 +920,8 @@ textmode.setTextSelection = function (startPos, endPos) {
|
||||||
if (!startPos || !endPos) return
|
if (!startPos || !endPos) return
|
||||||
|
|
||||||
if (this.textarea) {
|
if (this.textarea) {
|
||||||
const startIndex = util.getIndexForPosition(this.textarea, startPos.row, startPos.column)
|
const startIndex = getIndexForPosition(this.textarea, startPos.row, startPos.column)
|
||||||
const endIndex = util.getIndexForPosition(this.textarea, endPos.row, endPos.column)
|
const endIndex = getIndexForPosition(this.textarea, endPos.row, endPos.column)
|
||||||
if (startIndex > -1 && endIndex > -1) {
|
if (startIndex > -1 && endIndex > -1) {
|
||||||
if (this.textarea.setSelectionRange) {
|
if (this.textarea.setSelectionRange) {
|
||||||
this.textarea.focus()
|
this.textarea.focus()
|
||||||
|
@ -956,7 +963,7 @@ function load () {
|
||||||
}
|
}
|
||||||
|
|
||||||
// define modes
|
// define modes
|
||||||
module.exports = [
|
export const textModeMixins = [
|
||||||
{
|
{
|
||||||
mode: 'text',
|
mode: 'text',
|
||||||
mixin: textmode,
|
mixin: textmode,
|
||||||
|
|
|
@ -1,18 +1,32 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const VanillaPicker = require('./vanilla-picker')
|
import VanillaPicker from './vanilla-picker'
|
||||||
const Highlighter = require('./Highlighter').Highlighter
|
import { Highlighter } from './Highlighter'
|
||||||
const NodeHistory = require('./NodeHistory').NodeHistory
|
import { NodeHistory } from './NodeHistory'
|
||||||
const SearchBox = require('./SearchBox').SearchBox
|
import { SearchBox } from './SearchBox'
|
||||||
const ContextMenu = require('./ContextMenu').ContextMenu
|
import { ContextMenu } from './ContextMenu'
|
||||||
const TreePath = require('./TreePath').TreePath
|
import { TreePath } from './TreePath'
|
||||||
const Node = require('./Node')
|
import Node from './Node'
|
||||||
const ModeSwitcher = require('./ModeSwitcher')
|
import ModeSwitcher from './ModeSwitcher'
|
||||||
const util = require('./util')
|
import {
|
||||||
const autocomplete = require('./autocomplete').autocomplete
|
addClassName,
|
||||||
const translate = require('./i18n').translate
|
addEventListener,
|
||||||
const setLanguages = require('./i18n').setLanguages
|
debounce,
|
||||||
const setLanguage = require('./i18n').setLanguage
|
getAbsoluteTop,
|
||||||
|
getSelectionOffset,
|
||||||
|
hasParentNode,
|
||||||
|
improveSchemaError,
|
||||||
|
isPromise,
|
||||||
|
isValidValidationError,
|
||||||
|
parse,
|
||||||
|
removeClassName,
|
||||||
|
removeEventListener,
|
||||||
|
repair,
|
||||||
|
selectContentEditable,
|
||||||
|
setSelectionOffset
|
||||||
|
} from './util'
|
||||||
|
import { autocomplete } from './autocomplete'
|
||||||
|
import { setLanguage, setLanguages, translate } from './i18n'
|
||||||
|
|
||||||
// create a mixin with the functions for tree mode
|
// create a mixin with the functions for tree mode
|
||||||
const treemode = {}
|
const treemode = {}
|
||||||
|
@ -144,7 +158,7 @@ treemode._setOptions = function (options) {
|
||||||
this.setSchema(this.options.schema, this.options.schemaRefs)
|
this.setSchema(this.options.schema, this.options.schemaRefs)
|
||||||
|
|
||||||
// create a debounced validate function
|
// create a debounced validate function
|
||||||
this._debouncedValidate = util.debounce(this.validate.bind(this), this.DEBOUNCE_INTERVAL)
|
this._debouncedValidate = debounce(this.validate.bind(this), this.DEBOUNCE_INTERVAL)
|
||||||
|
|
||||||
if (options.onSelectionChange) {
|
if (options.onSelectionChange) {
|
||||||
this.onSelectionChange(options.onSelectionChange)
|
this.onSelectionChange(options.onSelectionChange)
|
||||||
|
@ -268,13 +282,13 @@ treemode.getText = function () {
|
||||||
*/
|
*/
|
||||||
treemode.setText = function (jsonText) {
|
treemode.setText = function (jsonText) {
|
||||||
try {
|
try {
|
||||||
this.set(util.parse(jsonText)) // this can throw an error
|
this.set(parse(jsonText)) // this can throw an error
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// try to repair json, replace JavaScript notation with JSON notation
|
// try to repair json, replace JavaScript notation with JSON notation
|
||||||
const repairedJsonText = util.repair(jsonText)
|
const repairedJsonText = repair(jsonText)
|
||||||
|
|
||||||
// try to parse again
|
// try to parse again
|
||||||
this.set(util.parse(repairedJsonText)) // this can throw an error
|
this.set(parse(repairedJsonText)) // this can throw an error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -285,13 +299,13 @@ treemode.setText = function (jsonText) {
|
||||||
*/
|
*/
|
||||||
treemode.updateText = function (jsonText) {
|
treemode.updateText = function (jsonText) {
|
||||||
try {
|
try {
|
||||||
this.update(util.parse(jsonText)) // this can throw an error
|
this.update(parse(jsonText)) // this can throw an error
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// try to repair json, replace JavaScript notation with JSON notation
|
// try to repair json, replace JavaScript notation with JSON notation
|
||||||
const repairJsonText = util.repair(jsonText)
|
const repairJsonText = repair(jsonText)
|
||||||
|
|
||||||
// try to parse again
|
// try to parse again
|
||||||
this.update(util.parse(repairJsonText)) // this can throw an error
|
this.update(parse(repairJsonText)) // this can throw an error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -531,7 +545,7 @@ treemode.validate = function () {
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
// apply all new errors
|
// apply all new errors
|
||||||
schemaErrors = this.validateSchema.errors
|
schemaErrors = this.validateSchema.errors
|
||||||
.map(error => util.improveSchemaError(error))
|
.map(error => improveSchemaError(error))
|
||||||
.map(function findNode (error) {
|
.map(function findNode (error) {
|
||||||
return {
|
return {
|
||||||
node: root.findNode(error.dataPath),
|
node: root.findNode(error.dataPath),
|
||||||
|
@ -610,7 +624,7 @@ treemode._validateCustom = function (json) {
|
||||||
const root = this.node
|
const root = this.node
|
||||||
const customValidateResults = this.options.onValidate(json)
|
const customValidateResults = this.options.onValidate(json)
|
||||||
|
|
||||||
const resultPromise = util.isPromise(customValidateResults)
|
const resultPromise = isPromise(customValidateResults)
|
||||||
? customValidateResults
|
? customValidateResults
|
||||||
: Promise.resolve(customValidateResults)
|
: Promise.resolve(customValidateResults)
|
||||||
|
|
||||||
|
@ -618,7 +632,7 @@ treemode._validateCustom = function (json) {
|
||||||
if (Array.isArray(customValidationPathErrors)) {
|
if (Array.isArray(customValidationPathErrors)) {
|
||||||
return customValidationPathErrors
|
return customValidationPathErrors
|
||||||
.filter(error => {
|
.filter(error => {
|
||||||
const valid = util.isValidValidationError(error)
|
const valid = isValidValidationError(error)
|
||||||
|
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
console.warn('Ignoring a custom validation error with invalid structure. ' +
|
console.warn('Ignoring a custom validation error with invalid structure. ' +
|
||||||
|
@ -674,7 +688,7 @@ treemode.refresh = function () {
|
||||||
treemode.startAutoScroll = function (mouseY) {
|
treemode.startAutoScroll = function (mouseY) {
|
||||||
const me = this
|
const me = this
|
||||||
const content = this.scrollableContent
|
const content = this.scrollableContent
|
||||||
const top = util.getAbsoluteTop(content)
|
const top = getAbsoluteTop(content)
|
||||||
const height = content.clientHeight
|
const height = content.clientHeight
|
||||||
const bottom = top + height
|
const bottom = top + height
|
||||||
const margin = 24
|
const margin = 24
|
||||||
|
@ -752,7 +766,7 @@ treemode.setDomSelection = function (selection) {
|
||||||
: null
|
: null
|
||||||
if (selection.range && container) {
|
if (selection.range && container) {
|
||||||
const range = Object.assign({}, selection.range, { container: container })
|
const range = Object.assign({}, selection.range, { container: container })
|
||||||
util.setSelectionOffset(range)
|
setSelectionOffset(range)
|
||||||
} else if (node) { // just a fallback
|
} else if (node) { // just a fallback
|
||||||
node.focus()
|
node.focus()
|
||||||
}
|
}
|
||||||
|
@ -778,7 +792,7 @@ treemode.getDomSelection = function () {
|
||||||
? Object.keys(node.dom).find(domName => node.dom[domName] === focusTarget)
|
? Object.keys(node.dom).find(domName => node.dom[domName] === focusTarget)
|
||||||
: null
|
: null
|
||||||
|
|
||||||
let range = util.getSelectionOffset()
|
let range = getSelectionOffset()
|
||||||
if (range && range.container.nodeName !== 'DIV') { // filter on (editable) divs)
|
if (range && range.container.nodeName !== 'DIV') { // filter on (editable) divs)
|
||||||
range = null
|
range = null
|
||||||
}
|
}
|
||||||
|
@ -903,13 +917,13 @@ treemode._createFrame = function () {
|
||||||
// Note: focus and blur events do not propagate, therefore they defined
|
// Note: focus and blur events do not propagate, therefore they defined
|
||||||
// using an eventListener with useCapture=true
|
// using an eventListener with useCapture=true
|
||||||
// see http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html
|
// see http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html
|
||||||
util.addEventListener(this.frame, 'focus', onEvent, true)
|
addEventListener(this.frame, 'focus', onEvent, true)
|
||||||
util.addEventListener(this.frame, 'blur', onEvent, true)
|
addEventListener(this.frame, 'blur', onEvent, true)
|
||||||
this.frame.onfocusin = onEvent // for IE
|
this.frame.onfocusin = onEvent // for IE
|
||||||
this.frame.onfocusout = onEvent // for IE
|
this.frame.onfocusout = onEvent // for IE
|
||||||
|
|
||||||
if (this.options.mainMenuBar) {
|
if (this.options.mainMenuBar) {
|
||||||
util.addClassName(this.contentOuter, 'has-main-menu-bar')
|
addClassName(this.contentOuter, 'has-main-menu-bar')
|
||||||
|
|
||||||
// create menu
|
// create menu
|
||||||
this.menu = document.createElement('div')
|
this.menu = document.createElement('div')
|
||||||
|
@ -1108,7 +1122,7 @@ treemode._onEvent = function (event) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// filter mouse events in the contents part of the editor (not the main menu)
|
// filter mouse events in the contents part of the editor (not the main menu)
|
||||||
if (event.type === 'mousedown' && util.hasParentNode(event.target, this.content)) {
|
if (event.type === 'mousedown' && hasParentNode(event.target, this.content)) {
|
||||||
this.deselect()
|
this.deselect()
|
||||||
|
|
||||||
if (node && event.target === node.dom.drag) {
|
if (node && event.target === node.dom.drag) {
|
||||||
|
@ -1133,7 +1147,7 @@ treemode._onEvent = function (event) {
|
||||||
*/
|
*/
|
||||||
treemode._updateTreePath = function (pathNodes) {
|
treemode._updateTreePath = function (pathNodes) {
|
||||||
if (pathNodes && pathNodes.length) {
|
if (pathNodes && pathNodes.length) {
|
||||||
util.removeClassName(this.navBar, 'nav-bar-empty')
|
removeClassName(this.navBar, 'nav-bar-empty')
|
||||||
|
|
||||||
const pathObjs = []
|
const pathObjs = []
|
||||||
pathNodes.forEach(node => {
|
pathNodes.forEach(node => {
|
||||||
|
@ -1154,7 +1168,7 @@ treemode._updateTreePath = function (pathNodes) {
|
||||||
})
|
})
|
||||||
this.treePath.setPath(pathObjs)
|
this.treePath.setPath(pathObjs)
|
||||||
} else {
|
} else {
|
||||||
util.addClassName(this.navBar, 'nav-bar-empty')
|
addClassName(this.navBar, 'nav-bar-empty')
|
||||||
}
|
}
|
||||||
|
|
||||||
function getName (node) {
|
function getName (node) {
|
||||||
|
@ -1245,12 +1259,12 @@ treemode._onMultiSelectStart = function (event) {
|
||||||
|
|
||||||
const editor = this
|
const editor = this
|
||||||
if (!this.mousemove) {
|
if (!this.mousemove) {
|
||||||
this.mousemove = util.addEventListener(window, 'mousemove', event => {
|
this.mousemove = addEventListener(window, 'mousemove', event => {
|
||||||
editor._onMultiSelect(event)
|
editor._onMultiSelect(event)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if (!this.mouseup) {
|
if (!this.mouseup) {
|
||||||
this.mouseup = util.addEventListener(window, 'mouseup', event => {
|
this.mouseup = addEventListener(window, 'mouseup', event => {
|
||||||
editor._onMultiSelectEnd(event)
|
editor._onMultiSelectEnd(event)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1316,11 +1330,11 @@ treemode._onMultiSelectEnd = function () {
|
||||||
|
|
||||||
// cleanup global event listeners
|
// cleanup global event listeners
|
||||||
if (this.mousemove) {
|
if (this.mousemove) {
|
||||||
util.removeEventListener(window, 'mousemove', this.mousemove)
|
removeEventListener(window, 'mousemove', this.mousemove)
|
||||||
delete this.mousemove
|
delete this.mousemove
|
||||||
}
|
}
|
||||||
if (this.mouseup) {
|
if (this.mouseup) {
|
||||||
util.removeEventListener(window, 'mouseup', this.mouseup)
|
removeEventListener(window, 'mouseup', this.mouseup)
|
||||||
delete this.mouseup
|
delete this.mouseup
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1480,7 +1494,7 @@ treemode._onKeyDown = function (event) {
|
||||||
const me = this
|
const me = this
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
// select all text when moving focus to an editable div
|
// select all text when moving focus to an editable div
|
||||||
util.selectContentEditable(me.focusTarget)
|
selectContentEditable(me.focusTarget)
|
||||||
}, 0)
|
}, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1535,7 +1549,7 @@ treemode._onKeyDown = function (event) {
|
||||||
*/
|
*/
|
||||||
treemode._createTable = function () {
|
treemode._createTable = function () {
|
||||||
if (this.options.navigationBar) {
|
if (this.options.navigationBar) {
|
||||||
util.addClassName(this.contentOuter, 'has-nav-bar')
|
addClassName(this.contentOuter, 'has-nav-bar')
|
||||||
}
|
}
|
||||||
|
|
||||||
this.scrollableContent = document.createElement('div')
|
this.scrollableContent = document.createElement('div')
|
||||||
|
@ -1655,7 +1669,7 @@ treemode.getSelection = function () {
|
||||||
*/
|
*/
|
||||||
treemode.onSelectionChange = function (callback) {
|
treemode.onSelectionChange = function (callback) {
|
||||||
if (typeof callback === 'function') {
|
if (typeof callback === 'function') {
|
||||||
this._selectionChangedHandler = util.debounce(callback, this.DEBOUNCE_INTERVAL)
|
this._selectionChangedHandler = debounce(callback, this.DEBOUNCE_INTERVAL)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1739,7 +1753,7 @@ treemode.getNodesByRange = function (start, end) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// define modes
|
// define modes
|
||||||
module.exports = [
|
export const treeModeMixins = [
|
||||||
{
|
{
|
||||||
mode: 'tree',
|
mode: 'tree',
|
||||||
mixin: treemode,
|
mixin: treemode,
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
exports.tryRequireAjv = function () {
|
||||||
|
try {
|
||||||
|
return require('ajv')
|
||||||
|
} catch (err) {
|
||||||
|
// no problem... when we need Ajv we will throw a neat exception
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
exports.tryRequireThemeJsonEditor = function () {
|
||||||
|
try {
|
||||||
|
require('./ace/theme-jsoneditor')
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue