Skip to content

redgoose-dev/json-editor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

167 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

JSON Editor

"json-editor"λŠ” μ›ΉλΈŒλΌμš°μ €μ—μ„œ json 데이터λ₯Ό νŽΈμ§‘ν•˜λŠ” μ—λ””ν„°μž…λ‹ˆλ‹€.
ν…μŠ€νŠΈ νŽΈμ§‘νˆ΄μ„ μ‚¬μš©ν•  수 μ—†κ±°λ‚˜ json 데이터λ₯Ό μ†μ‰½κ²Œ 닀루기 μœ„ν•˜μ—¬ λ§Œλ“€μ–΄μ§„ ν”„λ‘œκ·Έλž¨ μž…λ‹ˆλ‹€. 이 μ—λ””ν„°λŠ” λ…λ¦½μ μœΌλ‘œ λ™μž‘ν•˜λ©° λ©”μ„œλ“œλ₯Ό μ‹€ν–‰ν•˜μ—¬ μ—λ””ν„°μ˜ κΈ°λŠ₯을 μ‘°μž‘ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

ꢌμž₯λ˜λŠ” μ‚¬μš©ν™˜κ²½μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

  • λ°μŠ€ν¬νƒ‘
  • 큰 ν™”λ©΄μ˜ νƒœλΈ”λ¦Ώ

에디터가 μ–΄λ–»κ²Œ μ‚¬μš©λ˜λŠ”μ§€ 데λͺ¨λ₯Ό 확인해볼 수 μžˆμŠ΅λ‹ˆλ‹€. 데λͺ¨νŽ˜μ΄μ§€μ—μ„œ μž„μ‹œλ‘œ μ—λ””ν„°λ‘œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

데λͺ¨λ§ν¬: https://redgoose-dev.github.io/json-editor/

Using

javascript와 cssλ₯Ό λΆˆλŸ¬μ™€μ„œ 클래슀 μΈμŠ€ν„΄μŠ€λ₯Ό λ§Œλ“œλŠ” λ°©μ‹μœΌλ‘œ μ‹œμž‘ν•©λ‹ˆλ‹€.

import JsonEditor from '@redgoose/json-editor'
import '@redgoose/json-editor/css'

const jsonEditor = new JsonEditor(document.getElementById('target'), {
  live: true,
  theme: 'system',
  edit: 'all',
  node: {},
  openDepth: 2,
})

Options

μ»΄ν¬λ„ŒνŠΈλ₯Ό μ‚¬μš©ν• λ•Œμ— μ‚¬μš©ν•˜λŠ” μ˜΅μ…˜κ°’μž…λ‹ˆλ‹€.

  • live / (true,false)
    이 값을 μ‚¬μš©ν•˜λ©΄ 데이터가 μ—…λ°μ΄νŠΈ λ λ•Œλ§ˆλ‹€ update λ©”μ„œλ“œκ°€ ν˜ΈμΆœλ˜λ©΄μ„œ μ—…λ°μ΄νŠΈ 이벀트λ₯Ό ν˜ΈμΆœν•©λ‹ˆλ‹€.
  • theme / (system,light,dark)
    닀크λͺ¨λ“œλ₯Ό μ‚¬μš©ν•˜λŠ”μ§€μ— λŒ€ν•œ κ°’μž…λ‹ˆλ‹€.
  • edit / (all,value,none)
    μ»¨νŠΈλ‘€ν•  수 μžˆλŠ” λ²”μœ„λ₯Ό μ •ν•©λ‹ˆλ‹€.
  • node / ({},[])
    클래슀λ₯Ό μ΄ˆκΈ°ν™”ν• λ•Œ μ‚¬μš©ν•˜λŠ” λ…Έλ“œ 데이터 κ°’μž…λ‹ˆλ‹€.
  • openDepth / 0
    x번째 뎁슀의 λ…Έλ“œκ°€ μ—΄λ¦¬λŠ”μ§€μ— λŒ€ν•˜μ—¬ μ •ν•©λ‹ˆλ‹€.

Methods

μΈμŠ€ν„΄μŠ€μ—μ„œ μ‚¬μš©ν•  수 μžˆλŠ” λ©”μ„œλ“œλ“€μ΄λ©° ν•„μš”ν• λ•Œμ— 적절히 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. (μ•„λ‹ˆλ©΄ 클래슀 ν”„λ‘œν† νƒ€μž…μœΌλ‘œ 미리 ν™•μž₯ν•΄λ‘˜ 수 μžˆμŠ΅λ‹ˆλ‹€.)
μ‚¬μš©ν•  수 μžˆλŠ” 곡개 λ©”μ„œλ“œλ“€μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

