react-ace icon indicating copy to clipboard operation
react-ace copied to clipboard

Readme examples breaks when switching from java to json?

Open ajthinking opened this issue 3 years ago • 3 comments

How to use JSON instead of Java?

Java works

import AceEditor from "react-ace";
import "ace-builds/src-noconflict/mode-java";
import "ace-builds/src-noconflict/theme-github";

<AceEditor
mode="java"
theme="github"
name="UNIQUE_ID_OF_DIV"
editorProps={{ $blockScrolling: true }}
/>  

JSON does not work?

import AceEditor from "react-ace";
import "ace-builds/src-noconflict/mode-json";
import "ace-builds/src-noconflict/theme-github";

<AceEditor
mode="json"
theme="github"
name="UNIQUE_ID_OF_DIV"
editorProps={{ $blockScrolling: true }}
/>  

The error

Refused to execute script from '...../worker-json.js' because its MIME type ('text/html') is not executable. (anonymous) @ 2f896707-86be-497a-93b2-e1711942d7c7:1 2f896707-86be-497a-93b2-e1711942d7c7:1 Uncaught DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope': The script at '...../worker-json.js' failed to load.

ajthinking avatar Jan 06 '21 20:01 ajthinking

Same error here, because loading query is done with https://domain.tld/current/path/worker-json.js wich doesn't exists (and 404 page is an html one).

Adding import 'ace-builds/src-noconflict/worker-json' doesn't solve the pb.

My code is

import React from 'react'
import AceEditor from 'react-ace'
import 'ace-builds/src-noconflict/mode-json'
import 'ace-builds/src-noconflict/mode-xml'
import 'ace-builds/src-noconflict/theme-github'

const TextEditor = ({ input: { value, onChange, onBlur, onFocus, name }, onValidate, mode }) => (
  <AceEditor
    name={name}
    mode={mode}
    theme="github"
    // …

dcaillibaud avatar Feb 15 '21 15:02 dcaillibaud

The pb seems solved for me (thanks to https://ace.c9.io/#nav=howto), with

import React from 'react'
import AceEditor from 'react-ace'
// cf https://ace.c9.io/#nav=howto for webpack use
import 'ace-builds/webpack-resolver'

const TextEditor = ({ input: { value, onChange, onBlur, onFocus, name }, onValidate, mode }) => (
  <AceEditor
    name={name}
    mode={mode}
    theme="github"
    // …

dcaillibaud avatar Feb 15 '21 17:02 dcaillibaud

Just ran into this issue, and I was only able to fixing by piecing together bits of information from this issue and others.

First, I'm using webpack 5, so file-loader is deprecated. Thankfully it still works. I'm sure there is a better solution that is more inline with the new webpack workflow, but the first thing I did was install file-loader:

yarn add -D file-loader

This is required by ace-builds, as mentioned here: https://ace.c9.io/#nav=howto

For the import statements, ORDER MATTERS:

// ...
import 'ace-builds';
import 'ace-builds/webpack-resolver';
import AceEditor from 'react-ace';
// ...
<AceEditor
  mode='json'
  theme='monokai'
  onChange={function (value, event) {
    console.log(value, event, this);
  }}
  fontSize={14}
  showPrintMargin
  showGutter
  highlightActiveLine
  value={myValue}
  width='100%'
  height='100%'
  setOptions={{
    enableBasicAutocompletion: false,
    enableLiveAutocompletion: false,
    enableSnippets: false,
    showLineNumbers: true,
    tabSize: 2,
  }}
/>

Hope this helps someone.

brainthinks avatar Oct 20 '21 18:10 brainthinks