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