GithubHelp home page GithubHelp logo

Comments (7)

just-boris avatar just-boris commented on July 23, 2024 2

The older answer still applies: #456 (comment)

As the name suggests, code editor is for editable use-cases only. For read-only cases use another component. Loading Ace library for read-only use-case is a very big overkill. There are more lightweight options out there.

Updated the issue title to reflect what it really tracks (providing an official component for viewing code)

from components.

pan-kot avatar pan-kot commented on July 23, 2024 1

Hello Nichita,

The code editor currently does not support a read-only mode, but it is on our radar. The main concern is the impact on the bundle size the code editor has. Ideally, we want the readonly variant to be lightweight.

A viable alternative four now could be, for instance, https://prismjs.com/ to bring syntax highlight to the code snippets.

from components.

nichita-pasecinic avatar nichita-pasecinic commented on July 23, 2024 1

@fr0stf0x you could just change the config of the global 'Ace' instance to readOnly mode

useEffect(() => {
    import('ace-builds').then((aceLib) => {
      aceLib.config.set('themePath', CDN);
      aceLib.config.set('basePath', CDN);
      aceLib.config.set('modePath', CDN);
      aceLib.config.set('themePath', CDN);
      aceLib.config.set('workerPath', CDN);
     // this one
      aceLib.config.set('readOnly', readOnly); 
      aceLib.config.set('showPrintMargin', false);
      setAce(aceLib);
      setLoading(false);
    });
  }, [readOnly]);

In my case readOnly is a component prop

from components.

connorlanigan avatar connorlanigan commented on July 23, 2024 1

We have recently released the CodeView component. Details and examples can be found on the component's page on our website.

from components.

nichita-pasecinic avatar nichita-pasecinic commented on July 23, 2024

editor ref is not really necessary at this point, setting ace config is enough for now ace.config.set('readOnly', true).

from components.

nichita-pasecinic avatar nichita-pasecinic commented on July 23, 2024

IDK if there would be a more advances use case where editorRef would be necessary, but I'll surrey get back here 😄

Thanks, nice ui-kit!

from components.

fr0stf0x avatar fr0stf0x commented on July 23, 2024

@pan-kot @nichita-pasecinic any updates on this?

by far my solution is prevent any keydown or mousedown events but when I press backspace or delete, the code editor still displays the changed value

// MyCodeEditor.tsx

function preventChangeIfNeeded(event: KeyboardEvent) {
  // 'backspace' and 'delete' do not go into this
  const { target } = event;

  if (target && (target as HTMLTextAreaElement).classList.contains('ace_text-input')) {
    event.preventDefault();
    event.stopPropagation();
  }
}

function hideCursor(event: MouseEvent) {
  const cursorLayer = document.querySelector('.ace_cursor-layer');

  if (cursorLayer) {
    event.preventDefault();
    event.stopPropagation();
    (cursorLayer as HTMLDivElement).style.display = 'none';
  }
}

function MyCodeEditor({readonly}) {
// ...
  useEffect(() => {
    if (readonly) {
      window.addEventListener('keydown', preventChangeIfNeeded);
      window.addEventListener('mousedown', hideCursor);

      return () => {
        window.removeEventListener('keydown', preventChangeIfNeeded);
        window.removeEventListener('mousedown', hideCursor);
      };
    }
  }, [readonly]);
// ...
}

from components.

Related Issues (20)

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.