Download the PHP package nystudio107/craft-code-editor without Composer

On this page you can find all versions of the php package nystudio107/craft-code-editor. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package craft-code-editor

Scrutinizer Code Quality Code Coverage Build Status Code Intelligence Status

Code Editor for Craft CMS 3.x, 4.x & 5.0

Provides a code editor field with Twig & Craft API autocomplete

Requirements

Code Editor requires Craft CMS 3.x, 4.x or 5.x.

Installation

To install Code Editor, follow these steps:

  1. Open your terminal and go to your Craft project:

    cd /path/to/project
  2. Then tell Composer to require the package:

    composer require nystudio107/craft-code-editor

About Code Editor

Code Editor provides a full-featured code editor with syntax highlighting via the powerful Monaco Editor (the same editor that is the basis for VS Code).

It also can handle hundreds of other code languages, such as JavaScript, TypeScript, CSS, Markdown, and a whole lot more.

Code Editor provides full autocompletion for Twig filters/functions/tags, and the full Craft CMS API, including installed plugins:

And it adds hover documentation when you hover the cursor over an expression:

You can also add your own custom Autocompletes, and customize the behavior of the editor.

Code Editor also provides a Yii2 Validator for Twig templates and object templates.

If instead you need a Craft CMS field, use the Code Field plugin, which provides Code Editor wrapped in a field type.

Using Code Editor

Once you've added the nystudio107/craft-code-editor package to your plugin, module, or project, no further setup is needed. This is because it operates as an auto-bootstrapping Yii2 Module.

Code Editor is not a Craft CMS plugin, rather a package to be utilized by a plugin, module, or project.

It can be very easy to add to an existing project, as you can see from the Preparse field pull request that adds it the Preparse plugin.

In the Craft CP

Code Editor works just like the Craft CMS forms macros that should be familiar to plugin and module developers.

Import Macros

Simply import the macros:

Multi-line Editor

Then to create a textarea multi-line editor, do the following:

...where textAreaText is a variable containing the initial text that should be in the editor field. This will create the Twig editor.

To create a textareaField multi-line editor, do the following:

...where textAreaText is a variable containing the initial text that should be in the editor field. This will create the label and instructions, along with the Twig editor.

Single-line Editor

Then to create a text single-line editor, do the following:

...where text is a variable containing the initial text that should be in the editor field. This will create the Twig editor that is restricted to a single line, for simple Twig expressions.

To create a textField single-line editor, do the following:

...where text is a variable containing the initial text that should be in the editor field. This will create the label and instructions, along with the Twig editor that is restricted to a single line, for simple Twig expressions.

Regardless of the macro used, an Asset Bundle containing the necessary CSS & JavaScript for the editor to function will be included, and the editor initialized.

In Frontend Templates

Code Editor also works in frontend templates, but you can disable it via the allowTemplateAccess config setting. This is enabled by default.

There is also a allowFrontendAccess which is disabled by default. This allows access to the codeeditor/autocomplete/index endpoint for Twig & Craft API autocompletes. This is disabled by default, so if you want these completions on the frontend, you'll need to specifically enable it.

Do so by copying the config.php file to the Craft CMS config/ directory, renaming the file to codeeditor.php in the process, then set the allowFrontendAccess setting to true:

Then import the macros:

Create your own <textarea> element and include the necessary JavaScript, passing in the id of your textarea element:

Enabling the allowFrontendAccess setting allows access to the codeeditor/autocomplete/index endpoint, and add the codeeditor/templates directory to the template roots.

The following monacoOptions allow you to make the field read-only (though the user can still interact with the code):

Additional Options

The textarea, textareaField, text, textField, and includeJs macros all take three additional optional parameters:

fieldType

fieldType - an optional 2nd parameter. By default this is set to Code Editor. You only need to change it to something else if you're using a custom Autocomplete (see below)

e.g.:

editorOptions

editorOptions - an optional 4th parameter. This is an EditorOption passed in to configure the Monaco editor. By default, this is an empty object.

You would commonly use editorOptions to specify the language to be used for the Code Editor, or the theme, but you can override any EditorOption you like.

e.g.:

codeEditorOptions

codeEditorOptions - an optional 5th parameter. This object that can contain any data you want to pass from your Twig template down to the Autocomplete. This can be leveraged in custom Autocompletes to pass contextual for a particular field to the Autocomplete (see below)

e.g.:

You can pass in any options you like to codeEditorOptions (which might be used in a custom Autocomplete), but the following pre-defined options have a special meaning:

Using Additional Autocompletes

Code Editor adds autocompletes only when the editor language is twig. The reason is twofold:

Other languages have more robust support, and come with baked-in autocomplete and syntax highlighting.

By default, Code Editor uses the CraftApiAutocomplete & TwigLanguageAutocomplete, but it also includes an optional EnvironmentVariableAutocomplete which provides autocompletion of any Craft CMS Environment Variables and Aliases.

If you want to use the EnvironmentVariableAutocomplete or a custom Autocomplete you write, you'll need to add a little PHP code to your plugin, module, or project:

The above code will add Environment Variable & Alias autocompletes to all of your Code Editor editors.

However, because you might have several instances of a Code Editor on the same page, and they each may provide separate Autocompletes, you may want to selectively add a custom Autocomplete only when the fieldType matches a specific.

Here's an example from the Sprig plugin:

This ensures that the SprigApiAutocomplete Autocomplete will only be added when the fieldType passed into the Code Editor macros is set to SprigField.

Additionally, you may have an Autocomplete that you want to pass config information down to when it is instantiated. You can accomplish that by adding the Autocomplete as an array:

Note that all of the above examples add Autocompletes to the Autocompletes that Code Editor provides by default (CraftApiAutocomplete and TwigLanguageAutocomplete). If you want to replace them entirely, just empty the types[] array first:

Writing a Custom Autocomplete

Autocompletes extend from the base Autocomplete class, and implement the AutocompleteInterface

A simple Autocomplete would look like this:

The $name property is the name of your Autocomplete, and it is used for the autocomplete cache.

The $type property is either AutocompleteTypes::TwigExpressionAutocomplete (which only autocompletes inside of a Twig expression) or AutocompleteTypes::GeneralAutocomplete (which autocompletes everywhere).

The $hasSubProperties property indicates whether your Autocomplete returns nested sub-properties such as foo.bar.baz. This hint helps Code Editor present a better autocomplete experience.

CompleteItem::create() is a factory method that creates a CompleteItem object. You can use the Fluent Model setters as shown above, or you can set properties directly on the model as well. The CompleteItem::add() method adds it to the list of generated Autocompletes.

Your Autocomplete also has a $codeEditorOptions property which will contain any data passed down via the optional 5th codeEditorOptions parameter from your Twig template. This allows you to have contextual information this a particular field.

See the following examples for custom Autocompletes that you can use as a guide when creating your own:

Twig Template Validators

Code Editor also includes two Twig template Validators that you can use to validate Twig templates that are saved as part of a model:

You just add them as a rule on your model, and it will propagate the model with any errors that were encountered when rendering the template:

You can also add in any variables that should be presents in the Twig environment:

For the TwigObjectTemplateValidator, you can also pass in the object that should be used when rendering the object template:

JSON Schema Autocompletes

The Monaco editor that the Code Editor is based on supports JSON Schema for JSON Autocompletes that let you define the schema for an JSON editor instance.

You can play with an example of this in the Monaco playground

Code Editor adds some support to make it a bit easier to do, here's an example from the Craft Code Field plugin on one way to do it via a Twig template:

...where the optionsSchema variable is injected into the template, and contains the contents of the IEditorOptionsSchema.json file.

You could alternatively grab this file via an XHR from an Asset Bundle, or just inline the schema definition directly.

Code Editor Roadmap

Some things to do, and ideas for potential features:

Brought to you by nystudio107


All versions of craft-code-editor with dependencies

PHP Build Version
Package Version
Requires craftcms/cms Version ^3.0.0 || ^4.0.0 || ^5.0.0-beta.1
phpdocumentor/reflection-docblock Version ^5.0.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package nystudio107/craft-code-editor contains the following files

Loading the files please wait ....