Giter VIP home page Giter VIP logo

constrained-editor-plugin's Introduction

Constrained Editor Plugin

A Plugin which adds restrictions to the model of monaco-editor, so that only some parts of the code are editable and rest will become read-only. Please click here for Demo and click here for Documentation

Stats

CodeQL

Table of Contents

Demo

Demo.mov

How to install using NPM

npm i constrained-editor-plugin

Problem Statement

Monaco Editor is one of the most popular code editors in the market. It is developed by Microsoft.The Monaco Editor is the code editor that powers VS Code. Although it is packed with lot of features, it didn't have the feature to constrain the editable area, which is to basically allow editing in only certain parts of the content.

This plugin solves this issue, and will help you add that functionality into your monaco editor instance, without any performance issues.

Sample code

// Include constrainedEditorPlugin.js in your html.
require.config({ paths: { vs: '../node_modules/monaco-editor/dev/vs' } });
require(['vs/editor/editor.main'], function () {
  const container = document.getElementById('container')
  const editorInstance = monaco.editor.create(container, {
    value: [
      'const utils = {};',
      'function addKeysToUtils(){',
      '',
      '}',
      'addKeysToUtils();'
    ].join('\n'),
    language: 'javascript'
  });
  const model = editorInstance.getModel();

  // - Configuration for the Constrained Editor : Starts Here
  const constrainedInstance = constrainedEditor(monaco);
  constrainedInstance.initializeIn(editorInstance);
  constrainedInstance.addRestrictionsTo(model, [{
    // range : [ startLine, startColumn, endLine, endColumn ]
    range: [1, 7, 1, 12], // Range of Util Variable name
    label: 'utilName',
    validate: function (currentlyTypedValue, newRange, info) {
      const noSpaceAndSpecialChars = /^[a-z0-9A-Z]*$/;
      return noSpaceAndSpecialChars.test(currentlyTypedValue);
    }
  }, {
    range: [3, 1, 3, 1], // Range of Function definition
    allowMultiline: true,
    label: 'funcDefinition'
  }]);
  // - Configuration for the Constrained Editor : Ends Here
});

Walkthrough of Sample code

  • constrainedEditor is the globally available class to create an instance of the ConstrainedEditor. This instance has to be created by sending in the monaco variable as an argument.

  • constrainedEditor.initializeIn(editorInstance) is where the constrained editor will add the necessary functions into the editor instance. The Editor returned by the monaco editor during the monaco.editor.create() call should be sent here.

  • constrainedEditor.addRestrictionsTo(model,restrictions) is where the constrained editor will add restrictions to the model.

For detailed documentation on available APIs, click here

Sample Code for monaco-editor react

import { useRef } from "react";
import Editor from "@monaco-editor/react";
import { constrainedEditor } from "constrained-editor-plugin";

function App() {
  const editorRef = useRef(null);
  let restrictions = [];

  function handleEditorDidMount(editor, monaco) {
    editorRef.current = editor;
    const constrainedInstance = constrainedEditor(monaco);
    const model = editor.getModel();
    constrainedInstance.initializeIn(editor);
    restrictions.push({
      range: [1, 1, 2, 10],
      allowMultiline: true
    });
    constrainedInstance.addRestrictionsTo(model, restrictions);
  }

  return (
    <div className="App">
      <Editor
        onMount={handleEditorDidMount}
      />
    </div>
  );
}

export default App;

Thanks @chethan-devopsnow for the sample code : Click here to view discussion about this code

Potential Applications

Coding Tutorial Applications

This plugin can be used in applications which teach programming tutorials, where the application can be made in such as way that it allows users to edit in only certain places

Interviewing applications

This can be used to prevent the candidate to accidentally mess up the boilerplate code given to them.

Contributions and Issues

This project is open source and you are welcome to add more features to this plugin.

If your find any issue, please raise it here

License

Licensed under the MIT License.

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.