Giter VIP home page Giter VIP logo

wc-monaco-editor's Introduction

<wc-monaco-editor> Easily Embed a Monaco Editor

GitHub Releases NPM Releases Latest Status Release Status

Discord Published on WebComponents.org

Installation

Installation

npm i @vanillawc/wc-monaco-editor

Import from NPM

<script type="module" src="node_modules/@vanillawc/wc-monaco-editor/index.js"></script>

Import from CDN

<script type="module" src="https://cdn.jsdelivr.net/gh/vanillawc/wc-monaco-editor@1/index.js"></script>

Demo

Try it out here

Usage

Attributes

  • id - the editor id (default editor)
  • src - load the source from an external file
  • language - set the source language
  • font-size - set the font size (default 14px);
  • tab-size - set the tab size (in spaces) (default 2)
  • style - CSS styling (default height: 100%; width: 100%;)
  • no-minimap - disables the source minimap when present
  • word-wrap - enables word wrapping when present
  • wrap-indent - set the indent ['none','same','indent','deepindent'] for word wrapped lines (default none)

Note: The ID attribute is required if there are multiple editors

Properties

  • editor - the Monaco editor instance
  • src - get/set the src attribute from JS
  • value - get/set the editor's contents
  • tabSize - get/set the tab-index attribute from JS

Basic Usage

To edit plaintext

<wc-monaco-editor></wc-monaco-editor>

Language Support

Language support enables syntax highlighting, code completion, etc.

<wc-monaco-editor language="javascript"></wc-monaco-editor>

External Source

Load an external source file with the src attribute

<wc-monaco-editor src="sample.js" language="javascript"></wc-monaco-editor>

Advanced Configuration

A config can be provided for advanced use cases that require the full spectrum of Monaco Editor options.

<wc-monaco-editor config="config.json"></wc-monaco-editor>

config.json

{
  "language": "javascript",
  "minimap": {
    "enabled": false
  },
  "tabSize": 2,
  "fontSize": 16,
  "wordWrap": "on",
  "lineNumbersMinChars": 3,
  "wrappingIndent": "same",
  "mouseWheelZoom": true,
  "copyWithSyntaxHighlighting": false
}

Note: See the EditorOptions official documentation for all available options.

Contributing

See CONTRIBUTING.md

wc-monaco-editor's People

Contributors

evanplaice avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

wc-monaco-editor's Issues

Uncaught TypeError: Object prototype may only be an Object or null: undefined

Uncaught TypeError: Object prototype may only be an Object or null: undefined
    at setPrototypeOf (<anonymous>)
    at i (index.js:1)
    at s (index.js:1)
    at index.js:1
    at Object.self.MonacoEnvironment.getWorkerUrl (index.js:1)
    at o (index.js:1)
    at Object.self.MonacoEnvironment.getWorkerUrl (index.js:1)
    at o (index.js:1)
    at Module.self.MonacoEnvironment.getWorkerUrl (index.js:1)
    at o (index.js:1)

My code:

import React from "react";
import { App } from "./demo";
export default { title: "Button" };
const s = document.createElement("script");
s.type = "module";
s.src = "https://cdn.jsdelivr.net/npm/@vanillawc/wc-monaco-editor/index.js";
document.body.appendChild(s);
function Editor(props: { value: string }) {
    const ref = React.useRef<HTMLDivElement>(null);
    React.useEffect(() => {
        if (!ref.current) return;
        const dom = document.createElement("wc-wc-monaco-editor");
        dom.setAttribute("id", Math.random().toString());
        dom.setAttribute("language", "javascript");
        ref.current.appendChild(dom);
        return () => dom.remove();
    }, [ref.current]);
    React.useEffect(() => {
        if (!ref.current) return;
        // @ts-ignore
        ref.current.firstChild!.value = props.value;
    }, [ref.current, props.value]);
    return <div ref={ref}></div>;
}
export const Demo = () => (
    <>
                    <Editor value="return <div>AAAAAAAAA</div>;" />
                    <Editor value="return <div>Hello world!</div>;" />
    </>
);

React: "'importMeta' isn't currently enabled"

Blocked: waiting for Webpack to add support for import.meta

I tried to use wc-monaco-editor on a new React project that I created with create-react-app.
But I always get this error message:

./node_modules/@vanillawc/wc-monaco-editor/index.js
SyntaxError: C:\wegdamit\empty-react\node_modules\@vanillawc\wc-monaco-editor\index.js: Support for the experimental syntax 'importMeta' isn't currently enabled (9:45):

   7 | /* eslint no-undef: 0 */
   8 | 
>  9 | const monacoDir = new URL('monaco/', import.meta.url);
     |                                             ^
  10 | 
  11 | // eslint-disable-next-line
  12 | self.MonacoEnvironment = {

Add @babel/plugin-syntax-import-meta (https://git.io/vbKK6) to the 'plugins' section of your Babel config to enable parsing.

Also adding @babel/plugin-syntax-import-meta as a Babel plugin didn't work. Because it's a CRA project I couldn't edit the Babel config directly, so I followed this guide and used customize-cra and react-app-rewired to change the config: https://devinschulz.com/modify-create-react-apps-babel-configuration-without-ejecting/

Add Inline Source Support

Add the ability to load the source from an inline <script> tag

<wc-monaco-editor language="javascript">
  <script type="wc-content">
      function myGoodPerson(){
        return "what can I do for you ?"
      }
  </script>
</wc-monaco-editor>

Unexpected usage at z.loadForeignModule

The import from CDN (on README.md) doesn't work, or I'm doing something really wrong.
Everytime I type a letter it throws a new error.
error
I'm using rollup + svelte and can't really import from npm / don't know how.
Tried several ways, only thing working was just putting it into the static folder, but that's pretty slow and hacky.
So the CDN solution would be best.

how to pass configuration options?

Hi, i think this project is great! just an obvious question to ask before i evaluate whether i can use it - how do you pass configuration options to monaco? it doesnt seem possible from the docs.

`editor` is `undefined`

Hello, thank you for the great component!

I am trying to use this web component in Elm and it is working well.
But there is one small issue if I am trying to put property value from Elm to the web component.

Sorry, I am not good at developing using web components, so perhaps I am missing something.

I am getting the following call chain when I am trying to set value property.
image

I have modified the code in index.js to check this.editor for null as it is failing without it.

And it seems that Elm somehow is able to call value property before the method connectedCallback has been called (so this.editor is null).

I am wondering if it is the issue with the web component itself (missing null check) or I am doing something wrong here.
Thank you in advance for your help!

Update to version 0.29

This has an essential fix for many WebComp users in that the ariaContentArea can now be specificied by the developer. this prevents stuff being arbitraily added to the DOM which breaks some language's virtual dom implementations

microsoft/vscode#131865

Instructions for demo

I tried to load the demo page by dragging and dropping index.html into my browser and got

Access to script at 'file:///Users/.../wc-monaco-editor-main/index.min.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

I also tried it with a simple dev server and got

Failed to load module script: The server responded with a non-JavaScript, CSS MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.