2.0.1 • Published 2 years ago

sharedb-monaco v2.0.1

Weekly downloads
49
License
MIT
Repository
github
Last release
2 years ago

sharedb-monaco

npm.io npm.io

Two-way bindings between ShareDB and the Monaco Editor

Developed for the CodeCollab project.

Tested and works well with monaco-react.

This library also supports multi-editor support on the same document as well as Connection management to optimise network usage when no editors are attached.

Install

Using NPM:

$ npm install sharedb-monaco

Usage

Sample usage of this library in action

import ShareDBMonaco from "sharedb-monaco";
import monaco from "monaco-editor";

// Get a monaco editor (ICodeEditor) instance
let editor = monaco.editor.create(document.getElementById("editor"), {
    value: "print('Hello World!')",
    language: "python"
});

let binding = new ShareDBMonaco(options);
let model = binding.add(editor, attachOptions);

new ShareDBMonaco(options)

  • options \

    • id \ ID of the ShareDB document
    • namespace \ namespace of document within ShareDB, to be equal to that on the server
    • sharePath \ Path on ShareDB JSON object to apply operations to. For example, if your ShareDB document is structured as { foo: "", bar: "" }, set basharePath: "foo" for ShareDBMonaco to apply operations to foo
    • connection \<shareDb.Connection> | \ If a string is passed, it'll be passed to the constructor of new WebSocket(connection), otherwise it'll reuse the ShareDB Connection passed.
    • viewOnly \ Should this document ignore local writes. Default: false
    • loadingText \ Value to initialise monaco model with while loading the ShareDB document. Default: "Loading..."

    Either of the following options must also be supplied:

    • monaco \ The monaco object.
    • uri \<monaco.Uri> A Uri object for the new model.

    OR

    • model \<monaco.editor.ITextModel> The monaco model to bind the ShareDB document to.

Instantiating ShareDBMonaco without monaco and uri set and instead setting model will not allow you to use the setLangId() and setModelUri() methods. Language inference for the add() will also be disabled.


binding.add(editor[, attachOptions])

Adds an ICodeEditor instance to the ShareDBMonaco bindings. All document changes will now be listened for and applied to this instance.

  • editor \<monaco.editor.ICodeEditor> A monaco editor instance to bind listeners to.
  • attachOptions \
    • model \<monaco.editor.ITextModel> ITextModel instance to infer languageId from.
    • langId \ Language Id to set the language mode for editor
    • uri \<monaco.Uri> Sets this Uri to the current ITextModel and infer language mode from Uri.
  • Returns: \<monaco.editor.ITextModel>

attachOption will only have an effect if ShareDBMonaco was instantiated with monaco and uri set. Otherwise a warning will be thrown.

If model, langId and uri are set, first uri will be applied to the existing model, then the language mode of the model will be set to langId. If langId was not set, model will be used to infer the language mode. TLDR, the order of priority is: 1. langId 2. model 3. uri

NOTE: attachOptions.model does NOT replace the editor's model. It is only used for language inference and nothing more.


binding.setViewOnly(viewOnly)

Toggles the view-only state of the bindings.

  • viewOnly \ Should this document ignore local writes.

binding.setModelUri(uri)

Updates the Uri of the internal model. Requires ShareDBMonaco to be instantiated with monaco and uri.

  • uri \<monaco.Uri> New Uri to set to model
  • Returns: \<monaco.editor.ITextModel>

NOTE: Updating the Uri actually creates a new model as monaco does not have any internal APIs that allows for Uri updating.

If uri matches the uri of the existing model, the model will not be recreated however uri will be used for language inference and the language mode will be updated.


binding.setLangId(langId)

Sets the language of the internal monaco model

  • langId \ The Language Id

binding.remove(id)

Removes and detaches an ICodeEditor instance from this bindings with the specified id.

  • id \ Id of the ICodeEditor instance from ICodeEditor.getId()
  • Returns: \ If all editors have been removed, all document subscriptions will be removed. The \ will be fulfilled when all subscrptions have been closed.

binding.close()

Closes all document subscriptions, detaches all ICodeEditor instances from the ShareDB model, destroys the model. If the ShareDB.Connection was created by this instance, it will be destroyed too.

  • Returns: \ Fulfilled when all subscriptions have been closed.

Plugins

If you would like to use this package with multi-cursor support powered by ShareDB's Presence API, you can check out sharedb-monaco-cursor.

Usage with Next.JS

Any component that uses this module needs to be dynamically loaded with { ssr: false } set. This is due to an issue with the monaco editor requiring access to the navigator object that doesn't exist when the module is loaded on the server.

License

MIT

2.0.1

2 years ago

2.0.0

2 years ago

1.0.11

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago