Skip to content

Commit

Permalink
LIB-662 initial scifree ckeditor integration
Browse files Browse the repository at this point in the history
  • Loading branch information
bricas committed Sep 19, 2024
1 parent b296fc5 commit 8e60316
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 0 deletions.
1 change: 1 addition & 0 deletions configuration/editor.editor.library_page_html.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ settings:
items:
- Wc-search
- eresources
- Scifree
plugins:
drupallink:
linkit_enabled: true
Expand Down
6 changes: 6 additions & 0 deletions configuration/filter.format.library_page_html.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,9 @@ filters:
footnotes_collapse: '0'
footnotes_html: '1'
footnotes_css: '1'
filter_guides_ckeditor_scifree:
id: filter_guides_ckeditor_scifree
provider: guides
status: true
weight: 0
settings: { }
3 changes: 3 additions & 0 deletions custom/modules/guides/css/ckeditor-scifree.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
div.scifree {
margin: 1em;
}
3 changes: 3 additions & 0 deletions custom/modules/guides/guides.module
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ function guides_theme($existing, $type, $theme, $path) {
'ckeditor-wc-search' => [
'variables' => [],
],
'ckeditor-scifree' => [
'variables' => [],
],
'ckeditor-eresources' => [
'variables' => [
'resources' => NULL,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions custom/modules/guides/js/plugins/scifree/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(function (CKEDITOR) {

'use strict';

CKEDITOR.plugins.add('scifree', {
requires: 'widget',
icons: 'scifree',

init: function(editor) {
CKEDITOR.document.appendStyleText('.cke_button__scifree_label {display: inline;}');

editor.widgets.add('scifree', {
button: 'SciFree',
template: '<div class="scifree">SciFree Search Widget</div>',
allowedContent: 'div(!scifree)',
requiredContent: 'div(scifree)',
upcast: function(element) {
return element.name == 'div' && element.hasClass('scifree');
},
});
}
});
})(CKEDITOR);
72 changes: 72 additions & 0 deletions custom/modules/guides/src/Plugin/CKEditorPlugin/SciFree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Drupal\guides\Plugin\CKEditorPlugin;

use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\ckeditor\CKEditorPluginCssInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\editor\Entity\Editor;

/**
* Defines the "SciFree" plugin.
*
* @CKEditorPlugin(
* id = "scifree",
* label = @Translation("SciFree"),
* module = "guides"
* )
*/
class SciFree extends CKEditorPluginBase implements CKEditorPluginCssInterface {

use StringTranslationTrait;

/**
* {@inheritdoc}
*/
public function getDependencies(Editor $editor) {
return [];
}

/**
* Implements \Drupal\ckeditor\Plugin\CKEditorPluginInterface::isInternal().
*/
public function isInternal() {
return FALSE;
}

/**
* {@inheritdoc}
*/
public function getFile() {
return \Drupal::service('extension.list.module')->getPath('guides') . '/js/plugins/scifree/plugin.js';
}

/**
* {@inheritdoc}
*/
public function getButtons() {
return [
'Scifree' => [
'label' => $this->t('SciFree'),
'image' => \Drupal::service('extension.list.module')->getPath('guides') . '/js/plugins/scifree/icons/scifree.png',
],
];
}

/**
* {@inheritdoc}
*/
public function getConfig(Editor $editor) {
return [];
}

/**
* {@inheritdoc}
*/
public function getCssFiles(Editor $editor) {
return [
\Drupal::service('extension.list.module')->getPath('guides') . '/css/ckeditor-scifree.css',
];
}

}
82 changes: 82 additions & 0 deletions custom/modules/guides/src/Plugin/Filter/FilterCKEditorSciFree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Drupal\guides\Plugin\Filter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides a filter to convert sci free tags to a full widget.
*
* @Filter(
* id = "filter_guides_ckeditor_scifree",
* title = @Translation("Replace SciFree placeholder with the full widget"),
* type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE
* )
*/
class FilterCKEditorSciFree extends FilterBase implements ContainerFactoryPluginInterface {


/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;

/**
* Class constructor.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, RendererInterface $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->renderer = $renderer;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('renderer')
);
}

/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
return [];
}

/**
* {@inheritdoc}
*/
public function process($text, $langcode) {
$result = new FilterProcessResult($text);

if (strpos($text, '<div class="scifree"') !== FALSE) {
$render = [
'#theme' => 'ckeditor-scifree',
];
$widget = $this->renderer->render($render);
$text = str_replace('<div class="scifree">SciFree Search Widget</div>', $widget, $text);
$result->setProcessedText($text);
}
return $result;
}

/**
* {@inheritdoc}
*/
public function tips($long = FALSE) {
return $this->t('Replace SciFree placeholder with the full widget');
}

}
2 changes: 2 additions & 0 deletions custom/modules/guides/templates/ckeditor-scifree.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div id="scfwid" data-account="unb"></div>
<script src="https://search.scifree.se/assets/external/widget.js"></script>

0 comments on commit 8e60316

Please sign in to comment.