-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): add support for state editing
add react component @microlink/react-json-view work on #34
- Loading branch information
1 parent
af0d3d6
commit ccbe253
Showing
7 changed files
with
354 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
server-jetty/src/main/js/src/lg4j-node-output-with-schema.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { JSONEditor } from '@json-editor/json-editor' | ||
|
||
export class LG4JNodeOutput extends HTMLElement { | ||
|
||
static get observedAttributes() { | ||
return ['value']; | ||
} | ||
|
||
#editor | ||
|
||
constructor() { | ||
super() | ||
|
||
const shadowRoot = this.attachShadow({ mode: "open" }); | ||
|
||
const style = document.createElement("style"); | ||
style.textContent = ` | ||
<style> | ||
.je-indented-panel { | ||
margin-top: 0 !important; | ||
margin-bottom: 0 !important; | ||
} | ||
.je-child-editor-holder { | ||
margin-top: 0 !important; | ||
margin-bottom: 0 !important; | ||
} | ||
.je-object__controls { | ||
display: none; | ||
} | ||
.je-header.je-object__title { | ||
display: none; | ||
} | ||
select.je-switcher { | ||
pointer-events: none; | ||
cursor: not-allowed; | ||
visibility: hidden; | ||
} | ||
</style> | ||
` | ||
|
||
shadowRoot.appendChild(style); | ||
|
||
|
||
} | ||
|
||
attributeChangedCallback(name, oldValue, newValue) { | ||
if (name === 'value') { | ||
if (newValue !== null) { | ||
console.debug( "attributeChangedCallback.value", newValue ) | ||
this.#editor?.setValue( JSON.parse(newValue) ); | ||
|
||
} | ||
} | ||
} | ||
|
||
connectedCallback() { | ||
|
||
const value = this.textContent | ||
console.debug( "value", value ) | ||
this.#initialize( JSON.parse(value) ) | ||
|
||
} | ||
|
||
disconnectedCallback() { | ||
|
||
this.#editor.destroy(); | ||
|
||
this.#editor = null | ||
} | ||
|
||
#initialize( value ) { | ||
const schema = { | ||
"type": "object", | ||
|
||
"additionalProperties": { | ||
"options": { | ||
"collapsed": true, | ||
}, | ||
"type": [ | ||
"array", | ||
] | ||
}, | ||
|
||
"additionalProperties": { | ||
"options": { | ||
}, | ||
"type": [ | ||
"string", "number", "boolean", "object", "array", | ||
] | ||
} | ||
|
||
}; | ||
|
||
const container = document.createElement('div') | ||
|
||
this.shadowRoot.appendChild( container ); | ||
|
||
this.#editor = new JSONEditor(container, { | ||
schema: schema, | ||
no_additional_properties: false, | ||
required_by_default: true, | ||
disable_edit_json: true, | ||
disable_properties: true, | ||
disable_array_delete_all_rows: true, | ||
disable_array_add: true, | ||
array_controls_top: true, | ||
disable_array_delete: true, | ||
disable_array_reorder: true, | ||
|
||
startval: value | ||
}); | ||
|
||
} | ||
} | ||
|
||
|
||
window.customElements.define('lg4j-node-output', LG4JNodeOutput); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React from 'react' | ||
import ReactDOM from 'react-dom/client'; | ||
import ReactJson from '@microlink/react-json-view' | ||
|
||
export class LG4JNodeOutput extends HTMLElement { | ||
|
||
static get observedAttributes() { | ||
return ['value']; | ||
} | ||
|
||
constructor() { | ||
super() | ||
|
||
const shadowRoot = this.attachShadow({ mode: "open" }); | ||
|
||
const style = document.createElement("style"); | ||
style.textContent = ` | ||
<style> | ||
</style> | ||
` | ||
|
||
shadowRoot.appendChild(style); | ||
|
||
} | ||
|
||
attributeChangedCallback(name, oldValue, newValue) { | ||
if (name === 'value') { | ||
if (newValue !== null) { | ||
console.debug( "attributeChangedCallback.value", newValue ) | ||
} | ||
} | ||
} | ||
|
||
connectedCallback() { | ||
|
||
const value = this.textContent | ||
|
||
console.debug( "value", value ) | ||
|
||
this.root = this.#createRoot( JSON.parse(value) ) | ||
|
||
} | ||
|
||
disconnectedCallback() { | ||
|
||
this.root?.unmount() | ||
|
||
} | ||
|
||
|
||
/** | ||
* Represents an event triggered when an edit occurs. | ||
* | ||
* @typedef {Object} EditEvent | ||
* @property {Record<string, any>} existing_src - The original source object before the edit. | ||
* @property {any} existing_value - The original value before the edit. | ||
* @property {string} name - The name of the field that was edited. | ||
* @property {string[]} namespace - The namespace path indicating where the edit occurred. | ||
* @property {any} new_value - The new value after the edit. | ||
* @property {Record<string, any>} updated_src - The updated source object after the edit. | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {EditEvent} e | ||
*/ | ||
#onEdit( e ) { | ||
|
||
console.dir( e ) | ||
|
||
} | ||
|
||
|
||
#createRoot( value ) { | ||
|
||
const mountPoint = document.createElement('span'); | ||
this.shadowRoot.appendChild(mountPoint); | ||
|
||
const root = ReactDOM.createRoot(mountPoint); | ||
|
||
const component = React.createElement( ReactJson, { | ||
src: value, | ||
enableClipboard: false, | ||
displayDataTypes: false, | ||
name: false, | ||
collapsed: true, | ||
theme: 'monokai', | ||
onEdit: e => this.#onEdit(e) | ||
|
||
} ) | ||
|
||
root.render( component ) | ||
|
||
return root | ||
} | ||
} | ||
|
||
|
||
window.customElements.define('lg4j-node-output', LG4JNodeOutput); |
Oops, something went wrong.