All Projects → Pranomvignesh → constrained-editor-plugin

Pranomvignesh / constrained-editor-plugin

Licence: MIT license
This project is to restrict the editable area in the monaco-editor

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to constrained-editor-plugin

Form Generator
✨Element UI表单设计及代码生成器
Stars: ✭ 5,522 (+9940%)
Mutual labels:  monaco-editor
Monaco Collab Ext
Adds collaborative editing capabilities to the Monaco Editor
Stars: ✭ 83 (+50.91%)
Mutual labels:  monaco-editor
Apubplat
Devops自动化部署、堡垒机开源项目、Web Terminal
Stars: ✭ 167 (+203.64%)
Mutual labels:  monaco-editor
Vue Monaco
MonacoEditor component for Vue.js
Stars: ✭ 471 (+756.36%)
Mutual labels:  monaco-editor
Vue Eslint Demo
The online demo to check `eslint-plugin-vue`.
Stars: ✭ 29 (-47.27%)
Mutual labels:  monaco-editor
Browser Ext Github Monaco
This extension brings the famous Monaco editor to Github
Stars: ✭ 114 (+107.27%)
Mutual labels:  monaco-editor
Ngx Monaco Editor
Monaco Editor component for Angular 2 and Above
Stars: ✭ 347 (+530.91%)
Mutual labels:  monaco-editor
Vue Monaco Editor
Monaco Editor Vue Component
Stars: ✭ 187 (+240%)
Mutual labels:  monaco-editor
Notemaster
NoteMaster is an smart minimalistic persistent note-taking app to help boost productivity.
Stars: ✭ 40 (-27.27%)
Mutual labels:  monaco-editor
Yn
Yank Note 一款面向程序员的 Markdown 笔记应用。支持加密文档,代码片段运行,内置终端,图表嵌入,HTML 小工具。
Stars: ✭ 143 (+160%)
Mutual labels:  monaco-editor
Mongood
A MongoDB GUI with Fluent Design
Stars: ✭ 540 (+881.82%)
Mutual labels:  monaco-editor
D3 Id3
iD3: an Integrated Development Environment for D3.js
Stars: ✭ 789 (+1334.55%)
Mutual labels:  monaco-editor
Masscode
A free and open source code snippets manager for developers.
Stars: ✭ 1,878 (+3314.55%)
Mutual labels:  monaco-editor
Monaco Languageclient
NPM module to connect Monaco editor with language servers
Stars: ✭ 419 (+661.82%)
Mutual labels:  monaco-editor
Web Code
A text editor for the web based around monaco
Stars: ✭ 174 (+216.36%)
Mutual labels:  monaco-editor
Go Playground
Better Go Playground powered by React and Monaco editor
Stars: ✭ 354 (+543.64%)
Mutual labels:  monaco-editor
Orchestra
One language to be RegExp's Successor. Visually readable and rich, technically safe and extended, naturally scalable, advanced, and optimized
Stars: ✭ 103 (+87.27%)
Mutual labels:  monaco-editor
Sprite
🖌 Draw charts in code. Render in real-time. Embed anywhere as .png.
Stars: ✭ 201 (+265.45%)
Mutual labels:  monaco-editor
React Monaco Editor
Monaco Editor for React.
Stars: ✭ 2,536 (+4510.91%)
Mutual labels:  monaco-editor
Monaco Vim
VIM keybindings for monaco editor
Stars: ✭ 131 (+138.18%)
Mutual labels:  monaco-editor

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].