Skip to content

Problems using code-input in Vue.js #123

Open
@Bowrna

Description

@Bowrna

I want to use the syntax highlighter for my editor, and see the content only with syntax highlighted and in editable mode. But currently I see it in two different modes like below.
Is there any possibility to do it that way?
Screenshot 2024-11-04 at 5 30 37 PM

I am using this in VueJS and this is the code I have right now.

<template>
  <div ref="htmlEditor" class="html-editor">
    <textarea
      ref="editor"
      :value="value"
      @input="handleInput"
      is="code-input"
      data-language="html"
      :data-readonly="disabled"
      spellcheck="false"
    ></textarea>
  </div>
</template>

<script>
import Prism from 'prismjs';

export default {
  props: {
    value: { type: String, default: '' },
    language: { type: String, default: 'html' },
    disabled: Boolean,
  },

  methods: {
    handleInput(event) {
      this.$emit('input', event.target.value);
    },

    initializeEditor() {
      const textarea = this.$refs.editor;
      textarea.setAttribute('is', 'code-input');
      textarea.setAttribute('data-language', this.language);

      // Register Prism for syntax highlighting if needed
      if (window.codeInput) {
        window.codeInput.registerTemplate(
          'syntax-highlighted',
          window.codeInput.templates.prism(Prism, [])
        );
        window.codeInput.setDefaultTemplate('syntax-highlighted');
      }
    },
  },

  mounted() {
    this.initializeEditor();
  },
};
</script>

<style>
/* Hide the non-editable preview content */
.code-input pre[aria-hidden="true"] {
  display: none !important;
}

/* Additional styling */
.html-editor {
  width: 100%;
  position: relative;
}

.html-editor textarea {
  font-size: 15px;
  min-height: 200px;
  width: 100%;
  padding: 8px;
  border: none;
  resize: none;
}

.token.tag { font-weight: bold; }
.token.attr-name { color: #111; }
.token.attr-value { color: #0066cc; }

.html-editor textarea:focus {
  outline: none;
  border-color: #0066cc;
}
</style>

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:externalA bug/feature due to integration with another library / JavaScript framework, but not the browser.bugSomething isn't workinghelp wantedExtra attention is neededpriority:medium

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions