Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I drag my saved serialized json into the editor? #605

Open
xs-nayeem opened this issue Jan 21, 2024 · 4 comments
Open

How do I drag my saved serialized json into the editor? #605

xs-nayeem opened this issue Jan 21, 2024 · 4 comments

Comments

@xs-nayeem
Copy link

When I export my template, I get the JSON of the template. Now I want to drag that JSON into the editor. Now, only an React Component can be dragged into the editor. How do I drag the JSON in the editor?

@shota-f
Copy link

shota-f commented Jan 23, 2024

If you have JSON(string), you can use deserialize method in Editor-actions in place you want to load.
This is very simple example.
try:

  1. click Save button(probably equivalent to the serialized json you have)
  2. click Load button
import { Editor, Frame, useEditor } from "@craftjs/core"
import { FC, useState } from "react"

const TextNode = () => {
  return <div style={{ color: "purple" }}>hello</div>
}

const EditorForSaving: FC<{ setJSONData: (json: string) => void }> = ({
  setJSONData,
}) => {
  const { query } = useEditor()

  return (
    <div style={{ marginBottom: 24, backgroundColor: "#fafafa" }}>
      <h2>Editor for Saving</h2>
      <div>
        <button
          onClick={() => {
            setJSONData(query.serialize())
          }}
        >
          Save
        </button>
      </div>
      <Frame>
        <TextNode />
      </Frame>
    </div>
  )
}

const EditorForLoading: FC<{ jsonData: string }> = ({ jsonData }) => {
  const {
    actions: { deserialize },
  } = useEditor()

  return (
    <div style={{ backgroundColor: "#fff" }}>
      <h2>Editor for Loading</h2>
      <div>
        <button
          onClick={() => {
            if (jsonData) {
              deserialize(jsonData)
            }
          }}
        >
          Load
        </button>
      </div>
      <Frame>
        {
          // loaded in heare
        }
      </Frame>
    </div>
  )
}

export default function Demo() {
  const [jsonData, setJSONData] = useState("")

  return (
    <div>
      <h1>Save and Load example</h1>
      <Editor resolver={{ TextNode }}>
        <EditorForSaving setJSONData={setJSONData} />
      </Editor>
      <Editor resolver={{ TextNode }}>
        <EditorForLoading jsonData={jsonData} />
      </Editor>
    </div>
  )
}

@xs-nayeem
Copy link
Author

xs-nayeem commented Jan 28, 2024

If you have JSON(string), you can use deserialize method in Editor-actions in place you want to load. This is very simple example. try:

  1. click Save button(probably equivalent to the serialized json you have)
  2. click Load button
import { Editor, Frame, useEditor } from "@craftjs/core"
import { FC, useState } from "react"

const TextNode = () => {
  return <div style={{ color: "purple" }}>hello</div>
}

const EditorForSaving: FC<{ setJSONData: (json: string) => void }> = ({
  setJSONData,
}) => {
  const { query } = useEditor()

  return (
    <div style={{ marginBottom: 24, backgroundColor: "#fafafa" }}>
      <h2>Editor for Saving</h2>
      <div>
        <button
          onClick={() => {
            setJSONData(query.serialize())
          }}
        >
          Save
        </button>
      </div>
      <Frame>
        <TextNode />
      </Frame>
    </div>
  )
}

const EditorForLoading: FC<{ jsonData: string }> = ({ jsonData }) => {
  const {
    actions: { deserialize },
  } = useEditor()

  return (
    <div style={{ backgroundColor: "#fff" }}>
      <h2>Editor for Loading</h2>
      <div>
        <button
          onClick={() => {
            if (jsonData) {
              deserialize(jsonData)
            }
          }}
        >
          Load
        </button>
      </div>
      <Frame>
        {
          // loaded in heare
        }
      </Frame>
    </div>
  )
}

export default function Demo() {
  const [jsonData, setJSONData] = useState("")

  return (
    <div>
      <h1>Save and Load example</h1>
      <Editor resolver={{ TextNode }}>
        <EditorForSaving setJSONData={setJSONData} />
      </Editor>
      <Editor resolver={{ TextNode }}>
        <EditorForLoading jsonData={jsonData} />
      </Editor>
    </div>
  )
}

@shota-f I can load the JSON on click. There is no problem with that. What I want is, when I save the template as JSON I am showing those JSON as my template in the sidebar. Now I want to drag it from the sidebar to the editor? How do I do that? Every drag element must be in the Editor Resolver. How do i handle that is case of Serialized JSON?

@jorgegonzalez
Copy link

Bumping as I have the same question

@xs-nayeem
Copy link
Author

Bumping as I have the same question

@jorgegonzalez check out this issue. #316

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants