Skip to content

Commit 0bb89fb

Browse files
committed
#34 depth 열리는 값 추가
- 옵션 값 추가 - replace 메서드 파라메터 변경
1 parent 2961858 commit 0bb89fb

File tree

7 files changed

+51
-16
lines changed

7 files changed

+51
-16
lines changed

README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const jsonEditor = new JsonEditor(document.getElementById('target'), {
2626
live: true,
2727
theme: 'system',
2828
edit: 'all',
29+
node: {},
30+
openDepth: 2,
2931
})
3032
```
3133

@@ -40,6 +42,10 @@ const jsonEditor = new JsonEditor(document.getElementById('target'), {
4042
다크모드를 사용하는지에 대한 값입니다.
4143
- `edit` / (all,value,none)
4244
컨트롤할 수 있는 범위를 정합니다.
45+
- `node` / ({},[])
46+
클래스를 초기화할때 사용하는 노드 데이터 값입니다.
47+
- `openDepth` / 0
48+
x번째 뎁스의 노드가 열리는지에 대하여 정합니다.
4349

4450

4551
## Methods
@@ -186,16 +192,20 @@ jsonEditor.import(node, { foo: 'bar' }, true, true)
186192
데이터를 모두 교체합니다.
187193

188194
```javascript
189-
jsonEditor.replace(data, useUpdate)
195+
jsonEditor.replace(data, options, useUpdate)
190196
```
191197

192198
- `data`: 새로 교체할 데이터
199+
- `options`: 옵션
200+
- `openDepth`: 데이터가 교체될때 노드가 열리는 x번째 뎁스
193201
- `useUpdate`: 노드를 수정하고 업데이트 메서드를 실행합니다.
194202

195203
다음과 같이 사용할 수 있습니다.
196204

197205
```javascript
198-
jsonEditor.replace({ foo: 'bar' }, true)
206+
jsonEditor.replace({ foo: 'bar' }, {
207+
openDepth: 2,
208+
}, true)
199209
```
200210

201211
### export

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@redgoose/json-editor",
33
"description": "Easy editing JSON data",
44
"author": "redgoose <[email protected]>",
5-
"version": "1.1.1",
5+
"version": "1.2.0",
66
"keywords": [ "json", "editor", "library", "tool" ],
77
"license": "MIT",
88
"main": "./lib/json-editor.js",
@@ -40,11 +40,11 @@
4040
"node": ">=20"
4141
},
4242
"devDependencies": {
43-
"@sveltejs/vite-plugin-svelte": "^2.4.6",
43+
"@sveltejs/vite-plugin-svelte": "^3.1.1",
4444
"cash-dom": "^8.1.5",
45-
"feather-icons": "^4.29.1",
46-
"sass": "^1.69.5",
47-
"svelte": "^4.2.2",
48-
"vite": "^4.5.0"
45+
"feather-icons": "^4.29.2",
46+
"sass": "^1.77.5",
47+
"svelte": "^4.2.18",
48+
"vite": "^5.3.1"
4949
}
5050
}

src/dev/main.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ window.jsonEditor = new JsonEditor(editor, {
4242
theme: options.theme,
4343
live: options.live,
4444
edit: options.edit,
45+
openDepth: 3,
46+
node: {},
4547
})
4648
$ = window.$ = jsonEditor.$
4749
initJsonEditor()
@@ -126,7 +128,9 @@ function visiblePreview(sw)
126128

127129
function randomReplaceSampleItem()
128130
{
129-
jsonEditor.replace(randomPickInArray(samples), true)
131+
jsonEditor.replace(randomPickInArray(samples), {
132+
openDepth: 2,
133+
}, true)
130134
}
131135

132136
function renderPreview(src)

src/docs/components/editor/index.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function setupJsonEditor()
6161
live: true,
6262
theme: $theme,
6363
})
64-
editor.replace($source, false)
64+
editor.replace($source, {}, false)
6565
_editor.addEventListener('update', updateSource)
6666
_editor.addEventListener('context', customContext)
6767
dispatch('init', { instance: editor })

src/json-editor/index.d.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ declare module '@redgoose/json-editor' {
44
live?: boolean
55
theme?: 'system' | 'light' | 'dark'
66
edit?: 'all' | 'value' | 'none'
7+
openDepth?: boolean
8+
node?: object | any[]
79
}
810
type typeData = any[] | object
911
type typeAddNodeOptions = {
@@ -12,7 +14,7 @@ declare module '@redgoose/json-editor' {
1214
}
1315
type typeNames = 'object' | 'array' | 'string' | 'number' | 'boolean' | 'null'
1416

15-
class JsonEditor {
17+
export default class JsonEditor {
1618
// properties
1719
options: typeOptions
1820
context: Context
@@ -28,13 +30,13 @@ declare module '@redgoose/json-editor' {
2830
fold(node: HTMLElement, sw?: boolean): void
2931
clear(): void
3032
destroy(): void
31-
replace(data: typeData, useUpdate?: boolean): void
33+
replace(data: typeData, options?: object, useUpdate?: boolean): void
3234
import(target: HTMLElement, data: typeData, useUpdate?: boolean): void
3335
export(node: HTMLElement, json?: boolean, space?: number): any[]|object
3436
updateLanguage(): void
3537
}
3638

37-
class Context {
39+
export class Context {
3840
constructor(parent: JsonEditor, node: HTMLElement, isRoot: boolean)
3941
close(): void
4042
}

src/json-editor/index.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class JsonEditorCore {
3636
this.el.wrap.append(this.el.body)
3737
this.#changeTheme(this.options.theme)
3838
this.#changeEdit(this.options.edit)
39-
this.replace({}, false)
39+
this.replace(options.node || {}, {}, false)
40+
this.#openNodesWithDepth(options.openDepth)
4041
}
4142

4243
#setOptions(obj, prop, value)
@@ -277,6 +278,20 @@ class JsonEditorCore {
277278
}
278279
}
279280

281+
/**
282+
* depth 이하의 노드를 열어준다.
283+
* @param {number} depth
284+
*/
285+
#openNodesWithDepth(depth = 0)
286+
{
287+
if (!(depth > 0)) return
288+
const $nodes = this.el.body.find('.node:not(.root)')
289+
let arr = []
290+
$nodes.each((i, el) => {
291+
if ($(el).data('depth') < depth) this.fold($(el), true)
292+
})
293+
}
294+
280295
/**
281296
* NODE EVENTS
282297
*/
@@ -620,7 +635,7 @@ class JsonEditorCore {
620635
{
621636
if (!this.el.tree) return
622637
this.el.body.empty()
623-
this.replace({}, false)
638+
this.replace({}, {}, false)
624639
this.#updateTick()
625640
}
626641

@@ -636,15 +651,17 @@ class JsonEditorCore {
636651
/**
637652
* replace
638653
* @param {object|array} data
654+
* @param {object} options
639655
* @param {boolean} useUpdate
640656
*/
641-
replace(data, useUpdate = true)
657+
replace(data, options = {}, useUpdate = true)
642658
{
643659
this.el.body.empty()
644660
data = checkData(data)
645661
const $item = this.#createRoot(data)
646662
this.import($item, data, false, false)
647663
if (useUpdate) this.#updateTick()
664+
if (options?.openDepth) this.#openNodesWithDepth(options?.openDepth)
648665
}
649666

650667
/**

src/json-editor/libs/assets.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export const defaultOptions = {
22
live: false, // live update
33
theme: 'system', // system,light,dark
44
edit: 'all', // all,value,none
5+
openDepth: 0, // open a node with a depth
6+
node: undefined, // initial node
57
}
68

79
export const defaultAddNodeOptions = {

0 commit comments

Comments
 (0)