load and save data on start

This commit is contained in:
Reorx 2023-02-05 16:26:47 +08:00
parent d0ecf537d9
commit d3c4bf38d6
1 changed files with 12 additions and 6 deletions

View File

@ -7,7 +7,10 @@ import objectPath from 'object-path';
import { JSONEditor } from '@json-editor/json-editor/dist/jsoneditor'; import { JSONEditor } from '@json-editor/json-editor/dist/jsoneditor';
import * as sampleModule from '../../sample.resume.json'; import * as sampleModule from '../../sample.resume.json';
import { saveCVJSON } from '../lib/store'; import {
getCVData,
saveCVJSON,
} from '../lib/store';
import { import {
createElement, createElement,
downloadContent, downloadContent,
@ -100,6 +103,11 @@ jsoncvSchema.title = 'Resume'
// change some descriptions // change some descriptions
jsoncvSchema.properties.meta.properties.lastModified.description += '. This will be automatically updated when downloading.' jsoncvSchema.properties.meta.properties.lastModified.description += '. This will be automatically updated when downloading.'
// init data
let data = getCVData()
if (!data) data = sampleModule.default
// initialize editor // initialize editor
registerTheme(JSONEditor) registerTheme(JSONEditor)
registerIconLib(JSONEditor) registerIconLib(JSONEditor)
@ -110,11 +118,9 @@ const editor = new JSONEditor(elEditorContainer, {
iconlib: 'myiconlib', iconlib: 'myiconlib',
disable_array_delete_all_rows: true, disable_array_delete_all_rows: true,
no_additional_properties: true, no_additional_properties: true,
// startval: exampleData, startval: data,
}); });
editor.on('ready',() => { editor.on('ready',() => {
// editor.setValue(exampleData)
// add anchor to each schema element // add anchor to each schema element
document.querySelectorAll('[data-schemapath]').forEach(el => { document.querySelectorAll('[data-schemapath]').forEach(el => {
const schemapath = el.getAttribute('data-schemapath') const schemapath = el.getAttribute('data-schemapath')
@ -122,7 +128,7 @@ editor.on('ready',() => {
}) })
}) })
function getCVData() { function getEditorData() {
const data = editor.getValue() const data = editor.getValue()
return { return {
data, data,
@ -136,7 +142,7 @@ const $outputHTML = $('.output-html')
// listen to change // listen to change
editor.on('change', () => { editor.on('change', () => {
console.log('on editor change') console.log('on editor change')
const {json} = getCVData() const {json} = getEditorData()
$outputJSON.text(json) $outputJSON.text(json)
// save to localstorage // save to localstorage