ps. λ¨Όμ € μ†ŒμŠ€μ½”λ“œλŠ” λ‹€μŒκ³Ό 같이 μ •μ˜ν•©λ‹ˆλ‹€.

const jsonEditor = new JsonEditor()
const node = document.querySelector('.node') // node

addNode

object, array λ…Έλ“œμ—μ„œ μžμ‹ λ…Έλ“œλ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€.

jsonEditor.addNode(node, data, options, useUpdate, useUpdateCount)
  • node: 데이터λ₯Ό μΆ”κ°€ν•  λ…Έλ“œ μ—˜λ¦¬λ¨ΌνŠΈ
  • data: μΆ”κ°€ν•  데이터
  • options: μΆ”κ°€μ˜΅μ…˜
  • useUpdate: λ…Έλ“œλ₯Ό μΆ”κ°€ν•˜κ³  μ—…λ°μ΄νŠΈ λ©”μ„œλ“œλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.
  • useUpdateCount: λΆ€λͺ¨λ…Έλ“œ 데이터 갯수λ₯Ό μ—…λ°μ΄νŠΈν•©λ‹ˆλ‹€.

λ‹€μŒκ³Ό 같이 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

jsonEditor.addNode(
  node,
  { key: '', type: 'string', value: 'metal' },
  options,
  true,
  true
)

removeNode

λ…Έλ“œλ₯Ό μ‚­μ œν•©λ‹ˆλ‹€.

jsonEditor.removeNode(node, useUpdate)
  • node: 데이터λ₯Ό μΆ”κ°€ν•  λ…Έλ“œ μ—˜λ¦¬λ¨ΌνŠΈ
  • useUpdate: λ…Έλ“œλ₯Ό μ‚­μ œν•˜κ³  μ—…λ°μ΄νŠΈ λ©”μ„œλ“œλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.

λ‹€μŒκ³Ό 같이 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

jsonEditor.removeNode(node, true)

changeType

λ…Έλ“œμ˜ νƒ€μž…μ„ λ³€κ²½ν•©λ‹ˆλ‹€.

jsonEditor.changeType(node, type, useUpdate)
  • node: νƒ€μž…μ„ λ³€κ²½ν•  λ…Έλ“œ μ—˜λ¦¬λ¨ΌνŠΈ
  • type: νƒ€μž…μ˜ 이름 (object,array,string,number,boolean,null)
  • useUpdate: λ…Έλ“œ νƒ€μž…μ„ λ³€κ²½ν•˜κ³  μ—…λ°μ΄νŠΈ λ©”μ„œλ“œλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.

λ‹€μŒκ³Ό 같이 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

jsonEditor.changeType(node, 'boolean', true)

changeKey

λ…Έλ“œμ˜ keyλ₯Ό λ³€κ²½ν•©λ‹ˆλ‹€.

jsonEditor.changeKey(node, keyword)
  • node: νƒ€μž…μ„ λ³€κ²½ν•  λ…Έλ“œ μ—˜λ¦¬λ¨ΌνŠΈ
  • keyword: ν‚€μ›Œλ“œ

changeValue

λ…Έλ“œμ˜ valueλ₯Ό λ³€κ²½ν•©λ‹ˆλ‹€.

jsonEditor.changeValue(node, value)
  • node: νƒ€μž…μ„ λ³€κ²½ν•  λ…Έλ“œ μ—˜λ¦¬λ¨ΌνŠΈ
  • value: κ°’

duplicate

λ…Έλ“œλ₯Ό λ³΅μ œν•©λ‹ˆλ‹€.

jsonEditor.duplicate(node, useUpdate)
  • node: λ³΅μ œν•  λ…Έλ“œ μ—˜λ¦¬λ¨ΌνŠΈ
  • useUpdate: μ‹€ν–‰ν•˜κ³  μ—…λ°μ΄νŠΈ λ©”μ„œλ“œλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.

λ‹€μŒκ³Ό 같이 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

jsonEditor.duplicate(node, true)

fold

λ…Έλ“œλ₯Ό μ ‘κ±°λ‚˜ νŽΌμΉ©λ‹ˆλ‹€.

jsonEditor.fold(node, true)

import

데이터λ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€.

