Skip to content

Commit

Permalink
Merge branch 'release/1.0.17' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Jan 30, 2024
2 parents ea5d186 + f1b5985 commit d2d9c87
Show file tree
Hide file tree
Showing 26 changed files with 176 additions and 90 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/code-analysis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Code Analysis

on:
pull_request: null
push:
branches:
- develop
workflow_dispatch:
permissions:
contents: read
jobs:
code_analysis:
strategy:
fail-fast: false
matrix:
actions:
-
name: 'PHPStan'
run: composer phpstan
-
name: 'Coding Standards'
run: composer fix-cs
name: ${{ matrix.actions.name }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# see https://github.com/shivammathur/setup-php
- uses: shivammathur/setup-php@v2
with:
php-version: 8.2
coverage: none
# composer install cache - https://github.com/ramsey/composer-install
- uses: "ramsey/composer-install@v2"
- run: ${{ matrix.actions.run }}
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

All notable changes to this project will be documented in this file.

## 1.0.17 - 2024.01.29
### Added
* Compatibility with Craft CMS 5
* Add `phpstan` and `ecs` code linting
* Add `code-analysis.yaml` GitHub action

### Changed
* PHPstan code cleanup
* ECS code cleanup

### Fixed
* Use `getSections()` for Craft 3 & 4, and use `getEntries()` for Craft 5

## 1.0.16 - 2023.11.30
### Changed
* Update to `"monaco-editor": "^0.44.0"`
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2022 nystudio107
Copyright (c) nystudio107

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/quality-score.png?b=develop)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/?branch=develop) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/coverage.png?b=develop)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/?branch=develop) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/build.png?b=develop)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/build-status/develop) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/code-intelligence.svg?b=develop)](https://scrutinizer-ci.com/code-intelligence)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/quality-score.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/?branch=v5) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/coverage.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/?branch=v5) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/build.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/build-status/v5) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-code-editor/badges/code-intelligence.svg?b=v5)](https://scrutinizer-ci.com/code-intelligence)

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

Provides a code editor field with Twig & Craft API autocomplete

![Demo](./resources/code-editor-demo.gif)

## Requirements

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

## Installation

Expand Down Expand Up @@ -445,7 +445,7 @@ The Monaco editor that the Code Editor is based on supports [JSON Schema](https:

You can play with an example of this in the [Monaco playground](https://microsoft.github.io/monaco-editor/playground.html?source=v0.37.1#example-extending-language-services-configure-json-defaults)

Code Editor adds some support to make it a bit easier to do, here's an example from the Craft [Code Field plugin](https://github.com/nystudio107/craft-code-field) on one way to do it via a [Twig template](https://github.com/nystudio107/craft-code-field/blob/develop-v3/src/templates/_components/fields/Code_settings.twig#L162):
Code Editor adds some support to make it a bit easier to do, here's an example from the Craft [Code Field plugin](https://github.com/nystudio107/craft-code-field) on one way to do it via a [Twig template](https://github.com/nystudio107/craft-code-field/blob/v5/src/templates/_components/fields/Code_settings.twig#L161):

```js
{% js %}
Expand All @@ -464,7 +464,7 @@ monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
{% endjs %}
```

...where the `optionsSchema` variable is [injected into the template](https://github.com/nystudio107/craft-code-field/blob/develop-v3/src/fields/Code.php#L161), and contains the contents of the [`IEditorOptionsSchema.json`](https://github.com/nystudio107/craft-code-field/blob/develop-v3/src/resources/IEditorOptionsSchema.json) file.
...where the `optionsSchema` variable is [injected into the template](https://github.com/nystudio107/craft-code-field/blob/v5/src/fields/Code.php#L161), and contains the contents of the [`IEditorOptionsSchema.json`](https://github.com/nystudio107/craft-code-field/blob/v5/src/resources/IEditorOptionsSchema.json) file.

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

Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nystudio107/craft-code-editor",
"description": "Provides a code editor field with Twig & Craft API autocomplete",
"type": "yii2-extension",
"version": "1.0.16",
"version": "1.0.17",
"keywords": [
"code",
"editor",
Expand All @@ -28,10 +28,18 @@
}
],
"require": {
"craftcms/cms": "^3.0.0 || ^4.0.0 || ^5.0.0-alpha.1",
"phpdocumentor/reflection-docblock": "^5.0.0"
},
"require-dev": {
"craftcms/cms": "^3.0.0 || ^4.0.0 || ^5.0.0-alpha.1"
"craftcms/ecs": "dev-main",
"craftcms/phpstan": "dev-main",
"craftcms/rector": "dev-main"
},
"scripts": {
"phpstan": "vendor/bin/phpstan --ansi --memory-limit=1G",
"check-cs": "ecs check --ansi",
"fix-cs": "ecs check --fix --ansi"
},
"config": {
"allow-plugins": {
Expand Down
13 changes: 13 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use craft\ecs\SetList;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return static function(ECSConfig $ecsConfig): void {
$ecsConfig->paths([
__DIR__ . '/src',
__FILE__,
]);
$ecsConfig->parallel();
$ecsConfig->sets([SetList::CRAFT_CMS_4]);
};
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- vendor/craftcms/phpstan/phpstan.neon

parameters:
level: 5
paths:
- src
8 changes: 4 additions & 4 deletions src/CodeEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class CodeEditor extends Module implements BootstrapInterface
// Constants
// =========================================================================

const ID = 'codeeditor';
public const ID = 'codeeditor';

const DEFAULT_FIELD_TYPE = 'CodeEditor';
public const DEFAULT_FIELD_TYPE = 'CodeEditor';

// Public Static Properties
// =========================================================================
Expand Down Expand Up @@ -135,14 +135,14 @@ public function registerComponents(): void
public function registerEventHandlers(): void
{
// Base CP templates directory
Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $e) {
Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function(RegisterTemplateRootsEvent $e) {
if (is_dir($baseDir = $this->getBasePath() . DIRECTORY_SEPARATOR . 'templates')) {
$e->roots[$this->id] = $baseDir;
}
});
// Base Site templates directory
if (self::$settings->allowTemplateAccess) {
Event::on(View::class, View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $e) {
Event::on(View::class, View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS, function(RegisterTemplateRootsEvent $e) {
if (is_dir($baseDir = $this->getBasePath() . DIRECTORY_SEPARATOR . 'templates')) {
$e->roots[$this->id] = $baseDir;
}
Expand Down
2 changes: 1 addition & 1 deletion src/assetbundles/codeeditor/CodeEditorAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function init()
$this->js = [
'js/runtime.js',
'js/vendors.js',
'js/code-editor.js'
'js/code-editor.js',
];

parent::init();
Expand Down
6 changes: 3 additions & 3 deletions src/autocompletes/CraftApiAutocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class CraftApiAutocomplete extends ObjectParserAutocomplete
// Constants
// =========================================================================

const ELEMENT_ROUTE_EXCLUDES = [
public const ELEMENT_ROUTE_EXCLUDES = [
'matrixblock',
'globalset'
'globalset',
];

// Public Properties
Expand All @@ -46,7 +46,7 @@ class CraftApiAutocomplete extends ObjectParserAutocomplete
public $type = AutocompleteTypes::TwigExpressionAutocomplete;

/**
* @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties
* @var bool Whether the autocomplete should be parsed with . -delimited nested sub-properties
*/
public $hasSubProperties = true;

Expand Down
2 changes: 1 addition & 1 deletion src/autocompletes/EnvironmentVariableAutocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class EnvironmentVariableAutocomplete extends Autocomplete
public $type = AutocompleteTypes::GeneralAutocomplete;

/**
* @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties
* @var bool Whether the autocomplete should be parsed with . -delimited nested sub-properties
*/
public $hasSubProperties = false;

Expand Down
2 changes: 1 addition & 1 deletion src/autocompletes/ObjectAutocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ObjectAutocomplete extends ObjectParserAutocomplete
public $type = AutocompleteTypes::TwigExpressionAutocomplete;

/**
* @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties
* @var bool Whether the autocomplete should be parsed with . -delimited nested sub-properties
*/
public $hasSubProperties = true;

Expand Down
19 changes: 13 additions & 6 deletions src/autocompletes/SectionShorthandFieldsAutocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SectionShorthandFieldsAutocomplete extends ObjectParserAutocomplete
'type' => "the entry type",
'section' => "the entry’s section",
'author' => "the entry’s author",
]
],
];

// Public Properties
Expand All @@ -57,7 +57,7 @@ class SectionShorthandFieldsAutocomplete extends ObjectParserAutocomplete
public $type = AutocompleteTypes::TwigExpressionAutocomplete;

/**
* @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties
* @var bool Whether the autocomplete should be parsed with . -delimited nested sub-properties
*/
public $hasSubProperties = true;

Expand Down Expand Up @@ -118,15 +118,22 @@ public function generateCompleteItems(): void

return;
}
$sections = null;
// getSections() is used for Craft 3 & 4
if (method_exists(Craft::$app, 'getSections')) {
$sections = Craft::$app->getSections();
}
// getEntries() is used for Craft 5
if (method_exists(Craft::$app, 'getEntries')) {
$sections = Craft::$app->getEntries();
}
// Find the entry types used in the passed in sectionId
$sections = Craft::$app->getSections();
$section = $sections->getSectionById($this->sectionId);
if ($section) {
if ($sections && $section = $sections->getSectionById($this->sectionId)) {
$entryTypes = $section->getEntryTypes();
foreach ($entryTypes as $entryType) {
// Add the native fields in
if ($entryType->elementType) {
$element = new $entryType->elementType;
$element = new $entryType->elementType();
/* @var ElementInterface $element */
$this->parseObject('', $element, 0);
$this->addMagicGetterProperties($element);
Expand Down
16 changes: 8 additions & 8 deletions src/autocompletes/TwigLanguageAutocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class TwigLanguageAutocomplete extends Autocomplete
// Constants
// =========================================================================

const CRAFT_FILTER_DOCS_URL = 'https://craftcms.com/docs/4.x/dev/filters.html';
const FILTER_DOCS = [
public const CRAFT_FILTER_DOCS_URL = 'https://craftcms.com/docs/4.x/dev/filters.html';
public const FILTER_DOCS = [
'abs' => '[abs](https://twig.symfony.com/doc/3.x/filters/abs.html) | Returns the absolute value of a number.',
'address' => '[address](#address) | Formats an address.',
'append' => '[append](#append) | Appends HTML to the end of another element.',
Expand Down Expand Up @@ -128,8 +128,8 @@ class TwigLanguageAutocomplete extends Autocomplete
'withoutKey' => '[withoutKey](#withoutkey) | Returns an array without the specified key.',
];

const CRAFT_FUNCTION_DOCS_URL = 'https://craftcms.com/docs/4.x/dev/functions.html';
const FUNCTION_DOCS = [
public const CRAFT_FUNCTION_DOCS_URL = 'https://craftcms.com/docs/4.x/dev/functions.html';
public const FUNCTION_DOCS = [
'actionInput' => '[actionInput](#actioninput) | Outputs a hidden `action` input.',
'actionUrl' => '[actionUrl](#actionurl) | Generates a controller action URL.',
'alias' => '[alias](#alias) | Parses a string as an alias.',
Expand Down Expand Up @@ -182,8 +182,8 @@ class TwigLanguageAutocomplete extends Autocomplete
'url' => '[url](#url) | Generates a URL.',
];

const CRAFT_TAG_DOCS_URL = 'https://craftcms.com/docs/4.x/dev/tags.html';
const TAG_DOCS = [
public const CRAFT_TAG_DOCS_URL = 'https://craftcms.com/docs/4.x/dev/tags.html';
public const TAG_DOCS = [
'apply' => '[apply](https://twig.symfony.com/doc/3.x/tags/apply.html) | Applies Twig filters to the nested template code.',
'autoescape' => '[autoescape](https://twig.symfony.com/doc/3.x/tags/autoescape.html) | Controls the escaping strategy for the nested template code.',
'block' => '[block](https://twig.symfony.com/doc/3.x/tags/block.html) | Defines a template block.',
Expand Down Expand Up @@ -227,7 +227,7 @@ class TwigLanguageAutocomplete extends Autocomplete
'with' => '[with](https://twig.symfony.com/doc/3.x/tags/with.html) | Creates a nested template scope.',
];

const ADDITIONAL_TAGS = [
public const ADDITIONAL_TAGS = [
'else',
'elseif',
'endblock',
Expand All @@ -248,7 +248,7 @@ class TwigLanguageAutocomplete extends Autocomplete
public $type = AutocompleteTypes::TwigExpressionAutocomplete;

/**
* @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties
* @var bool Whether the autocomplete should be parsed with . -delimited nested sub-properties
*/
public $hasSubProperties = false;

Expand Down
6 changes: 3 additions & 3 deletions src/base/Autocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class Autocomplete extends Model implements AutocompleteInterface
// Constants
// =========================================================================

const COMPLETION_KEY = '__completions';
public const COMPLETION_KEY = '__completions';

// Public Properties
// =========================================================================
Expand Down Expand Up @@ -88,9 +88,9 @@ public function addCompleteItem(CompleteItem $item, string $path = ''): void
$path = $item->label;
}
ArrayHelper::setValue($this->completeItems, $path, [
self::COMPLETION_KEY => array_filter($item->toArray(), static function ($v) {
self::COMPLETION_KEY => array_filter($item->toArray(), static function($v) {
return !is_null($v);
})
}),
]);
}

Expand Down
Loading

0 comments on commit d2d9c87

Please sign in to comment.