Fixed #847: allow customizing the rules determining whether a value is a timestamp or not

This commit is contained in:
jos 2019-11-20 10:47:49 +01:00
parent 728c10c137
commit a615f7ab91
5 changed files with 45 additions and 9 deletions

View File

@ -3,6 +3,12 @@
https://github.com/josdejong/jsoneditor https://github.com/josdejong/jsoneditor
## not yet published, version 7.3.0
- Fixed #847: allow customizing the in rules determining whether a value
is a timestamp or not.
## 2019-10-27, version 7.2.1 ## 2019-10-27, version 7.2.1
- Fixed #826: editor not allowing indentation `0`. - Fixed #826: editor not allowing indentation `0`.

View File

@ -432,11 +432,16 @@ Constructs a new JSONEditor.
} }
``` ```
- `{boolean} timestampTag` - `{boolean | function(value: any) -> boolean} timestampTag`
If `true` (default), a tag with the date/time of a timestamp is displayed If `true` (default), a tag with the date/time of a timestamp is displayed
right from timestamps. A value is considered a timestamp when it right from values containing a timestamp. A value is considered a timestamp
has a value larger than Jan 1th 2000, `946684800000`. when it is a number with value larger than Jan 1th 2000, `946684800000`.
When `timestampTag` a is a function, a timestamp tag will be displayed for
values for which `timestampTag(value)` returns `true`. This way it is
possible to alter the default rules for determining whether a value
is a timestamp or not.
- `{string} language` - `{string} language`

View File

@ -34,8 +34,6 @@ import {
import { translate } from './i18n' import { translate } from './i18n'
import { DEFAULT_MODAL_ANCHOR } from './constants' import { DEFAULT_MODAL_ANCHOR } from './constants'
const YEAR_2000 = 946684800000
/** /**
* @constructor Node * @constructor Node
* Create a new Node * Create a new Node
@ -1784,10 +1782,7 @@ export class Node {
} }
// show date tag when value is a timestamp in milliseconds // show date tag when value is a timestamp in milliseconds
if (this.editor.options.timestampTag && if (this.editor.showTimestampTag(value)) {
typeof value === 'number' &&
value > YEAR_2000 &&
!isNaN(new Date(value).valueOf())) {
if (!this.dom.date) { if (!this.dom.date) {
this.dom.date = document.createElement('div') this.dom.date = document.createElement('div')
this.dom.date.className = 'jsoneditor-date' this.dom.date.className = 'jsoneditor-date'

View File

@ -17,6 +17,7 @@ import {
hasParentNode, hasParentNode,
improveSchemaError, improveSchemaError,
isPromise, isPromise,
isTimestamp,
isValidValidationError, isValidValidationError,
parse, parse,
removeClassName, removeClassName,
@ -1760,6 +1761,23 @@ treemode.getNodesByRange = function (start, end) {
return serializableNodes return serializableNodes
} }
/**
* Test whether to show a timestamp tag or not
* @param {*} value Any type of value
* @return {boolean} Returns true when the value is a timestamp
*/
treemode.showTimestampTag = function (value) {
if (typeof this.options.timestampTag === 'function') {
return this.options.timestampTag(value)
}
if (this.options.timestampTag === true) {
return isTimestamp(value)
}
return false
}
// define modes // define modes
export const treeModeMixins = [ export const treeModeMixins = [
{ {

View File

@ -7,6 +7,7 @@ import jsonMap from 'json-source-map'
import { translate } from './i18n' import { translate } from './i18n'
const MAX_ITEMS_FIELDS_COLLECTION = 10000 const MAX_ITEMS_FIELDS_COLLECTION = 10000
const YEAR_2000 = 946684800000
/** /**
* Parse JSON using the parser built-in in the browser. * Parse JSON using the parser built-in in the browser.
@ -1354,6 +1355,17 @@ export function parseString (str) {
return str return str
} }
/**
* Test whether some value contains a timestamp.
* @param {*} value Any type of value
* @return {boolean} Returns true when the value is a timestamp
*/
export function isTimestamp (value) {
return typeof value === 'number' &&
value > YEAR_2000 &&
!isNaN(new Date(value).valueOf())
}
/** /**
* Return a human readable document size * Return a human readable document size
* For example formatSize(7570718) outputs '7.2 MiB' * For example formatSize(7570718) outputs '7.2 MiB'