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

For those using nuxt 3 #111

Open
Zalcherei opened this issue Oct 23, 2022 · 2 comments
Open

For those using nuxt 3 #111

Zalcherei opened this issue Oct 23, 2022 · 2 comments

Comments

@Zalcherei
Copy link

Zalcherei commented Oct 23, 2022

Not really an issue, but for those using nuxt 3:

Create file named "vue-editor.client.vue" in "./components/" (file name can be anything, but remember ".client.vue").
You can paste this inside of it:

<script lang="ts">
  import EditorJS from "@editorjs/editorjs";

  interface Props {
    holder: string;
    config: Object;
    initialized: Function;
  }

  export default defineNuxtComponent({
    name: "vue-editor-js",
    props: {
      holder: {
        type: String,
        default: () => "vue-editor-js",
        require: true,
      },
      config: {
        type: Object,
        default: () => ({}),
        require: true,
      },
      initialized: {
        type: Function,
        default: () => {},
      },
    },
    setup: (props, context) => {
      const state = reactive({ editor: null });

      function initEditor(props: Props) {
        destroyEditor();
        state.editor = new EditorJS({
          holder: props.holder || "vue-editor-js",
          ...props.config,
        });
        props.initialized(state.editor);
      }

      function destroyEditor() {
        if (state.editor) {
          state.editor.destory();
          state.editor = null;
        }
      }

      onMounted((_: void) => initEditor(props));

      return { props, state };
    },
  });
</script>

OR

<script lang="ts" setup>
  import EditorJS from "@editorjs/editorjs";

  const editorProps = defineProps({
    holder: {
      type: String,
      default: () => "vue-editor-js",
      require: true,
    },
    config: {
      type: Object,
      default: () => ({}),
      require: true,
    },
    initialized: {
      type: Function,
      default: () => {},
    },
  });

  const state = reactive({ editor: null });

  const destroyEditor = () => {
    if (state.editor) {
      state.editor.destory();
      state.editor = null;
    }
  };

  const initEditor = () => {
    destroyEditor();
    state.editor = new EditorJS({
      holder: editorProps.holder || "vue-editor-js",
      ...editorProps.config,
    });
    editorProps.initialized(state.editor);
  };

  onMounted((_: void) => initEditor());
</script>
<template>
  <div :id="holder"></div>
</template>

Inside "app.vue" you should add following:

<script lang="ts" setup>
onMounted(() => {
  const VueEditor = () => import("../components/nuxt-editor.client.vue");
});
</script>
<template>
  <ClientOnly>
    <vue-editor />
  </ClientOnly>
</template> 

Or if you want easiest way:

Inside ./components/vue-editor.client.vue:

<script lang="ts" setup>
  import EditorJS from "@editorjs/editorjs";
  // @ts-ignore
  import Header from "@editorjs/header";

  onMounted(() => {
    new EditorJS({
      holder: "vue-editor-js",
      autofocus: true,
      tools: {
        header: {
          class: Header,
          config: {
            placeholder: "Enter a header",
            levels: [1, 2, 3, 4, 5, 6],
            defaultLevel: 3,
          },
        },
      },
    });
  });
</script>
<template>
  <div id="vue-editor-js"></div>
</template>

inside app.vue:

<template>
  <ClientOnly>
    <vue-editor />
  </ClientOnly>
</template>
@vin-ni
Copy link

vin-ni commented Dec 12, 2022

Here's another approach on how to implement it:

In components i have a file called editorjs.vue

<template>
  <div ref="editorDom" />
</template>

<script lang="ts" setup>
// import
import EditorJS from "@editorjs/editorjs";

// variables
const editor = ref<EditorJS>();
const editorDom = ref<HTMLElement>();

onMounted(() => {
  editor.value = new EditorJS({
    holder: editorDom.value
  });
});
</script>

I can then use it anywhere like that:

<editorjs />

@FalkoJoseph
Copy link

Here's another approach on how to implement it:

In components i have a file called editorjs.vue

<template>
  <div ref="editorDom" />
</template>

<script lang="ts" setup>
// import
import EditorJS from "@editorjs/editorjs";

// variables
const editor = ref<EditorJS>();
const editorDom = ref<HTMLElement>();

onMounted(() => {
  editor.value = new EditorJS({
    holder: editorDom.value
  });
});
</script>

I can then use it anywhere like that:

<editorjs />

Thank you! This worked for me!

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