diff --git a/src/components/JSONNode.js b/src/components/JSONNode.js index fd99909..c3ae542 100644 --- a/src/components/JSONNode.js +++ b/src/components/JSONNode.js @@ -8,9 +8,7 @@ import { escapeHTML, unescapeHTML } from '../utils/stringUtils' import { getInnerText, insideRect } from '../utils/domUtils' import { stringConvert, valueType, isUrl } from '../utils/typeUtils' -import type { - PropertyData, ObjectData, ArrayData, JSONData, - SearchResult, SearchResultStatus } from '../types' +import type { PropertyData, JSONData, SearchResultStatus } from '../types' /** * @type {JSONNode | null} activeContextMenu singleton holding the JSONNode having @@ -393,14 +391,15 @@ export default class JSONNode extends Component { } componentDidUpdate (prevProps, prevState) { - if (this.props.data.focusProperty && !prevProps.data.focusProperty) { + if (this.props.prop && this.props.prop.focus && + !(prevProps.props.prop && prevProps.props.prop.focus)) { console.log('focus property', this.getPath()) // TODO: cleanup if (this.refs.property) { this.refs.property.focus() } } - if (this.props.data.focusValue && !prevProps.data.focusValue) { + if (this.props.data.focus && !prevProps.data.focus) { console.log('focus value', this.getPath()) // TODO: cleanup if (this.refs.value) { this.refs.value.focus() diff --git a/src/components/TreeMode.js b/src/components/TreeMode.js index 9c7c443..22a3d8c 100644 --- a/src/components/TreeMode.js +++ b/src/components/TreeMode.js @@ -7,7 +7,8 @@ import { parseJSON } from '../utils/jsonUtils' import { enrichSchemaError } from '../utils/schemaUtils' import { jsonToData, dataToJson, toDataPath, patchData, pathExists, - expand, addErrors, search, addSearchResults, nextSearchResult, previousSearchResult + expand, addErrors, addFocus, + search, addSearchResults, nextSearchResult, previousSearchResult } from '../jsonData' import { duplicate, insert, append, remove, @@ -118,6 +119,10 @@ export default class TreeMode extends Component { if (searchResults) { data = addSearchResults(data, searchResults, this.state.search.active) } + // TODO: moveTo active search result (not focus!) + // if (this.state.search.active) { + // data = addFocus(data, this.state.search.active) + // } // console.log('data', data) diff --git a/src/jsonData.js b/src/jsonData.js index 797fec4..52c98b2 100644 --- a/src/jsonData.js +++ b/src/jsonData.js @@ -9,7 +9,7 @@ import { setIn, updateIn, getIn, deleteIn, insertAt } from './utils/immutability import { isObject } from './utils/typeUtils' import isEqual from 'lodash/isEqual' -import type {JSONData, SearchResult} from './types' +import type {JSONData, DataPointer} from './types' /** * Expand function which will expand all nodes @@ -509,8 +509,8 @@ export function addErrors (data, errors) { /** * Search some text in all properties and values */ -export function search (data: JSONData, text: string): SearchResult[] { - let results: SearchResult[] = [] +export function search (data: JSONData, text: string): DataPointer[] { + let results: DataPointer[] = [] traverse(data, function (value, path) { // check property name @@ -546,7 +546,7 @@ export function search (data: JSONData, text: string): SearchResult[] { * returned as next * - When `searchResults` is empty, null will be returned */ -export function nextSearchResult (searchResults: SearchResult[], current: SearchResult): SearchResult | null { +export function nextSearchResult (searchResults: DataPointer[], current: DataPointer): DataPointer | null { if (searchResults.length === 0) { return null } @@ -570,7 +570,7 @@ export function nextSearchResult (searchResults: SearchResult[], current: Search * returned as next * - When `searchResults` is empty, null will be returned */ -export function previousSearchResult (searchResults: SearchResult[], current: SearchResult): SearchResult | null { +export function previousSearchResult (searchResults: DataPointer[], current: DataPointer): DataPointer | null { if (searchResults.length === 0) { return null } @@ -589,7 +589,7 @@ export function previousSearchResult (searchResults: SearchResult[], current: Se /** * Merge searchResults into the data */ -export function addSearchResults (data: JSONData, searchResults: SearchResult[], activeSearchResult: SearchResult) { +export function addSearchResults (data: JSONData, searchResults: DataPointer[], activeSearchResult: DataPointer) { let updatedData = data searchResults.forEach(function (searchResult) { @@ -612,21 +612,20 @@ export function addSearchResults (data: JSONData, searchResults: SearchResult[], /** * Merge a object describing where the focus is to the data - * - * @param {JSONData} data - * @param {SearchResult} focusOn - * @return {JSONData} Returns an updated copy of data */ -export function addFocus (data: JSONData, focusOn: SearchResult) { - if (focusOn.value) { - const dataPath = toDataPath(data, focusOn.dataPath).concat('focusValue') +export function addFocus (data: JSONData, focusOn: DataPointer) { + if (focusOn.type == 'value') { + const dataPath = toDataPath(data, focusOn.dataPath).concat('focus') return setIn(data, dataPath, true) } - if (focusOn.property) { - const dataPath = toDataPath(data, focusOn.dataPath).concat('focusProperty') - return setIn(data, dataPath, true) + if (focusOn.type === 'property') { + const valueDataPath = toDataPath(data, focusOn.dataPath) + const propertyDataPath = allButLast(valueDataPath).concat('focus') + return setIn(data, propertyDataPath, true) } + + return data } /** diff --git a/src/types.js b/src/types.js index 2249968..689dc87 100644 --- a/src/types.js +++ b/src/types.js @@ -50,7 +50,7 @@ export type JSONArrayType = Array; /********************** TYPES FOR THE JSON DATA MODEL *************************/ export type SearchResultStatus = 'normal' | 'active' -export type SearchResultType = 'value' | 'property' +export type DataPointerType = 'value' | 'property' export type PropertyData = { name: string, @@ -82,11 +82,11 @@ export type JSONData = ObjectData | ArrayData | ValueData export type Path = string[] -export type SearchResult = { +export type DataPointer = { dataPath: Path, - type: SearchResultType + type: DataPointerType } -// TODO: SearchResult.dataPath is an array, JSONSchemaError.dataPath is a string -> make this consistent +// TODO: DataPointer.dataPath is an array, JSONSchemaError.dataPath is a string -> make this consistent // TODO: remove SetOptions, merge into Options (everywhere in the public API)