jsonEditor.import(node, data, useUpdate, useUpdateCount)
  • node: 데이터λ₯Ό μΆ”κ°€ν•  λ…Έλ“œ μ—˜λ¦¬λ¨ΌνŠΈ
  • data: κ°€μ Έμ˜¬ 데이터
  • useUpdate: λ…Έλ“œλ₯Ό μΆ”κ°€ν•˜κ³  μ—…λ°μ΄νŠΈ λ©”μ„œλ“œλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.
  • useUpdateCount: λΆ€λͺ¨λ…Έλ“œ 데이터 갯수λ₯Ό μ—…λ°μ΄νŠΈν•©λ‹ˆλ‹€.

λ‹€μŒκ³Ό 같이 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

jsonEditor.import(node, { foo: 'bar' }, true, true)

replace

데이터λ₯Ό λͺ¨λ‘ κ΅μ²΄ν•©λ‹ˆλ‹€.

jsonEditor.replace(data, options, useUpdate)
  • data: μƒˆλ‘œ ꡐ체할 데이터
  • options: μ˜΅μ…˜
    • openDepth: 데이터가 κ΅μ²΄λ λ•Œ λ…Έλ“œκ°€ μ—΄λ¦¬λŠ” x번째 뎁슀
  • useUpdate: λ…Έλ“œλ₯Ό μˆ˜μ •ν•˜κ³  μ—…λ°μ΄νŠΈ λ©”μ„œλ“œλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.

λ‹€μŒκ³Ό 같이 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

jsonEditor.replace({ foo: 'bar' }, {
  openDepth: 2,
}, true)

export

μ—λ””ν„°μ˜ 데이터λ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€.

jsonEditor.export(node, json, space)
  • node: κ°€μ Έμ˜¬ λ°μ΄ν„°μ˜ λ…Έλ“œ μ—˜λ¦¬λ¨ΌνŠΈ
  • json: json ν˜•μ‹μ˜ 데이터 λ³€ν™˜μ—¬λΆ€
  • space: json 곡백

λ‹€μŒκ³Ό 같이 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

const space = 2 // null,2,'\t'
jsonEditor.export(node, true, space)

clear

λ…Έλ“œμ˜ λ‚΄μš©μ„ λΉ„μ›λ‹ˆλ‹€.

jsonEditor.clear()

destroy

λ§Œλ“€μ–΄μ§„ μΈμŠ€ν„΄μŠ€λ₯Ό νŒŒκ΄΄ν•©λ‹ˆλ‹€.

jsonEditor.destroy()

Events

μ—λ””ν„°μ—μ„œ μΌμ–΄λ‚œ 일듀을 μ™ΈλΆ€μ—μ„œ μ΄λ²€νŠΈλ¦¬μŠ€λ„ˆλ‘œ λ°›μ•„μ˜¬ 수 μžˆμŠ΅λ‹ˆλ‹€.

ps. λ¨Όμ € μ†ŒμŠ€μ½”λ“œλŠ” λ‹€μŒκ³Ό 같이 μ •μ˜ν•©λ‹ˆλ‹€.

const jsonEditor = new JsonEditor()
const wrap = jsonEditor.el.wrap.get(0) // wrap element

update

μ—λ””ν„°μ˜ λ‚΄μš©μ΄ μˆ˜μ •λœλ‹€λ©΄ μ‹€ν–‰λ˜λŠ” μ΄λ²€νŠΈμž…λ‹ˆλ‹€.

wrap.addEventListener('update', ({ detail }) => {
  console.log('updated data', src)
})
  • detail: 데이터 객체

context

μ»¨ν…μŠ€νŠΈ 메뉴가 μ—΄λ¦΄λ•Œλ§ˆλ‹€ μ‹€ν–‰λ˜λŠ” μ΄λ²€νŠΈμž…λ‹ˆλ‹€. 메뉴λ₯Ό μ»€μŠ€ν„°λ§ˆμ΄μ¦ˆ ν•  수 있으며 제곡된 νŒŒλΌλ©”ν„° 값을 μ΄μš©ν•˜μ—¬ 상황에 맞게 메뉴λ₯Ό μ‘°μž‘ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

