Skip to content

Commit

Permalink
Merge branch 'release/4.0.11' into v4
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Oct 10, 2023
2 parents b0366ae + db2a443 commit 5149298
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## 4.0.11 - 2023.10.09
### Changed
* Ensure that the code field is rendered as formatted (but read-only) code when viewing a revision ([#12](https://github.com/nystudio107/craft-code-field/issues/12))

## 4.0.10 - 2023.08.06
### Changed
* Clean up `IEditorOptionsSchema.json` to handle `<Record>` types and refactor `GoToLocationValues` to `$def`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nystudio107/craft-code-field",
"description": "Provides a Code Field that has a full-featured code editor with syntax highlighting & autocomplete",
"type": "craft-plugin",
"version": "4.0.10",
"version": "4.0.11",
"keywords": [
"craft",
"cms",
Expand Down
98 changes: 66 additions & 32 deletions src/fields/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public static function displayName(): string
// Public Methods
// =========================================================================


/**
* @inheritdoc
*/
Expand Down Expand Up @@ -198,41 +197,24 @@ public function getSettingsHtml(): ?string
*/
public function getInputHtml($value, ElementInterface $element = null): string
{
// Get our id and namespace
$id = Html::id($this->handle);
$namespacedId = Craft::$app->getView()->namespaceInputId($id);
$twigVariables = $this->getFieldRenderVariables($value, $element, true);
// Render the input template
return Craft::$app->getView()->renderTemplate(
'codefield/_components/fields/Code_input',
$twigVariables
);
}

// Extract just the languages that have been selected for display
$displayLanguages = [];
if ($this->showLanguageDropdown) {
$monacoLanguages = require(__DIR__ . '/MonacoLanguages.php');
$decomposedLanguages = array_column($monacoLanguages, 'label', 'value');
$displayLanguages = array_intersect_key($decomposedLanguages, array_flip($this->availableLanguages));
// Handle "all" checkbox
if ($this->availableLanguages[0] === '*') {
$displayLanguages = $decomposedLanguages;
}
$displayLanguages = array_map(static function ($k, $v) {
return ['value' => $k, 'label' => $v];
}, array_keys($displayLanguages), array_values($displayLanguages));
}
$monacoOptionsOverride = Json::decodeIfJson($this->monacoEditorOptions);
if ($monacoOptionsOverride === null || is_string($monacoOptionsOverride)) {
$monacoOptionsOverride = [];
}
/**
* @inheritdoc
*/
public function getStaticHtml(mixed $value, ElementInterface $element): string
{
$twigVariables = $this->getFieldRenderVariables($value, $element, false);
// Render the input template
return Craft::$app->getView()->renderTemplate(
'codefield/_components/fields/Code_input',
[
'name' => $this->handle,
'value' => $value,
'field' => $this,
'orientation' => $this->getOrientation($element),
'id' => $id,
'namespacedId' => $namespacedId,
'displayLanguages' => $displayLanguages,
'monacoOptionsOverride' => $monacoOptionsOverride,
]
$twigVariables
);
}

Expand Down Expand Up @@ -282,4 +264,56 @@ public function getContentColumnType(): string

return Schema::TYPE_TEXT;
}

// Protected Methods
// =========================================================================

/**
* Return the Twig variables for rendering the field
*
* @param $value
* @param ElementInterface $element
* @param bool $enabled Whether the field is enabled or not
* @return array[]
*/
protected function getFieldRenderVariables($value, ElementInterface $element, bool $enabled): array
{
// Get our id and namespace
$id = Html::id($this->handle);
$namespacedId = Craft::$app->getView()->namespaceInputId($id);

// Extract just the languages that have been selected for display
$displayLanguages = [];
if ($this->showLanguageDropdown) {
$monacoLanguages = require(__DIR__ . '/MonacoLanguages.php');
$decomposedLanguages = array_column($monacoLanguages, 'label', 'value');
$displayLanguages = array_intersect_key($decomposedLanguages, array_flip($this->availableLanguages));
// Handle "all" checkbox
if ($this->availableLanguages[0] === '*') {
$displayLanguages = $decomposedLanguages;
}
$displayLanguages = array_map(static function ($k, $v) {
return ['value' => $k, 'label' => $v];
}, array_keys($displayLanguages), array_values($displayLanguages));
}
$monacoOptionsOverride = Json::decodeIfJson($this->monacoEditorOptions);
if ($monacoOptionsOverride === null || is_string($monacoOptionsOverride)) {
$monacoOptionsOverride = [];
}
// Disable the Monaco editor
if (!$enabled) {
$monacoOptionsOverride['domReadOnly'] = true;
$monacoOptionsOverride['readOnly'] = true;
}
return [
'name' => $this->handle,
'value' => $value,
'field' => $this,
'orientation' => $this->getOrientation($element),
'id' => $id,
'namespacedId' => $namespacedId,
'displayLanguages' => $displayLanguages,
'monacoOptionsOverride' => $monacoOptionsOverride,
];
}
}

0 comments on commit 5149298

Please sign in to comment.