WordPress/wp-admin/js/theme-plugin-editor.min.js

1 line
11 KiB
JavaScript
Raw Normal View History

window.wp||(window.wp={}),wp.themePluginEditor=function(i){"use strict";var t,s={l10n:{lintError:{singular:"",plural:""},saveAlert:"",saveError:""},codeEditor:{},instance:null,noticeElements:{},dirty:!1,lintErrors:[],init:function(e,t){s.form=e,t&&i.extend(s,t),s.noticeTemplate=wp.template("wp-file-editor-notice"),s.noticesContainer=s.form.find(".editor-notices"),s.submitButton=s.form.find(":input[name=submit]"),s.spinner=s.form.find(".submit .spinner"),s.form.on("submit",s.submit),s.textarea=s.form.find("#newcontent"),s.textarea.on("change",s.onChange),s.warning=i(".file-editor-warning"),s.docsLookUpButton=s.form.find("#docs-lookup"),s.docsLookUpList=s.form.find("#docs-list"),0<s.warning.length&&s.showWarning(),!1!==s.codeEditor&&_.defer(function(){s.initCodeEditor()}),i(s.initFileBrowser),i(window).on("beforeunload",function(){if(s.dirty)return s.l10n.saveAlert}),s.docsLookUpList.on("change",function(){""===i(this).val()?s.docsLookUpButton.prop("disabled",!0):s.docsLookUpButton.prop("disabled",!1)})},showWarning:function(){var e=s.warning.find(".file-editor-warning-message").text();i("#wpwrap").attr("aria-hidden","true"),i(document.body).addClass("modal-open").append(s.warning.detach()),s.warning.removeClass("hidden").find(".file-editor-warning-go-back").focus(),s.warningTabbables=s.warning.find("a, button"),s.warningTabbables.on("keydown",s.constrainTabbing),s.warning.on("click",".file-editor-warning-dismiss",s.dismissWarning),setTimeout(function(){wp.a11y.speak(wp.sanitize.stripTags(e.replace(/\s+/g," ")),"assertive")},1e3)},constrainTabbing:function(e){var t,i;9===e.which&&(t=s.warningTabbables.first()[0],(i=s.warningTabbables.last()[0])!==e.target||e.shiftKey?t===e.target&&e.shiftKey&&(i.focus(),e.preventDefault()):(t.focus(),e.preventDefault()))},dismissWarning:function(){wp.ajax.post("dismiss-wp-pointer",{pointer:s.themeOrPlugin+"_editor_notice"}),s.warning.remove(),i("#wpwrap").removeAttr("aria-hidden"),i("body").removeClass("modal-open")},onChange:function(){s.dirty=!0,s.removeNotice("file_saved")},submit:function(e){var t={};e.preventDefault(),i.each(s.form.serializeArray(),function(){t[this.name]=this.value}),s.instance&&(t.newcontent=s.instance.codemirror.getValue()),s.isSaving||(s.lintErrors.length?s.instance.codemirror.setCursor(s.lintErrors[0].from.line):(s.isSaving=!0,s.textarea.prop("readonly",!0),s.instance&&s.instance.codemirror.setOption("readOnly",!0),s.spinner.addClass("is-active"),e=wp.ajax.post("edit-theme-plugin-file",t),s.lastSaveNoticeCode&&s.removeNotice(s.lastSaveNoticeCode),e.done(function(e){s.lastSaveNoticeCode="file_saved",s.addNotice({code:s.lastSaveNoticeCode,type:"success",message:e.message,dismissible:!0}),s.dirty=!1}),e.fail(function(e){e=i.extend({code:"save_error",message:s.l10n.saveError},e,{type:"error",dismissible:!0});s.lastSaveNoticeCode=e.code,s.addNotice(e)}),e.always(function(){s.spinner.removeClass("is-active"),s.isSaving=!1,s.textarea.prop("readonly",!1),s.instance&&s.instance.codemirror.setOption("readOnly",!1)})))},addNotice:function(e){var t;if(!e.code)throw new Error("Missing code.");return s.removeNotice(e.code),(t=i(s.noticeTemplate(e))).hide(),t.find(".notice-dismiss").on("click",function(){s.removeNotice(e.code),e.onDismiss&&e.onDismiss(e)}),wp.a11y.speak(e.message),s.noticesContainer.append(t),t.slideDown("fast"),s.noticeElements[e.code]=t},removeNotice:function(e){return!!s.noticeElements[e]&&(s.noticeElements[e].slideUp("fast",function(){i(this).remove()}),delete s.noticeElements[e],!0)},initCodeEditor:function(){var e,t=i.extend({},s.codeEditor);t.onTabPrevious=function(){i("#templateside").find(":tabbable").last().focus()},t.onTabNext=function(){i("#template").find(":tabbable:not(.CodeMirror-code)").first().focus()},t.onChangeLintingErrors=function(e){0===(s.lintErrors=e).length&&s.submitButton.toggleClass("disabled",!1)},t.onUpdateErrorNotice=function(e){s.submitButton.toggleClass("disabled",0<e.length),0!==e.length?(e=1===e.length?s.l10n.lintError.singular.replace("%d","1"):s.l10n.lintError.plural.replace("%d",String(e.length)),s.addNotice({code:"lint_err