From 21bf47a4f48f2b60a4d632f7942a4c88411c70ec Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Mon, 25 Nov 2024 17:30:45 +1300 Subject: [PATCH] API Use dynamic config for ModalController forms --- _config/config.yml | 7 ++- client/dist/js/TinyMCE_sslink-anchor.js | 2 +- client/dist/js/TinyMCE_sslink-internal.js | 2 +- client/src/legacy/TinyMCE_sslink-anchor.js | 2 +- client/src/legacy/TinyMCE_sslink-internal.js | 2 +- code/Controllers/CMSPageEditController.php | 8 --- code/Forms/AnchorLinkFormFactory.php | 4 +- code/Forms/InternalLinkModalExtension.php | 62 -------------------- 8 files changed, 10 insertions(+), 79 deletions(-) delete mode 100644 code/Forms/InternalLinkModalExtension.php diff --git a/_config/config.yml b/_config/config.yml index 47a7621559..a179cf9e0e 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -9,11 +9,12 @@ SilverStripe\Admin\LeftAndMain: Name: cmsmodals --- SilverStripe\Admin\ModalController: - extensions: - - SilverStripe\CMS\Forms\InternalLinkModalExtension + link_modal_form_factories: + editorInternalLink: SilverStripe\CMS\Forms\InternalLinkFormFactory + editorAnchorLink: SilverStripe\CMS\Forms\AnchorLinkFormFactory --- Name: cmshierarchy --- SilverStripe\ORM\Hierarchy\Hierarchy: node_threshold_total: 50 - node_threshold_leaf: 250 \ No newline at end of file + node_threshold_leaf: 250 diff --git a/client/dist/js/TinyMCE_sslink-anchor.js b/client/dist/js/TinyMCE_sslink-anchor.js index 767ec55e02..9e7d431d73 100644 --- a/client/dist/js/TinyMCE_sslink-anchor.js +++ b/client/dist/js/TinyMCE_sslink-anchor.js @@ -1 +1 @@ -!function(){"use strict";var e={803:function(e,t){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;t.default={ANCHORSELECTOR_CURRENT_FIELD:"ANCHORSELECTOR_CURRENT_FIELD",ANCHORSELECTOR_UPDATED:"ANCHORSELECTOR_UPDATED",ANCHORSELECTOR_UPDATING:"ANCHORSELECTOR_UPDATING",ANCHORSELECTOR_UPDATE_FAILED:"ANCHORSELECTOR_UPDATE_FAILED"}},979:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.beginUpdating=function(e){return{type:i.default.ANCHORSELECTOR_UPDATING,payload:{pageId:e}}},t.updateFailed=function(e){return{type:i.default.ANCHORSELECTOR_UPDATE_FAILED,payload:{pageId:e}}},t.updated=function(e,t){let n=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return{type:i.default.ANCHORSELECTOR_UPDATED,payload:{pageId:e,anchors:t,cacheResult:n}}},t.updatedCurrentField=function(e,t,n){return{type:i.default.ANCHORSELECTOR_CURRENT_FIELD,payload:{pageId:e,anchors:t,fieldID:n}}};var r,i=(r=n(803))&&r.__esModule?r:{default:r}},207:function(e){e.exports=Injector},127:function(e){e.exports=InsertLinkModal},594:function(e){e.exports=React},145:function(e){e.exports=ReactDomClient},40:function(e){e.exports=ReactRedux},502:function(e){e.exports=ShortcodeSerialiser},153:function(e){e.exports=TinyMCEActionRegistrar},815:function(e){e.exports=i18n},669:function(e){e.exports=jQuery}},t={};function n(r){var i=t[r];if(void 0!==i)return i.exports;var o=t[r]={exports:{}};return e[r](o,o.exports,n),o.exports}!function(){var e=c(n(815)),t=c(n(153)),r=c(n(594)),i=n(145),o=n(40),a=c(n(669)),d=c(n(502)),l=n(127),s=n(207),u=n(979);function c(e){return e&&e.__esModule?e:{default:e}}const p="sslinkanchor",f={init(n){t.default.addAction("sslink",{text:e.default._t("CMS.LINKLABEL_ANCHOR","Anchor on a page"),onAction:e=>e.execCommand(p),priority:60},n.getParam("editorIdentifier")).addCommandWithUrlTest(p,/^\[sitetree_link.+]#[^#\]]+$/),n.addCommand(p,(()=>{const e=(0,a.default)(`#${n.id}`).entwine("ss"),t=Number((0,a.default)("#Form_EditForm_ID").val()||0),r=(0,a.default)(n.getBody()).find("[id],[name]").toArray().map((e=>e.id||e.name));ss.store.dispatch((0,u.updatedCurrentField)(t,r,n.id)),e.openLinkAnchorDialog()}))}},E="insert-link__dialog-wrapper--anchor",R=(0,s.provideInjector)((0,l.createInsertLinkModal)("SilverStripe\\CMS\\Controllers\\CMSPageEditController","editorAnchorLink"));a.default.entwine("ss",(t=>{t("textarea.htmleditor").entwine({openLinkAnchorDialog(){let e=t(`#${E}`);e.length||(e=t(`
`),t("body").append(e)),e.addClass("insert-link__dialog-wrapper"),e.setElement(this),e.open()}}),t(`#${E}`).entwine({ReactRoot:null,renderModal(n){var a=this;const d=ss.store,l=this.getOriginalAttributes(),s=this.getRequireLinkText(),u=Number(t("#Form_EditForm_ID").val()||0);let c=this.getReactRoot();c||(c=(0,i.createRoot)(this[0]),this.setReactRoot(c)),c.render(r.default.createElement(o.Provider,{store:d},r.default.createElement(R,{isOpen:n,onInsert:function(){return a.handleInsert(...arguments)},onClosed:()=>this.close(),title:e.default._t("CMS.LINK_ANCHOR","Link to an anchor on a page"),bodyClassName:"modal__dialog",className:"insert-link__dialog-wrapper--anchor",fileAttributes:l,identifier:"Admin.InsertLinkAnchorModal",requireLinkText:s,currentPageID:u})))},buildAttributes(e){return{href:`${d.default.serialise({name:"sitetree_link",properties:{id:e.PageID}},!0)}${e.Anchor&&e.Anchor.length?`#${e.Anchor}`:""}`,target:e.TargetBlank?"_blank":"",title:e.Description}},getOriginalAttributes(){const e=this.getElement().getEditor(),n=t(e.getSelectedNode()),r=(n.attr("href")||"").split("#");if(!r[0])return{};const i=d.default.match("sitetree_link",!1,r[0]);return i?{PageID:i.properties.id?parseInt(i.properties.id,10):0,Anchor:r[1]||"",Description:n.attr("title"),TargetBlank:!!n.attr("target")}:{}}})})),tinymce.PluginManager.add(p,(e=>f.init(e)))}()}(); \ No newline at end of file +!function(){"use strict";var e={803:function(e,t){Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;t.default={ANCHORSELECTOR_CURRENT_FIELD:"ANCHORSELECTOR_CURRENT_FIELD",ANCHORSELECTOR_UPDATED:"ANCHORSELECTOR_UPDATED",ANCHORSELECTOR_UPDATING:"ANCHORSELECTOR_UPDATING",ANCHORSELECTOR_UPDATE_FAILED:"ANCHORSELECTOR_UPDATE_FAILED"}},979:function(e,t,n){Object.defineProperty(t,"__esModule",{value:!0}),t.beginUpdating=function(e){return{type:i.default.ANCHORSELECTOR_UPDATING,payload:{pageId:e}}},t.updateFailed=function(e){return{type:i.default.ANCHORSELECTOR_UPDATE_FAILED,payload:{pageId:e}}},t.updated=function(e,t){let n=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return{type:i.default.ANCHORSELECTOR_UPDATED,payload:{pageId:e,anchors:t,cacheResult:n}}},t.updatedCurrentField=function(e,t,n){return{type:i.default.ANCHORSELECTOR_CURRENT_FIELD,payload:{pageId:e,anchors:t,fieldID:n}}};var r,i=(r=n(803))&&r.__esModule?r:{default:r}},207:function(e){e.exports=Injector},127:function(e){e.exports=InsertLinkModal},594:function(e){e.exports=React},145:function(e){e.exports=ReactDomClient},40:function(e){e.exports=ReactRedux},502:function(e){e.exports=ShortcodeSerialiser},153:function(e){e.exports=TinyMCEActionRegistrar},815:function(e){e.exports=i18n},669:function(e){e.exports=jQuery}},t={};function n(r){var i=t[r];if(void 0!==i)return i.exports;var a=t[r]={exports:{}};return e[r](a,a.exports,n),a.exports}!function(){var e=c(n(815)),t=c(n(153)),r=c(n(594)),i=n(145),a=n(40),o=c(n(669)),d=c(n(502)),l=n(127),s=n(207),u=n(979);function c(e){return e&&e.__esModule?e:{default:e}}const p="sslinkanchor",f={init(n){t.default.addAction("sslink",{text:e.default._t("CMS.LINKLABEL_ANCHOR","Anchor on a page"),onAction:e=>e.execCommand(p),priority:60},n.getParam("editorIdentifier")).addCommandWithUrlTest(p,/^\[sitetree_link.+]#[^#\]]+$/),n.addCommand(p,(()=>{const e=(0,o.default)(`#${n.id}`).entwine("ss"),t=Number((0,o.default)("#Form_EditForm_ID").val()||0),r=(0,o.default)(n.getBody()).find("[id],[name]").toArray().map((e=>e.id||e.name));ss.store.dispatch((0,u.updatedCurrentField)(t,r,n.id)),e.openLinkAnchorDialog()}))}},E="insert-link__dialog-wrapper--anchor",A=(0,s.provideInjector)((0,l.createInsertLinkModal)("SilverStripe\\Admin\\LeftAndMain","editorAnchorLink"));o.default.entwine("ss",(t=>{t("textarea.htmleditor").entwine({openLinkAnchorDialog(){let e=t(`#${E}`);e.length||(e=t(`
`),t("body").append(e)),e.addClass("insert-link__dialog-wrapper"),e.setElement(this),e.open()}}),t(`#${E}`).entwine({ReactRoot:null,renderModal(n){var o=this;const d=ss.store,l=this.getOriginalAttributes(),s=this.getRequireLinkText(),u=Number(t("#Form_EditForm_ID").val()||0);let c=this.getReactRoot();c||(c=(0,i.createRoot)(this[0]),this.setReactRoot(c)),c.render(r.default.createElement(a.Provider,{store:d},r.default.createElement(A,{isOpen:n,onInsert:function(){return o.handleInsert(...arguments)},onClosed:()=>this.close(),title:e.default._t("CMS.LINK_ANCHOR","Link to an anchor on a page"),bodyClassName:"modal__dialog",className:"insert-link__dialog-wrapper--anchor",fileAttributes:l,identifier:"Admin.InsertLinkAnchorModal",requireLinkText:s,currentPageID:u})))},buildAttributes(e){return{href:`${d.default.serialise({name:"sitetree_link",properties:{id:e.PageID}},!0)}${e.Anchor&&e.Anchor.length?`#${e.Anchor}`:""}`,target:e.TargetBlank?"_blank":"",title:e.Description}},getOriginalAttributes(){const e=this.getElement().getEditor(),n=t(e.getSelectedNode()),r=(n.attr("href")||"").split("#");if(!r[0])return{};const i=d.default.match("sitetree_link",!1,r[0]);return i?{PageID:i.properties.id?parseInt(i.properties.id,10):0,Anchor:r[1]||"",Description:n.attr("title"),TargetBlank:!!n.attr("target")}:{}}})})),tinymce.PluginManager.add(p,(e=>f.init(e)))}()}(); \ No newline at end of file diff --git a/client/dist/js/TinyMCE_sslink-internal.js b/client/dist/js/TinyMCE_sslink-internal.js index 7c345556b4..55c9066f40 100644 --- a/client/dist/js/TinyMCE_sslink-internal.js +++ b/client/dist/js/TinyMCE_sslink-internal.js @@ -1 +1 @@ -!function(){"use strict";var t={207:function(t){t.exports=Injector},127:function(t){t.exports=InsertLinkModal},594:function(t){t.exports=React},145:function(t){t.exports=ReactDomClient},40:function(t){t.exports=ReactRedux},502:function(t){t.exports=ShortcodeSerialiser},153:function(t){t.exports=TinyMCEActionRegistrar},815:function(t){t.exports=i18n},669:function(t){t.exports=jQuery}},e={};function n(i){var r=e[i];if(void 0!==r)return r.exports;var o=e[i]={exports:{}};return t[i](o,o.exports,n),o.exports}!function(){var t=c(n(815)),e=c(n(153)),i=c(n(594)),r=n(145),o=n(40),a=c(n(669)),s=c(n(502)),l=n(127),d=n(207);function c(t){return t&&t.__esModule?t:{default:t}}const u="sslinkinternal",p={init(n){e.default.addAction("sslink",{text:t.default._t("CMS.LINKLABEL_PAGE","Page on this site"),onAction:t=>t.execCommand(u),priority:90},n.getParam("editorIdentifier")).addCommandWithUrlTest(u,/^\[sitetree_link.+]$/),n.addCommand(u,(()=>{(0,a.default)(`#${n.id}`).entwine("ss").openLinkInternalDialog()}))}},f="insert-link__dialog-wrapper--internal",g=(0,d.provideInjector)((0,l.createInsertLinkModal)("SilverStripe\\CMS\\Controllers\\CMSPageEditController","editorInternalLink"));a.default.entwine("ss",(e=>{e("textarea.htmleditor").entwine({openLinkInternalDialog(){let t=e(`#${f}`);t.length||(t=e(`
`),e("body").append(t)),t.addClass("insert-link__dialog-wrapper"),t.setElement(this),t.open()}}),e(`#${f}`).entwine({ReactRoot:null,renderModal(e){var n=this;const a=ss.store,s=this.getOriginalAttributes(),l=this.getRequireLinkText();let d=this.getReactRoot();d||(d=(0,r.createRoot)(this[0]),this.setReactRoot(d)),d.render(i.default.createElement(o.Provider,{store:a},i.default.createElement(g,{isOpen:e,onInsert:function(){return n.handleInsert(...arguments)},onClosed:()=>this.close(),title:t.default._t("CMS.LINK_PAGE","Link to a page"),bodyClassName:"modal__dialog",className:"insert-link__dialog-wrapper--internal",fileAttributes:s,identifier:"Admin.InsertLinkInternalModal",requireLinkText:l})))},buildAttributes(t){return{href:`${s.default.serialise({name:"sitetree_link",properties:{id:t.PageID}},!0)}${t.Anchor&&t.Anchor.length?`#${t.Anchor}`:""}`,target:t.TargetBlank?"_blank":"",title:t.Description}},getOriginalAttributes(){const t=this.getElement().getEditor(),n=e(t.getSelectedNode()),i=(n.attr("href")||"").split("#");if(!i[0])return{};const r=s.default.match("sitetree_link",!1,i[0]);return r?{PageID:r.properties.id?parseInt(r.properties.id,10):0,Anchor:i[1]||"",Description:n.attr("title"),TargetBlank:!!n.attr("target")}:{}}})})),tinymce.PluginManager.add(u,(t=>p.init(t)))}()}(); \ No newline at end of file +!function(){"use strict";var t={207:function(t){t.exports=Injector},127:function(t){t.exports=InsertLinkModal},594:function(t){t.exports=React},145:function(t){t.exports=ReactDomClient},40:function(t){t.exports=ReactRedux},502:function(t){t.exports=ShortcodeSerialiser},153:function(t){t.exports=TinyMCEActionRegistrar},815:function(t){t.exports=i18n},669:function(t){t.exports=jQuery}},e={};function n(i){var r=e[i];if(void 0!==r)return r.exports;var o=e[i]={exports:{}};return t[i](o,o.exports,n),o.exports}!function(){var t=c(n(815)),e=c(n(153)),i=c(n(594)),r=n(145),o=n(40),a=c(n(669)),s=c(n(502)),l=n(127),d=n(207);function c(t){return t&&t.__esModule?t:{default:t}}const u="sslinkinternal",p={init(n){e.default.addAction("sslink",{text:t.default._t("CMS.LINKLABEL_PAGE","Page on this site"),onAction:t=>t.execCommand(u),priority:90},n.getParam("editorIdentifier")).addCommandWithUrlTest(u,/^\[sitetree_link.+]$/),n.addCommand(u,(()=>{(0,a.default)(`#${n.id}`).entwine("ss").openLinkInternalDialog()}))}},f="insert-link__dialog-wrapper--internal",g=(0,d.provideInjector)((0,l.createInsertLinkModal)("SilverStripe\\Admin\\LeftAndMain","editorInternalLink"));a.default.entwine("ss",(e=>{e("textarea.htmleditor").entwine({openLinkInternalDialog(){let t=e(`#${f}`);t.length||(t=e(`
`),e("body").append(t)),t.addClass("insert-link__dialog-wrapper"),t.setElement(this),t.open()}}),e(`#${f}`).entwine({ReactRoot:null,renderModal(e){var n=this;const a=ss.store,s=this.getOriginalAttributes(),l=this.getRequireLinkText();let d=this.getReactRoot();d||(d=(0,r.createRoot)(this[0]),this.setReactRoot(d)),d.render(i.default.createElement(o.Provider,{store:a},i.default.createElement(g,{isOpen:e,onInsert:function(){return n.handleInsert(...arguments)},onClosed:()=>this.close(),title:t.default._t("CMS.LINK_PAGE","Link to a page"),bodyClassName:"modal__dialog",className:"insert-link__dialog-wrapper--internal",fileAttributes:s,identifier:"Admin.InsertLinkInternalModal",requireLinkText:l})))},buildAttributes(t){return{href:`${s.default.serialise({name:"sitetree_link",properties:{id:t.PageID}},!0)}${t.Anchor&&t.Anchor.length?`#${t.Anchor}`:""}`,target:t.TargetBlank?"_blank":"",title:t.Description}},getOriginalAttributes(){const t=this.getElement().getEditor(),n=e(t.getSelectedNode()),i=(n.attr("href")||"").split("#");if(!i[0])return{};const r=s.default.match("sitetree_link",!1,i[0]);return r?{PageID:r.properties.id?parseInt(r.properties.id,10):0,Anchor:i[1]||"",Description:n.attr("title"),TargetBlank:!!n.attr("target")}:{}}})})),tinymce.PluginManager.add(u,(t=>p.init(t)))}()}(); \ No newline at end of file diff --git a/client/src/legacy/TinyMCE_sslink-anchor.js b/client/src/legacy/TinyMCE_sslink-anchor.js index a415666ed9..7eae3a7ade 100644 --- a/client/src/legacy/TinyMCE_sslink-anchor.js +++ b/client/src/legacy/TinyMCE_sslink-anchor.js @@ -44,7 +44,7 @@ const plugin = { }; const modalId = 'insert-link__dialog-wrapper--anchor'; -const sectionConfigKey = 'SilverStripe\\CMS\\Controllers\\CMSPageEditController'; +const sectionConfigKey = 'SilverStripe\\Admin\\LeftAndMain'; const formName = 'editorAnchorLink'; const InsertLinkInternalModal = provideInjector(createInsertLinkModal(sectionConfigKey, formName)); diff --git a/client/src/legacy/TinyMCE_sslink-internal.js b/client/src/legacy/TinyMCE_sslink-internal.js index 2b4887ba44..182755c683 100644 --- a/client/src/legacy/TinyMCE_sslink-internal.js +++ b/client/src/legacy/TinyMCE_sslink-internal.js @@ -36,7 +36,7 @@ const plugin = { }; const modalId = 'insert-link__dialog-wrapper--internal'; -const sectionConfigKey = 'SilverStripe\\CMS\\Controllers\\CMSPageEditController'; +const sectionConfigKey = 'SilverStripe\\Admin\\LeftAndMain'; const formName = 'editorInternalLink'; const InsertLinkInternalModal = provideInjector(createInsertLinkModal(sectionConfigKey, formName)); diff --git a/code/Controllers/CMSPageEditController.php b/code/Controllers/CMSPageEditController.php index fb9e95b96b..3a59b29a5e 100644 --- a/code/Controllers/CMSPageEditController.php +++ b/code/Controllers/CMSPageEditController.php @@ -4,7 +4,6 @@ use Page; use SilverStripe\Admin\LeftAndMain; -use SilverStripe\Admin\ModalController; use SilverStripe\CampaignAdmin\AddToCampaignHandler; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Control\Controller; @@ -37,18 +36,11 @@ class CMSPageEditController extends CMSMain public function getClientConfig(): array { - $modalController = ModalController::singleton(); return ArrayLib::array_merge_recursive(parent::getClientConfig(), [ 'form' => [ 'AddToCampaignForm' => [ 'schemaUrl' => $this->Link('schema/AddToCampaignForm'), ], - 'editorInternalLink' => [ - 'schemaUrl' => $modalController->Link('schema/editorInternalLink'), - ], - 'editorAnchorLink' => [ - 'schemaUrl' => $modalController->Link('schema/editorAnchorLink/:pageid'), - ], ], ]); } diff --git a/code/Forms/AnchorLinkFormFactory.php b/code/Forms/AnchorLinkFormFactory.php index 19e9dea82a..45179dc703 100644 --- a/code/Forms/AnchorLinkFormFactory.php +++ b/code/Forms/AnchorLinkFormFactory.php @@ -10,7 +10,7 @@ protected function getFormFields($controller, $name, $context) // Ensure current page is selected $pageIDField = $fields->dataFieldByName('PageID'); - $pageIDField->setValue((int)$context['PageID']); + $pageIDField->setValue((int)$context['ItemID']); // Get anchor selector field $fields->insertAfter( @@ -22,6 +22,6 @@ protected function getFormFields($controller, $name, $context) public function getRequiredContext() { - return array_merge(parent::getRequiredContext(), [ 'PageID' ]); + return array_merge(parent::getRequiredContext(), [ 'ItemID' ]); } } diff --git a/code/Forms/InternalLinkModalExtension.php b/code/Forms/InternalLinkModalExtension.php deleted file mode 100644 index cf3edd41da..0000000000 --- a/code/Forms/InternalLinkModalExtension.php +++ /dev/null @@ -1,62 +0,0 @@ - - */ -class InternalLinkModalExtension extends Extension -{ - private static $url_handlers = [ - 'editorAnchorLink/$ItemID' => 'editorAnchorLink', // Matches FormSchemaController::schema args - ]; - - private static $allowed_actions = [ - 'editorInternalLink', - 'editorAnchorLink', - ]; - - /** - * Form for inserting internal link pages - * - * @return Form - */ - public function editorInternalLink() - { - $showLinkText = $this->getOwner()->getRequest()->getVar('requireLinkText'); - $factory = InternalLinkFormFactory::singleton(); - return $factory->getForm( - $this->getOwner(), - "editorInternalLink", - [ 'RequireLinkText' => isset($showLinkText) ] - ); - } - - public function editorAnchorLink() - { - // Note: Should work both via schema() and as direct request - $request = $this->getOwner()->getRequest(); - $showLinkText = $request->getVar('requireLinkText'); - $pageID = $request->param('ItemID'); - $factory = AnchorLinkFormFactory::singleton(); - $form = $factory->getForm( - $this->getOwner(), - "editorAnchorLink", - [ 'RequireLinkText' => isset($showLinkText), 'PageID' => $pageID ] - ); - - // Set url handler that includes pageID - $form->setRequestHandler( - LeftAndMainFormRequestHandler::create($form, [$pageID]) - ); - - return $form; - } -}