Skip to content
This repository was archived by the owner on Jun 24, 2023. It is now read-only.

Commit b0c59ca

Browse files
committed
roots
1 parent d0cc1ee commit b0c59ca

19 files changed

+388
-153
lines changed

packages/editable/src/EditableElement.ts

+88-20
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@ import {
88
mergeRefs
99
} from "@editable-jsx/state"
1010
import { toast } from "@editable-jsx/ui"
11-
import { Dispatch, SetStateAction, useEffect, useState } from "react"
12-
import { Editor } from "./Editor"
11+
import {
12+
createElement,
13+
Dispatch,
14+
FC,
15+
SetStateAction,
16+
useEffect,
17+
useState
18+
} from "react"
19+
import { Editable, editable } from "./editable"
20+
import { EditableDocument, EditableRoot } from "./EditableRoot"
1321
import { PropChange } from "./prop-types/types"
22+
import { REF_SYMBOL } from "./REF_SYMBOL"
1423

1524
/**
1625
* An editable element is a wrapper around a React element that can be edited in the editor.
@@ -32,7 +41,12 @@ import { PropChange } from "./prop-types/types"
3241
*
3342
* */
3443
export class EditableElement<
35-
Ref extends { name?: string; visible?: boolean } = any
44+
Ref extends {
45+
name?: string
46+
visible?: boolean
47+
[k: string]: any
48+
[REF_SYMBOL]: any
49+
} = any
3650
> extends EventTarget {
3751
ref?: Ref
3852
childIds: string[] = []
@@ -41,7 +55,7 @@ export class EditableElement<
4155
forwardedRef: boolean = false
4256
dirty: any = false
4357
properties: ControlledStore = createControlledStore()
44-
editor: Editor = {} as any
58+
// editor: Editor = {} as any
4559
index: string | undefined
4660
refs = {
4761
setKey: null as Dispatch<SetStateAction<number>> | null,
@@ -53,6 +67,12 @@ export class EditableElement<
5367
mounted: boolean = false
5468
args = [] as any[]
5569

70+
/** assigned by the creator */
71+
ownerDocument!: EditableDocument
72+
73+
/** assigned by the creator */
74+
root!: EditableRoot
75+
5676
constructor(
5777
public id: string,
5878
public source: JSXSource,
@@ -63,6 +83,10 @@ export class EditableElement<
6383
super()
6484
}
6585

86+
getRootNode() {
87+
return this.#root
88+
}
89+
6690
remount() {
6791
this.refs.setKey?.((i) => i + 1)
6892
}
@@ -75,19 +99,63 @@ export class EditableElement<
7599
return this.properties.useStore((s) => s.data["name"].value as string)
76100
}
77101

78-
useChildren() {
79-
return this.editor.useStore((s) => [
80-
...(s.elements[this.id]?.children ?? [])
81-
])
82-
}
102+
// useChildren() {
103+
// return this.editor.useStore((s) => [
104+
// ...(s.elements[this.id]?.children ?? [])
105+
// ])
106+
// }
83107

84108
useIsDirty() {
85109
return this.properties?.useStore(
86110
(s) => Object.keys(this.changes).length > 0
87111
)
88112
}
89113

90-
appendChild(id: string) {
114+
appendChild(element: EditableElement) {
115+
this.childIds.push(element.id)
116+
}
117+
118+
removeChild(element: EditableElement) {
119+
this.childIds = this.childIds.filter((id) => id !== element.id)
120+
}
121+
122+
appendNewElement(
123+
element: EditableElement,
124+
componentType: string | FC,
125+
props: any
126+
) {
127+
console.log("appendNewElement", componentType)
128+
if (typeof componentType === "string") {
129+
console.log("appendNewElement", componentType)
130+
element.refs.setMoreChildren?.((children) => [
131+
...children,
132+
createElement(editable[componentType], {
133+
_source: {
134+
...element.source,
135+
lineNumber: -1,
136+
elementName: componentType
137+
},
138+
key: children.length,
139+
...props
140+
})
141+
])
142+
} else {
143+
console.log("appendNewElement", componentType)
144+
element.refs.setMoreChildren?.((children) => [
145+
...children,
146+
createElement(Editable, {
147+
component: componentType,
148+
_source: {
149+
...element.source,
150+
lineNumber: -1,
151+
elementName:
152+
componentType.displayName || componentType.name || undefined
153+
},
154+
key: children.length,
155+
...props
156+
} as any)
157+
])
158+
}
91159
}
92160

93161
update(source: JSXSource, props: any) {
@@ -169,7 +237,7 @@ export class EditableElement<
169237

170238
setRef(el: Ref) {
171239
this.ref = el
172-
this.editor.setRef(this, el)
240+
el[REF_SYMBOL] = this
173241
this.dispatchEvent(
174242
new CustomEvent("ref-changed", {
175243
detail: {
@@ -229,11 +297,11 @@ export class EditableElement<
229297
}
230298

231299
useIsSelected() {
232-
return this.editor.useState((state) => this.isSelected)
300+
return this.ownerDocument.useState((state) => this.isSelected)
233301
}
234302

235303
get isSelected() {
236-
return this.editor.state.context.selectedId === this.treeId
304+
return this.ownerDocument.state.context.selectedId === this.treeId
237305
}
238306

239307
useCollapsed(): [any, any] {
@@ -318,7 +386,7 @@ export class EditableElement<
318386
let controls = {}
319387
let entity = this
320388

321-
this.editor.plugins.forEach((plugin) => {
389+
this.ownerDocument.plugins.forEach((plugin) => {
322390
if (plugin.controls && plugin.applicable(entity)) {
323391
Object.assign(controls, plugin.controls(entity))
324392
}
@@ -328,8 +396,8 @@ export class EditableElement<
328396
}
329397

330398
get icon() {
331-
for (var i = this.editor.plugins.length - 1; i >= 0; i--) {
332-
let plugin = this.editor.plugins[i]
399+
for (var i = this.ownerDocument.plugins.length - 1; i >= 0; i--) {
400+
let plugin = this.ownerDocument.plugins[i]
333401
if (plugin.icon && plugin.applicable(this)) {
334402
return plugin.icon(this)
335403
}
@@ -349,7 +417,7 @@ export class EditableElement<
349417
)
350418

351419
try {
352-
console.log(await this.editor.save(diffs))
420+
console.log(await this.ownerDocument.save(diffs))
353421
this.changes = {}
354422
this.changed = false
355423
} catch (e: any) {
@@ -372,12 +440,12 @@ export class EditableElement<
372440

373441
get children() {
374442
return this.childIds
375-
.map((id) => this.editor.getElementById(id)!)
443+
.map((id) => this.ownerDocument.getElementById(id)!)
376444
.filter(Boolean)
377445
}
378446

379447
get parent() {
380-
return this.editor.getElementById(this.parentId!)
448+
return this.ownerDocument.getElementById(this.parentId!)
381449
}
382450

383451
getObjectByPath<T>(path: string[]): T {
@@ -395,7 +463,7 @@ export class EditableElement<
395463
if (path.length > 1) {
396464
for (let i = 0; i < path.length - 1; i++) {
397465
el = el?.[path[i]]
398-
let edit = this.editor.findEditableElement(el)
466+
let edit = this.getRootNode().findEditableElement(el)
399467
if (edit) {
400468
editable = edit
401469
remainingPath = path.slice(i + 1)

0 commit comments

Comments
 (0)