Skip to content

Commit

Permalink
fix: solved reactivity issue (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: Puria Nafisi Azizi <[email protected]>
  • Loading branch information
bbtgnn and puria authored Jul 29, 2024
1 parent a00d6de commit e6fd8e6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"generate": "stencil generate",
"sbb": "stencil build && storybook build --docs",
"dev": "NODE_ENV=development npm-run-all -p dev:*",
"dev:start": "stencil build --watch --serve",
"dev:start": "stencil build --watch --dev --serve",
"dev:storybook": "sleep 6 && storybook dev -p 6006"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export namespace Components {
interface DyneCodeEditor {
"class": string;
"config": EditorStateConfig;
"content": string;
"getContent": () => Promise<string>;
"name": string;
"setContent": (text: string) => Promise<void>;
Expand Down Expand Up @@ -95,6 +96,7 @@ declare namespace LocalJSX {
interface DyneCodeEditor {
"class"?: string;
"config"?: EditorStateConfig;
"content"?: string;
"name"?: string;
}
interface DyneInline {
Expand Down
36 changes: 26 additions & 10 deletions src/components/dyne-code-editor/dyne-code-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Element, Method, State, h, Prop } from '@stencil/core';
import { Component, Element, Method, State, h, Prop, Host, Watch } from '@stencil/core';

import { basicSetup } from 'codemirror';
import { EditorState, EditorStateConfig } from '@codemirror/state';
Expand All @@ -16,16 +16,30 @@ import { nanoid } from 'nanoid';
export class DyneCodeEditor {
@Element() el: HTMLElement;

@Prop() name = nanoid(5);
@Prop() config: EditorStateConfig = { extensions: basicSetup };
@Prop() class = '';
@Prop({ reflect: true }) name = nanoid(5);
@Prop({ reflect: true }) config: EditorStateConfig = { extensions: basicSetup };
@Prop({ reflect: true }) class = '';
@Prop({ reflect: true }) content = '';

@State() view: EditorView;
view: EditorView;

@Watch('content')
async updateEditorContent() {
await this.setContent(this.content);
}

private get state() {
return this.view.state;
}

//

async componentDidLoad() {
this.view = createEditor(this.getContainer(), { ...this.config, doc: this.content });
}

//

@Method()
async getContent(): Promise<string> {
return this.state.doc.toString();
Expand All @@ -40,18 +54,20 @@ export class DyneCodeEditor {

//

async componentDidLoad() {
this.view = createEditor(this.getContainer(), this.config);
}

private getContainer() {
const editorContainer = this.el.shadowRoot?.getElementById(this.name);
if (!editorContainer) throw new Error('Container not initialized');
return editorContainer;
}

//

render() {
return <div id={this.name} class={this.class}></div>;
return (
<Host>
<div id={this.name} class={this.class}></div>
</Host>
);
}
}

Expand Down
40 changes: 21 additions & 19 deletions src/components/dyne-slangroom-editor/dyne-slangroom-editor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Element, State, Prop, Method, h } from '@stencil/core';
import { Component, Element, State, Prop, Method, h, Watch } from '@stencil/core';

// import { dracula } from 'thememirror';
import { defaultKeymap } from '@codemirror/commands';
Expand Down Expand Up @@ -76,6 +76,7 @@ export class DyneSlangroomEditor {
data: parseJsonObjectWithFallback(data),
keys,
});

this.isExecuting = false;
}

Expand Down Expand Up @@ -155,22 +156,25 @@ export class DyneSlangroomEditor {
<Section title={EditorId.CONTRACT}>
<dyne-code-editor
name={EditorId.CONTRACT}
config={{ doc: this.contract, extensions: this.keyboardExtension }}
content={this.contract}
config={{ extensions: this.keyboardExtension }}
></dyne-code-editor>
</Section>

<Section title={EditorId.DATA}>
<dyne-code-editor
name={EditorId.DATA}
config={{ doc: this.data, extensions: [this.keyboardExtension, json()] }}
content={this.data}
config={{ extensions: [this.keyboardExtension, json()] }}
></dyne-code-editor>
</Section>

{this.keysMode == 'editor' && (
<Section title={EditorId.KEYS}>
<dyne-code-editor
name={EditorId.KEYS}
config={{ doc: this.keys, extensions: [this.keyboardExtension, json()] }}
content={this.keys}
config={{ extensions: [this.keyboardExtension, json()] }}
></dyne-code-editor>
</Section>
)}
Expand All @@ -180,8 +184,7 @@ export class DyneSlangroomEditor {
<Container className="md:grow md:w-0 shrink-0 md:basis-2">
{this.showEmptyState && <EmptyState />}
{this.isExecuting && <Spinner />}
{this.value && <ValueRenderer value={this.value} />}
{this.error && <ErrorRenderer error={this.error} />}
{this.result && <ResultRenderer result={this.result} />}
</Container>
</div>
</div>
Expand Down Expand Up @@ -218,13 +221,19 @@ function parseJsonObjectWithFallback(string: string): Record<string, unknown> {

// Partials

function ResultRenderer(props: { result: SlangroomResult }) {
const { result } = props;
if (result.success === true) return <ValueRenderer value={result.value} />;
else return <ErrorRenderer error={result.error} />;
}

function ValueRenderer(props: { value: SlangroomValue }) {
return (
<Section title="Result">
<dyne-code-editor
name={EditorId.RESULT}
content={JSON.stringify(props.value, null, 2)}
config={{
doc: JSON.stringify(props.value, null, 2),
extensions: [json()],
}}
></dyne-code-editor>
Expand Down Expand Up @@ -259,7 +268,7 @@ function ErrorRenderer(props: { error: SlangroomError }) {
// text-slate-800 -> #1E293B
function AnsiRenderer(props: { text: string; className?: string }) {
const { text, className = '' } = props;
const converter = new Convert({bg: '#F1F5F9', fg: '#1E293B'});
const converter = new Convert({ bg: '#F1F5F9', fg: '#1E293B' });
return <pre class={className} innerHTML={converter.toHtml(text)}></pre>;
}

Expand All @@ -268,23 +277,16 @@ function ZencodeErrorRenderer(props: { error: ZencodeRuntimeError }) {
return (
<div>
<Title name="trace" className="mb-1" />
<dyne-code-editor
config={{
doc: error.trace.join('\n'),
}}
></dyne-code-editor>
<dyne-code-editor name="trace" content={error.trace.join('\n')}></dyne-code-editor>

<Title name="logs" className="mb-1" />
<dyne-code-editor
config={{
doc: error.logs.join('\n'),
}}
></dyne-code-editor>
<dyne-code-editor name="logs" content={error.logs.join('\n')}></dyne-code-editor>

<Title name="heap" className="mb-1" />
<dyne-code-editor
name="heap"
content={JSON.stringify(error.heap, null, 2)}
config={{
doc: JSON.stringify(error.heap, null, 2),
extensions: [json()],
}}
></dyne-code-editor>
Expand Down

0 comments on commit e6fd8e6

Please sign in to comment.