From 1cb36a45f62cb71f3eaff9339a7284bb662a2056 Mon Sep 17 00:00:00 2001 From: tobimori Date: Wed, 10 May 2023 11:42:45 +0200 Subject: [PATCH] move to custom API route for better compatiblity --- composer.json | 2 +- config/api.php | 91 +++++++++++++++++++++++++++ config/sections.php | 10 +++ config/sections/heading-structure.php | 45 ------------- config/sections/seo-preview.php | 52 --------------- index.js | 2 +- index.php | 6 +- package.json | 2 +- src/sections/heading-structure.vue | 27 +++++--- src/sections/seo-preview.vue | 24 ++++--- vendor/composer/installed.php | 8 +-- 11 files changed, 141 insertions(+), 128 deletions(-) create mode 100644 config/api.php create mode 100644 config/sections.php delete mode 100644 config/sections/heading-structure.php delete mode 100644 config/sections/seo-preview.php diff --git a/composer.json b/composer.json index 9afe2ea..2180544 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "tobimori/kirby-seo", "description": "The ultimate Kirby SEO toolkit", "type": "kirby-plugin", - "version": "0.1.2", + "version": "0.1.3", "license": "MIT", "homepage": "https://github.com/tobimori/kirby-seo#readme", "authors": [ diff --git a/config/api.php b/config/api.php new file mode 100644 index 0000000..2ca05bd --- /dev/null +++ b/config/api.php @@ -0,0 +1,91 @@ + [ + 'dirtyPageOrSite' => function (string $slug) { + $kirby = kirby(); + $page = $slug == 'site' ? $kirby->site() : $kirby->page(Str::replace($slug, '+', '/')); + + if ($this->requestBody()) { + $form = Form::for($page, [ // Form class handles transformation of changed items + 'ignoreDisabled' => true, + 'input' => array_merge(['title' => $page->title()], $page->content()->data(), $this->requestBody()), + 'language' => $kirby->language()->code() + ]); + + $page = $page->clone(['content' => $form->data()]); + } + + return $page; + } + ], + 'routes' => [ + [ + 'pattern' => '/k-seo/(:any)/heading-structure', + 'method' => 'POST', + 'action' => function (string $slug) { + $model = $this->dirtyPageOrSite($slug); + + if ($model instanceof Page) { + $page = $model->render(); + $dom = new DOMDocument(); + $dom->loadHTML(htmlspecialchars_decode(iconv('UTF-8', 'ISO-8859-1', htmlentities($page, ENT_COMPAT, 'UTF-8')), ENT_QUOTES), libxml_use_internal_errors(true)); + + $xpath = new DOMXPath($dom); + $headings = $xpath->query('//h1|//h2|//h3|//h4|//h5|//h6'); + $data = []; + + foreach ($headings as $heading) { + $data[] = [ + 'level' => (int) str_replace('h', '', $heading->nodeName), + 'text' => $heading->textContent, + ]; + } + + return $data; + } + + return null; + } + ], + [ + 'pattern' => '/k-seo/(:any)/seo-preview', + 'method' => 'POST', + 'action' => function (string $slug) { + $model = $this->dirtyPageOrSite($slug); + + if ($model instanceof Site) { + $model = $model->homePage(); + } + + if ($model instanceof Page) { + $meta = $model->metadata(); + + $ogImage = $meta->ogImage()->toFile()?->thumb([ + 'width' => 1200, + 'height' => 630, + 'crop' => true, + ]); + + return [ + 'page' => $model->slug(), + 'url' => $model->url(), + 'title' => $meta->title()->value(), + 'description' => $meta->metaDescription()->value(), + 'ogTitle' => $meta->ogTitle()->value(), + 'ogDescription' => $meta->ogDescription()->value(), + 'ogImage' => $ogImage?->url(), + ]; + } + + return null; + } + ] + ] +]; diff --git a/config/sections.php b/config/sections.php new file mode 100644 index 0000000..8e73df8 --- /dev/null +++ b/config/sections.php @@ -0,0 +1,10 @@ + [ + 'mixins' => ['headline'], + ], + 'heading-structure' => [ + 'mixins' => ['headline'] + ] +]; diff --git a/config/sections/heading-structure.php b/config/sections/heading-structure.php deleted file mode 100644 index c92c17c..0000000 --- a/config/sections/heading-structure.php +++ /dev/null @@ -1,45 +0,0 @@ - ['headline'], - 'computed' => [ - 'value' => function () { - $model = $this->model(); - $kirby = $model->kirby(); - - if ($kirby->request()->query()->isNotEmpty()) { - $form = Form::for($model, [ // Form class handles transformation of changed items - 'ignoreDisabled' => true, - 'input' => array_merge(['title' => $model->title()], $model->content()->data(), kirby()->request()->query()->data()), - 'language' => $kirby->language()->code() - ]); - - $model = $model->clone(['content' => $form->data()]); - } - - if ($model instanceof Page) { - $page = $model->render(); - $dom = new DOMDocument(); - $dom->loadHTML(htmlspecialchars_decode(iconv('UTF-8', 'ISO-8859-1', htmlentities($page, ENT_COMPAT, 'UTF-8')), ENT_QUOTES), libxml_use_internal_errors(true)); - - $xpath = new DOMXPath($dom); - $headings = $xpath->query('//h1|//h2|//h3|//h4|//h5|//h6'); - $data = []; - - foreach ($headings as $heading) { - $data[] = [ - 'level' => (int) str_replace('h', '', $heading->nodeName), - 'text' => $heading->textContent, - ]; - } - - return $data; - } - - return null; - } - ] -]; diff --git a/config/sections/seo-preview.php b/config/sections/seo-preview.php deleted file mode 100644 index ad89fff..0000000 --- a/config/sections/seo-preview.php +++ /dev/null @@ -1,52 +0,0 @@ - ['headline'], - 'computed' => [ - 'value' => function () { - $model = $this->model(); - $kirby = $model->kirby(); - - if ($kirby->request()->query()->isNotEmpty()) { - $form = Form::for($model, [ // Form class handles transformation of changed items - 'ignoreDisabled' => true, - 'input' => array_merge(['title' => $model->title()], $model->content()->data(), kirby()->request()->query()->data()), - 'language' => $kirby->language()->code() - ]); - - $model = $model->clone(['content' => $form->data()]); - } - - if ($model instanceof Site) { - $model = $model->homePage(); // todo: show actual site fields instead of home page - } - - if ($model instanceof Page) { - $meta = $model->metadata(); - - $ogImage = $meta->ogImage()->toFile()?->thumb([ - 'width' => 1200, - 'height' => 630, - 'crop' => true, - ]); - - return [ - 'page' => $model->slug(), - 'url' => $model->url(), - 'title' => $meta->title()->value(), - 'description' => $meta->metaDescription()->value(), - 'ogTitle' => $meta->ogTitle()->value(), - 'ogDescription' => $meta->ogDescription()->value(), - 'ogImage' => $ogImage?->url(), - ]; - } - - - return null; - } - ] -]; diff --git a/index.js b/index.js index 7b682fc..f4088d6 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -(function(){"use strict";var k=function(){var e=this,r=e.$createElement,t=e._self._c||r;return e.value?t("div",{staticClass:"k-heading-structure"},[t("div",{staticClass:"k-heading-structure-label k-field-label"},[t("k-icon",{attrs:{type:"headline"}}),t("span",[e._v(e._s(e.label||e.$t("heading-structure")))]),e.isLoading?t("k-loader"):e._e()],1),t("k-box",{attrs:{theme:""}},[t("ol",{staticClass:"k-heading-structure-list"},e._l(e.value,function(s,i){return t("li",{key:i,class:"k-heading-structure-item level-"+s.level+" "+(e.itemInvalid(s,i)?"is-invalid":""),style:"z-index: "+(e.value.length-i)},[t("span",{staticClass:"k-heading-structure-item-level"},[e._v("H"+e._s(s.level))]),t("span",{staticClass:"k-heading-structure-item-text"},[e._v(e._s(s.text))])])}),0)]),e.incorrectOrder&&!e.noH1?t("k-box",{staticClass:"k-heading-structure-notice",attrs:{theme:"negative"}},[t("k-icon",{attrs:{type:"alert"}}),t("k-text",[e._v(e._s(e.$t("incorrect-heading-order")))])],1):e._e(),e.multipleH1?t("k-box",{staticClass:"k-heading-structure-notice",attrs:{theme:"negative"}},[t("k-icon",{attrs:{type:"alert"}}),t("k-text",[e._v(e._s(e.$t("multiple-h1-tags")))])],1):e._e(),e.noH1?t("k-box",{staticClass:"k-heading-structure-notice",attrs:{theme:"negative"}},[t("k-icon",{attrs:{type:"alert"}}),t("k-text",[e._v(e._s(e.$t("missing-h1-tag")))])],1):e._e()],1):e._e()},m=[],te="";function o(e,r,t,s,i,d,g,Y){var n=typeof e=="function"?e.options:e;r&&(n.render=r,n.staticRenderFns=t,n._compiled=!0),s&&(n.functional=!0),d&&(n._scopeId="data-v-"+d);var l;if(g?(l=function(a){a=a||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext,!a&&typeof __VUE_SSR_CONTEXT__!="undefined"&&(a=__VUE_SSR_CONTEXT__),i&&i.call(this,a),a&&a._registeredComponents&&a._registeredComponents.add(g)},n._ssrRegister=l):i&&(l=Y?function(){i.call(this,(n.functional?this.parent:this).$root.$options.shadowRoot)}:i),l)if(n.functional){n._injectStyles=l;var Z=n.render;n.render=function(ee,f){return l.call(f),Z(ee,f)}}else{var h=n.beforeCreate;n.beforeCreate=h?[].concat(h,l):[l]}return{exports:e,options:n}}const w={data(){return{label:null,value:null,isLoading:!0}},created(){this.handleLoad()},computed:{changes(){return this.$store.getters["content/changes"]()},incorrectOrder(){var e;return(e=this.value)==null?void 0:e.some((r,t)=>{var s,i;return r.level>((i=(s=this.value[t-1])==null?void 0:s.level)!=null?i:0)+1})},multipleH1(){var e;return((e=this.value)==null?void 0:e.filter(r=>r.level===1).length)>1},noH1(){var e;return((e=this.value)==null?void 0:e.filter(r=>r.level===1).length)===0}},methods:{async handleLoad(e){this.isLoading=!0;let r={};Object.entries(e!=null?e:this.changes).map(([s,i])=>{r[s]=encodeURIComponent(JSON.stringify(i))});const t=await this.$api.get(this.parent+"/sections/"+this.name,r);this.value=t.value,this.label=t.label,this.isLoading=!1},itemInvalid(e,r){var t,s;return!!(e.level>((s=(t=this.value[r-1])==null?void 0:t.level)!=null?s:0)+1||e.level===1&&this.value[r-1]||e.level===1&&this.value.filter(i=>i.level===1).length>1)}},watch:{changes(e){this.$helper.debounce(r=>this.handleLoad(r),200)(e)}}},_={};var b=o(w,k,m,!1,y,null,null,null);function y(e){for(let r in _)this[r]=_[r]}var C=function(){return b.exports}(),$=Object.freeze(Object.defineProperty({__proto__:null,default:C},Symbol.toStringTag,{value:"Module"})),S=function(){var e=this,r=e.$createElement,t=e._self._c||r;return t("div",{staticClass:"k-facebook-preview"},[t("div",{staticClass:"k-facebook-preview__image"},[t("img",{staticClass:"k-facebook-preview__img",attrs:{src:e.ogImage}})]),t("div",{staticClass:"k-facebook-preview__content"},[t("span",{staticClass:"k-facebook-preview__url"},[e._v(e._s(e.host))]),t("span",{staticClass:"k-facebook-preview__title"},[e._v(e._s(e.ogTitle))]),t("p",{staticClass:"k-facebook-preview__description"},[e._v(e._s(e.ogDescription))])])])},x=[],re="";const O={props:{ogTitle:String,url:String,ogDescription:String,ogImage:String},computed:{host(){return new URL(this.url).host}}},c={};var L=o(O,S,x,!1,T,null,null,null);function T(e){for(let r in c)this[r]=c[r]}var R=function(){return L.exports}(),P=function(){var e=this,r=e.$createElement,t=e._self._c||r;return t("div",{staticClass:"k-google-search-preview"},[t("span",{staticClass:"k-google-search-preview__url"},[t("span",[e._v(e._s(e.origin))]),e._l(e.breadcrumbs,function(s,i){return t("span",{key:i,staticClass:"k-google-search-preview__url__breadcrumb"},[e._v(" "+e._s(s)+" ")])})],2),t("h2",{staticClass:"k-google-search-preview__headline"},[e._v(e._s(e.title))]),t("p",{staticClass:"k-google-search-preview__paragraph"},[e._v(" "+e._s(e.description)+" ")])])},I=[],se="";const j={props:{title:String,url:String,description:String},computed:{origin(){return new URL(this.url).origin},breadcrumbs(){return this.url.split("/").slice(3)}}},u={};var F=o(j,P,I,!1,E,null,null,null);function E(e){for(let r in u)this[r]=u[r]}var M=function(){return F.exports}(),U=function(){var e=this,r=e.$createElement,t=e._self._c||r;return t("div",{staticClass:"k-twitter-preview"},[t("div",{staticClass:"k-twitter-preview__image"},[t("img",{staticClass:"k-twitter-preview__img",attrs:{src:e.ogImage}})]),t("div",{staticClass:"k-twitter-preview__content"},[t("span",{staticClass:"k-twitter-preview__url"},[e._v(e._s(e.host))]),t("span",{staticClass:"k-twitter-preview__title"},[e._v(e._s(e.ogTitle))]),t("p",{staticClass:"k-twitter-preview__description"},[e._v(e._s(e.ogDescription))])])])},H=[],ie="";const z={props:{ogTitle:String,url:String,ogDescription:String,twitterCardType:String,ogImage:String},computed:{host(){return new URL(this.url).host}}},v={};var N=o(z,U,H,!1,D,null,null,null);function D(e){for(let r in v)this[r]=v[r]}var G=function(){return N.exports}(),J=function(){var e=this,r=e.$createElement,t=e._self._c||r;return t("div",{staticClass:"k-seo-preview"},[t("k-select-field",{attrs:{label:"Preview",type:"select",options:e.options,empty:!1},model:{value:e.type,callback:function(s){e.type=s},expression:"type"}}),e.value?t("div",{staticClass:"k-seo-preview__inner"},[e.type==="google"?t("google-preview",e._b({},"google-preview",e.value,!1)):e._e(),e.type==="twitter"?t("twitter-preview",e._b({},"twitter-preview",e.value,!1)):e._e(),e.type==="facebook"?t("facebook-preview",e._b({},"facebook-preview",e.value,!1)):e._e()],1):e._e()],1)},V=[],ne="";const X={components:{GooglePreview:M,TwitterPreview:G,FacebookPreview:R},data(){var r;const e=(r=localStorage.getItem("kSEOPreviewType"))!=null?r:"google";return{label:null,value:null,type:e}},created(){this.handleLoad()},computed:{changes(){return this.$store.getters["content/changes"]()},options(){return[{value:"google",text:"Google"},{value:"twitter",text:"Twitter"},{value:"facebook",text:"Facebook"}]}},methods:{async handleLoad(e){this.isLoading=!0;let r={};Object.entries(e!=null?e:this.changes).map(([s,i])=>{r[s]=encodeURIComponent(JSON.stringify(i))});const t=await this.$api.get(this.parent+"/sections/"+this.name,r);this.value=t.value,this.label=t.label,this.isLoading=!1}},watch:{changes(e){this.$helper.debounce(r=>this.handleLoad(r),200)(e)},type(){localStorage.setItem("kSEOPreviewType",this.type)}}},p={};var W=o(X,J,V,!1,q,null,null,null);function q(e){for(let r in p)this[r]=p[r]}var A=function(){return W.exports}(),B=Object.freeze(Object.defineProperty({__proto__:null,default:A},Symbol.toStringTag,{value:"Module"}));const K=e=>e.substring(e.lastIndexOf("/")+1,e.lastIndexOf(".")).toLowerCase(),Q=Object.freeze({import(e){return Object.entries(e).reduce((r,[t,s])=>(r[K(t)]=s.default,r),{})}});panel.plugin("tobimori/seo",{sections:Q.import({"./sections/heading-structure.vue":$,"./sections/seo-preview.vue":B})})})(); +(function(){"use strict";var k=function(){var e=this,s=e.$createElement,t=e._self._c||s;return e.value?t("div",{staticClass:"k-heading-structure"},[t("div",{staticClass:"k-heading-structure-label k-field-label"},[t("k-icon",{attrs:{type:"headline"}}),t("span",[e._v(e._s(e.label||e.$t("heading-structure")))]),e.isLoading?t("k-loader"):e._e()],1),t("k-box",{attrs:{theme:""}},[t("ol",{staticClass:"k-heading-structure-list"},e._l(e.value,function(r,i){return t("li",{key:i,class:"k-heading-structure-item level-"+r.level+" "+(e.itemInvalid(r,i)?"is-invalid":""),style:"z-index: "+(e.value.length-i)},[t("span",{staticClass:"k-heading-structure-item-level"},[e._v("H"+e._s(r.level))]),t("span",{staticClass:"k-heading-structure-item-text"},[e._v(e._s(r.text))])])}),0)]),e.incorrectOrder&&!e.noH1?t("k-box",{staticClass:"k-heading-structure-notice",attrs:{theme:"negative"}},[t("k-icon",{attrs:{type:"alert"}}),t("k-text",[e._v(e._s(e.$t("incorrect-heading-order")))])],1):e._e(),e.multipleH1?t("k-box",{staticClass:"k-heading-structure-notice",attrs:{theme:"negative"}},[t("k-icon",{attrs:{type:"alert"}}),t("k-text",[e._v(e._s(e.$t("multiple-h1-tags")))])],1):e._e(),e.noH1?t("k-box",{staticClass:"k-heading-structure-notice",attrs:{theme:"negative"}},[t("k-icon",{attrs:{type:"alert"}}),t("k-text",[e._v(e._s(e.$t("missing-h1-tag")))])],1):e._e()],1):e._e()},w=[],te="";function l(e,s,t,r,i,d,h,Y){var n=typeof e=="function"?e.options:e;s&&(n.render=s,n.staticRenderFns=t,n._compiled=!0),r&&(n.functional=!0),d&&(n._scopeId="data-v-"+d);var o;if(h?(o=function(a){a=a||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext,!a&&typeof __VUE_SSR_CONTEXT__!="undefined"&&(a=__VUE_SSR_CONTEXT__),i&&i.call(this,a),a&&a._registeredComponents&&a._registeredComponents.add(h)},n._ssrRegister=o):i&&(o=Y?function(){i.call(this,(n.functional?this.parent:this).$root.$options.shadowRoot)}:i),o)if(n.functional){n._injectStyles=o;var Z=n.render;n.render=function(ee,f){return o.call(f),Z(ee,f)}}else{var g=n.beforeCreate;n.beforeCreate=g?[].concat(g,o):[o]}return{exports:e,options:n}}const b={data(){return{label:null,value:null,isLoading:!0}},created(){this.isLoading=!0,this.load().then(e=>{this.label=e.label}),this.handleLoad(),this.debouncedLoad=this.$helper.debounce(e=>{this.handleLoad(e)},200)},computed:{changes(){return this.$store.getters["content/changes"]()},incorrectOrder(){var e;return(e=this.value)==null?void 0:e.some((s,t)=>{var r,i;return s.level>((i=(r=this.value[t-1])==null?void 0:r.level)!=null?i:0)+1})},multipleH1(){var e;return((e=this.value)==null?void 0:e.filter(s=>s.level===1).length)>1},noH1(){var e;return((e=this.value)==null?void 0:e.filter(s=>s.level===1).length)===0}},methods:{async handleLoad(e){this.isLoading=!0;const s=this.parent.toString().split("/").pop(),t=await this.$api.post(`/k-seo/${s}/heading-structure`,e!=null?e:this.changes);this.value=t,this.isLoading=!1},itemInvalid(e,s){var t,r;return!!(e.level>((r=(t=this.value[s-1])==null?void 0:t.level)!=null?r:0)+1||e.level===1&&this.value[s-1]||e.level===1&&this.value.filter(i=>i.level===1).length>1)}},watch:{changes(e){this.debouncedLoad(e)}}},_={};var m=l(b,k,w,!1,$,null,null,null);function $(e){for(let s in _)this[s]=_[s]}var y=function(){return m.exports}(),C=Object.freeze(Object.defineProperty({__proto__:null,default:y},Symbol.toStringTag,{value:"Module"})),S=function(){var e=this,s=e.$createElement,t=e._self._c||s;return t("div",{staticClass:"k-facebook-preview"},[t("div",{staticClass:"k-facebook-preview__image"},[t("img",{staticClass:"k-facebook-preview__img",attrs:{src:e.ogImage}})]),t("div",{staticClass:"k-facebook-preview__content"},[t("span",{staticClass:"k-facebook-preview__url"},[e._v(e._s(e.host))]),t("span",{staticClass:"k-facebook-preview__title"},[e._v(e._s(e.ogTitle))]),t("p",{staticClass:"k-facebook-preview__description"},[e._v(e._s(e.ogDescription))])])])},L=[],se="";const x={props:{ogTitle:String,url:String,ogDescription:String,ogImage:String},computed:{host(){return new URL(this.url).host}}},c={};var T=l(x,S,L,!1,O,null,null,null);function O(e){for(let s in c)this[s]=c[s]}var P=function(){return T.exports}(),R=function(){var e=this,s=e.$createElement,t=e._self._c||s;return t("div",{staticClass:"k-google-search-preview"},[t("span",{staticClass:"k-google-search-preview__url"},[t("span",[e._v(e._s(e.origin))]),e._l(e.breadcrumbs,function(r,i){return t("span",{key:i,staticClass:"k-google-search-preview__url__breadcrumb"},[e._v(" "+e._s(r)+" ")])})],2),t("h2",{staticClass:"k-google-search-preview__headline"},[e._v(e._s(e.title))]),t("p",{staticClass:"k-google-search-preview__paragraph"},[e._v(" "+e._s(e.description)+" ")])])},I=[],re="";const j={props:{title:String,url:String,description:String},computed:{origin(){return new URL(this.url).origin},breadcrumbs(){return this.url.split("/").slice(3)}}},u={};var F=l(j,R,I,!1,E,null,null,null);function E(e){for(let s in u)this[s]=u[s]}var M=function(){return F.exports}(),H=function(){var e=this,s=e.$createElement,t=e._self._c||s;return t("div",{staticClass:"k-twitter-preview"},[t("div",{staticClass:"k-twitter-preview__image"},[t("img",{staticClass:"k-twitter-preview__img",attrs:{src:e.ogImage}})]),t("div",{staticClass:"k-twitter-preview__content"},[t("span",{staticClass:"k-twitter-preview__url"},[e._v(e._s(e.host))]),t("span",{staticClass:"k-twitter-preview__title"},[e._v(e._s(e.ogTitle))]),t("p",{staticClass:"k-twitter-preview__description"},[e._v(e._s(e.ogDescription))])])])},z=[],ie="";const U={props:{ogTitle:String,url:String,ogDescription:String,twitterCardType:String,ogImage:String},computed:{host(){return new URL(this.url).host}}},v={};var D=l(U,H,z,!1,G,null,null,null);function G(e){for(let s in v)this[s]=v[s]}var N=function(){return D.exports}(),V=function(){var e=this,s=e.$createElement,t=e._self._c||s;return t("div",{staticClass:"k-seo-preview"},[t("k-select-field",{attrs:{label:"Preview",type:"select",options:e.options,empty:!1},model:{value:e.type,callback:function(r){e.type=r},expression:"type"}}),e.value?t("div",{staticClass:"k-seo-preview__inner"},[e.type==="google"?t("google-preview",e._b({},"google-preview",e.value,!1)):e._e(),e.type==="twitter"?t("twitter-preview",e._b({},"twitter-preview",e.value,!1)):e._e(),e.type==="facebook"?t("facebook-preview",e._b({},"facebook-preview",e.value,!1)):e._e()],1):e._e()],1)},X=[],ne="";const W={components:{GooglePreview:M,TwitterPreview:N,FacebookPreview:P},data(){var s;const e=(s=localStorage.getItem("kSEOPreviewType"))!=null?s:"google";return{label:null,value:null,type:e}},created(){this.isLoading=!0,this.load().then(e=>{this.label=e.label}),this.handleLoad(),this.debouncedLoad=this.$helper.debounce(e=>{this.handleLoad(e)},200)},computed:{changes(){return this.$store.getters["content/changes"]()},options(){return[{value:"google",text:"Google"},{value:"twitter",text:"Twitter"},{value:"facebook",text:"Facebook"}]}},methods:{async handleLoad(e){this.isLoading=!0;const s=this.parent.toString().split("/").pop(),t=await this.$api.post(`/k-seo/${s}/seo-preview`,e!=null?e:this.changes);this.value=t,this.isLoading=!1}},watch:{changes(e){this.debouncedLoad(e)},type(){localStorage.setItem("kSEOPreviewType",this.type)}}},p={};var q=l(W,V,X,!1,A,null,null,null);function A(e){for(let s in p)this[s]=p[s]}var B=function(){return q.exports}(),J=Object.freeze(Object.defineProperty({__proto__:null,default:B},Symbol.toStringTag,{value:"Module"}));const K=e=>e.substring(e.lastIndexOf("/")+1,e.lastIndexOf(".")).toLowerCase(),Q=Object.freeze({import(e){return Object.entries(e).reduce((s,[t,r])=>(s[K(t)]=r.default,s),{})}});panel.plugin("tobimori/seo",{sections:Q.import({"./sections/heading-structure.vue":C,"./sections/seo-preview.vue":J})})})(); diff --git a/index.php b/index.php index d76b4e8..bfba62c 100644 --- a/index.php +++ b/index.php @@ -38,10 +38,8 @@ 'de' => Yaml::decode(F::read(__DIR__ . '/translations/de.yml')), 'en' => Yaml::decode(F::read(__DIR__ . '/translations/en.yml')) ], - 'sections' => [ - 'heading-structure' => require __DIR__ . '/config/sections/heading-structure.php', - 'seo-preview' => require __DIR__ . '/config/sections/seo-preview.php', - ], + 'sections' => require __DIR__ . '/config/sections.php', + 'api' => require __DIR__ . '/config/api.php', 'siteMethods' => [ 'schema' => fn ($type) => SchemaSingleton::getInstance($type), 'schemas' => fn () => SchemaSingleton::getInstances(), diff --git a/package.json b/package.json index 6147793..9dc0501 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "kirby-seo", "description": "The ultimate Kirby SEO toolkit", - "version": "0.1.2", + "version": "0.1.3", "private": true, "license": "MIT", "author": "Tobias Möritz", diff --git a/src/sections/heading-structure.vue b/src/sections/heading-structure.vue index c47d3cb..8a66c2c 100644 --- a/src/sections/heading-structure.vue +++ b/src/sections/heading-structure.vue @@ -44,7 +44,16 @@ export default { } }, created() { - this.handleLoad() + this.isLoading = true + + this.load().then((data) => { + this.label = data.label + }) // loads label and properties + this.handleLoad() // handles metadata & title change + + this.debouncedLoad = this.$helper.debounce((changes) => { + this.handleLoad(changes) + }, 200) // debounce function for dirty changes watcher }, computed: { changes() { @@ -64,15 +73,13 @@ export default { async handleLoad(changes) { this.isLoading = true - let newChanges = {} - Object.entries(changes ?? this.changes).map(([key, value]) => { - newChanges[key] = encodeURIComponent(JSON.stringify(value)) - }) - const response = await this.$api.get(this.parent + '/sections/' + this.name, newChanges) - - this.value = response.value - this.label = response.label + const page = this.parent.toString().split('/').pop() + const response = await this.$api.post( + `/k-seo/${page}/heading-structure`, + changes ?? this.changes + ) + this.value = response this.isLoading = false }, itemInvalid(item, index) { @@ -85,7 +92,7 @@ export default { }, watch: { changes(changes) { - this.$helper.debounce((changes) => this.handleLoad(changes), 200)(changes) + this.debouncedLoad(changes) } } } diff --git a/src/sections/seo-preview.vue b/src/sections/seo-preview.vue index d94934e..a8288a1 100644 --- a/src/sections/seo-preview.vue +++ b/src/sections/seo-preview.vue @@ -32,7 +32,16 @@ export default { } }, created() { - this.handleLoad() + this.isLoading = true + + this.load().then((data) => { + this.label = data.label + }) // loads label and properties + this.handleLoad() // handles metadata & title change + + this.debouncedLoad = this.$helper.debounce((changes) => { + this.handleLoad(changes) + }, 200) // debounce function for dirty changes watcher }, computed: { changes() { @@ -59,21 +68,16 @@ export default { async handleLoad(changes) { this.isLoading = true - let newChanges = {} - Object.entries(changes ?? this.changes).map(([key, value]) => { - newChanges[key] = encodeURIComponent(JSON.stringify(value)) - }) - const response = await this.$api.get(this.parent + '/sections/' + this.name, newChanges) - - this.value = response.value - this.label = response.label + const page = this.parent.toString().split('/').pop() + const response = await this.$api.post(`/k-seo/${page}/seo-preview`, changes ?? this.changes) + this.value = response this.isLoading = false } }, watch: { changes(changes) { - this.$helper.debounce((changes) => this.handleLoad(changes), 200)(changes) + this.debouncedLoad(changes) }, type() { localStorage.setItem('kSEOPreviewType', this.type) diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index 9f460b5..2affaef 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -1,8 +1,8 @@ array( 'name' => 'tobimori/kirby-seo', - 'pretty_version' => '0.0.1', - 'version' => '0.0.1.0', + 'pretty_version' => '0.1.3', + 'version' => '0.1.3.0', 'reference' => NULL, 'type' => 'kirby-plugin', 'install_path' => __DIR__ . '/../../', @@ -29,8 +29,8 @@ 'dev_requirement' => false, ), 'tobimori/kirby-seo' => array( - 'pretty_version' => '0.0.1', - 'version' => '0.0.1.0', + 'pretty_version' => '0.1.3', + 'version' => '0.1.3.0', 'reference' => NULL, 'type' => 'kirby-plugin', 'install_path' => __DIR__ . '/../../',