wrap.addEventListener('context', ({ detail: { body, node, type, isRoot, $ } }) => {
  if (!isRoot) return
  const $ul = $(body).children()
  const $items = $(`
    <li><button type="button" data-key="#1">custom #1</button></li>
    <li><button type="button" data-key="#2">custom #2</button></li>
  `)
  $items.find('button').on('click', (e) => {
    console.log('click item-key:', $(e.currentTarget).data('key'))
    jsonEditor.context.close()
  })
  $ul.append($items)
})
  • body: μ—΄λ¦° μ»¨ν…μŠ€νŠΈ 메뉴 μ˜μ—­. 이 μ—˜λ¦¬λ¨ΌνŠΈμ—λ‹€ ν•­λͺ©μ„ μ‘°μž‘ν•˜λ©΄ λ©λ‹ˆλ‹€.
  • node: μ„ νƒλœ λ…Έλ“œ μ—˜λ¦¬λ¨ΌνŠΈ
  • type: μ„ νƒλœ λ…Έλ“œμ˜ νƒ€μž…
  • isRoot: ν˜„μž¬ μ„ νƒλœ λ…Έλ“œκ°€ λ£¨νŠΈμΈμ§€ κ΅¬λΆ„ν•˜λŠ” κ°’
  • $: μ»΄ν¬λ„ŒνŠΈ λ‚΄λΆ€μ—μ„œ μ‚¬μš©ν•˜λŠ” Cashλ₯Ό μ΄μš©ν•˜μ—¬ μ†μ‰½κ²Œ dom을 λ‹€λ£° 수 μžˆμŠ΅λ‹ˆλ‹€.

Exports guide

λ‹€μŒ 경둜λ₯Ό μ°Έκ³ ν•˜μ—¬ λͺ¨λ“ˆλ“€μ„ import κΈ°λŠ₯을 μ΄μš©ν•˜λŠ”λ° μ°Έκ³ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

  • @redgoose/json-editor: μ½”μ–΄ 라이브러리
  • @redgoose/json-editor/css: μŠ€νƒ€μΌμ‹œνŠΈ
  • @redgoose/json-editor/lib/umd: μ½”μ–΄ 라이브러리 UMD

Custom style

μ—λ””ν„°μ˜ λ””μžμΈμ„ μˆ˜μ •ν•  수 μžˆλŠ” μš”μ†Œλ“€μ„ λ³€μˆ˜ν™” μ‹œμΌ°μŠ΅λ‹ˆλ‹€.
λ‹€μŒκ³Ό 같이 μ™ΈλΆ€ μ˜μ—­μ—μ„œ 에디터 μŠ€νƒ€μΌμ„ μ»€μŠ€ν„°λ§ˆμ΄μ¦ˆ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

.editor {
  --json-editor-color-base: red;
  --json-editor-color-focus: blue;
}
@media (prefers-color-scheme: dark) {
  .editor {
    --json-editor-color-base: green;
    --json-editor-color-focus: yellow;
  }
}

main.scss 파일의 μ½”λ“œλ₯Ό μ°Έκ³ ν•˜μ—¬ 직접 μŠ€νƒ€μΌμ„ νŽΈμ§‘ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

Extention language

클래슀 λ©”μ„œλ“œλ₯Ό κ΅μ²΄ν•˜μ—¬ 에디터 ν…μŠ€νŠΈλ₯Ό λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

JsonEditor.prototype.updateLanguage = function()
{
  this.lang = Object.assign(this.lang, {
    nodeChangeSort: 'λ…Έλ“œ μˆœμ„œλ³€κ²½',
    nodeContextMenu: 'λ…Έλ“œλ©”λ‰΄',
    nodeFold: 'μ ‘κΈ°/펼치기',
    contextChangeType: 'νƒ€μž…λ³€κ²½',
    contextInsertNode: 'λ…Έλ“œμΆ”κ°€',
    contextTypeObject: '객체',
    contextTypeArray: 'λ°°μ—΄',
    contextTypeString: '문자',
    contextTypeNumber: '번호',
    contextTypeBoolean: 'λΆ€μšΈ',
    contextTypeNull: '널',
    contextDuplicate: 'λ…Έλ“œλ³΅μ œ',
    contextRemove: 'λ…Έλ“œμ‚­μ œ',
  })
}

Developing a wrapper

JSON Editor 에디터λ₯Ό web component, react, vue, svelte 같은 ν™˜κ²½μ—μ„œ μ‚¬μš©ν•  수 μžˆλ„λ‘ μ»΄ν¬λ„ŒνŠΈλ₯Ό λž˜ν•‘ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
λ‹€μŒ 링크λ₯Ό μ°Έκ³ ν•˜μ—¬ κ°œλ°œν•  수 μžˆμŠ΅λ‹ˆλ‹€.

About

JSON data editor and management

Resources

License

Stars

Watchers

Forks

Contributors