Source of file ProseEditorField.php
Size: 7,974 Bytes - Last Modified: 2021-12-23T10:50:06+00:00
/var/www/docs.ssmods.com/process/src/src/ProseEditorField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 | <?php namespace Symbiote\Prose; use SilverStripe\Forms\FormField; use SilverStripe\View\Requirements; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Assets\File; use SilverStripe\Core\Manifest\ModuleResourceLoader; use SilverStripe\ORM\DataObject; use SilverStripe\Core\Convert; use SilverStripe\Assets\Shortcodes\ImageShortcodeProvider; class ProseEditorField extends FormField { private static $allowed_actions = [ 'childnodes', ]; private static $type_map = [ 'page' => SiteTree::class, 'file' => File::class, ]; protected $uploadPath = 'Uploads'; protected $settings = [ 'menu' => [ 'insertlink' => true, 'insertimage' => true, 'bulletlist' => true, 'orderedlist' => true, 'quote' => true, 'paragraph' => true, 'pre' => true, 'hr' => true, 'table' => true, 'shortcode' => true, 'viewsource' => true, ], 'linkSelector' => [ 'internal' => true ] ]; /** * is this editor attached to a particular object? */ protected $contextId = 0; protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_TEXT; public function __construct($name, $title = null, $value = null) { parent::__construct($name, $title, $value); // include TinyMCE Javascript Requirements::css('symbiote/silverstripe-prose-editor: editor/dist/main.css'); Requirements::javascript('symbiote/silverstripe-prose-editor: editor/dist/main.js'); } public function setContext(DataObject $object) { // expects a link and ID to be available. if ($object->hasMethod('RelativeLink')) { $this->setUploadPath($object->RelativeLink()); } $this->setContextId($object->ID); return $this; } public function setUploadPath($v) { $this->uploadPath = $v; return $this; } public function getUploadPath() { return $this->uploadPath; } public function setContextId($v) { $this->contextId = $v; return $this; } public function getContextId() { return $this->contextId; } public function enableSetting($type, $name) { $this->settings[$type][$name] = true; return $this; } public function disableSetting($type, $name) { $this->settings[$type][$name] = false; return $this; } public function enableMenuItem($name) { $this->settings['menu'][$name] = true; return $this; } public function disableMenuItem($name) { $this->settings['menu'][$name] = false; return $this; } /** * {@inheritdoc} */ public function getAttributes() { $attributes = array_merge( parent::getAttributes(), array( 'class' => 'ProseEditorField', 'data-prose-url' => '__prose', 'data-upload-path' => $this->uploadPath, 'data-context-id' => $this->contextId, 'data-prose-config' => \json_encode($this->settings), 'value' => null, 'type' => null, ) ); return $attributes; } /** * Return value with all values encoded in html entities * * @return string Raw HTML */ public function ValueEntities() { return htmlentities($this->Value(), ENT_COMPAT, 'UTF-8'); } /** * Request nodes from the server * * @param SS_HTTPRequest $request * @return JSONString */ public function childnodes($request) { $data = array(); $searchType = 'page'; if ($request->param('ID')) { $searchType = $request->param('ID'); } $rootObjectType = $this->config()->type_map[$searchType]; if ($request->getVar('search')) { return $this->performSearch($request->getVar('search'), $rootObjectType); } $parentId = (int)$request->getVar('id'); if (!$parentId || $parentId == '#') { $parentId = 0; } $selectable = null; if ($request->param('OtherID')) { $selectable = explode(',', $request->param('OtherID')); } $type = $rootObjectType; $id = $parentId; if (!$type || $id < 0) { $data = array(0 => array('data' => 'An error has occurred')); } else { $children = null; if ($id === 0) { $children = DataObject::get($rootObjectType)->filter('ParentID', 0); } else { $object = DataObject::get_by_id($type, $id); $children = $this->childrenOfNode($object); } $data = array(); if ($children && count($children)) { foreach ($children as $child) { if ($child->ID < 0) { continue; } $haskids = $child->numChildren() > 0; $nodeData = [ 'text' => isset($child->MenuTitle) ? $child->MenuTitle : $child->Title, 'id' => $child->ID, ]; if ($selectable && !in_array($child->ClassName, $selectable)) { $nodeData['clickable'] = false; } $thumbs = null; if ($child->ClassName == Image::class) { $thumbs = $this->generateThumbnails($child); $nodeData['icon'] = $thumbs['x32']; } else if (!$haskids) { // $nodeData['icon'] = ModuleResourceLoader::singleton()->resolvePath('symbiote/silverstripe-frontend-authoring: client/images/page.png'); $nodeData['icon'] = 'resources/symbiote/silverstripe-frontend-authoring/client/images/page.png'; } $nodeData['children'] = $haskids; $nodeData['data'] = [ 'link' => $child instanceof File ? $child->getURL() : $child->RelativeLink() ]; // $nodeEntry = array( // 'attributes' => array('id' => $child->ClassName . '-' . $child->ID, 'text' => Convert::raw2att($nodeData['text']), 'link' => $child->RelativeLink()), // 'data' => $nodeData, // 'state' => $haskids ? 'closed' : 'open' // ); // if ($thumbs) { // $nodeEntry['thumbs'] = $thumbs; // } $data[] = $nodeData; } } } $this->getResponse()->addHeader('Content-Type', 'application/json'); return Convert::raw2json($data); } /** * Called to generate thumbnails before sending the image data back * * @param Image $image */ protected function generateThumbnails(Image $image) { $thumbs = array(); $thumbs['x16'] = $image->Fit(16, 16)->Link(); $thumbs['x32'] = $image->Fit(32, 32)->Link(); $thumbs['x128'] = $image->Fit(128, 128)->Link(); return $thumbs; } /** * Method to work around bug where Hierarchy.php directly refers to * ShowInMenus, which is only available on SiteTree * * @param DataObject $node * @return DataList */ protected function childrenOfNode($node) { $result = $node->stageChildren(true); if (isset($result)) { foreach ($result as $child) { if (!$child->canView()) { $result->remove($child); } } } return $result; } } |