Files
wordpress/wp-admin/js/code-editor.min.js
Weston Ruter 0ca9a70ec2 Code Editor: Improve types and fix options handling to avoid double-linting at initialization.
* Refactor how CodeMirror is initialized so that the full settings are provided up-front. This avoids the linting from being applied twice at initialization, the first time with an incorrect configuration.
* Add initial TypeScript configuration for core with `npm run typecheck:js`.
* Add comprehensive types for code editor files: `code-editor.js`, `javascript-lint.js`, and `htmlhint-kses.js`.
* Move code editor scripts from `src/js/_enqueues/vendor/codemirror/` to `src/js/_enqueues/lib/codemirror/`. The CodeMirror library is sourced from the npm package as of r61539.
* Remove (deprecated) `esprima.js` from being committed to SVN since in r61539 it was switched to using the npm package as its source.
* Move `fakejshint.js` to `src/js/_enqueues/deprecated`.

Developed in https://github.com/WordPress/wordpress-develop/pull/10900

Follow up to r61611, r61539.

Props westonruter, jonsurrell, justlevine.
See #64662, #48456.
Fixes #64661.

Built from https://develop.svn.wordpress.org/trunk@61800


git-svn-id: http://core.svn.wordpress.org/trunk@61106 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2026-03-03 20:27:51 +00:00

2 lines
3.3 KiB
JavaScript

/*! This file is auto-generated */
void 0===window.wp&&(window.wp={}),void 0===window.wp.codeEditor&&(window.wp.codeEditor={}),function(u,c){"use strict";function d(r){let s=[],a=[];function c(t){r.onUpdateErrorNotice&&!_.isEqual(s,a)&&(r.onUpdateErrorNotice(s,t),a=s)}function d(){let t=r.codemirror?.lint??!1;if(!t)return!1;!0===t?t={}:_.isObject(t)&&(t=u.extend({},t));var i,e=t;return"javascript"===r.codemirror?.mode&&r.jshint&&u.extend(e,r.jshint),"css"===r.codemirror?.mode&&r.csslint&&u.extend(e,r.csslint),"htmlmixed"===r.codemirror?.mode&&r.htmlhint&&(e.rules=u.extend({},r.htmlhint),r.jshint&&e.rules&&(e.rules.jshint=r.jshint),r.csslint)&&e.rules&&(e.rules.csslint=r.csslint),e.onUpdateLinting=(i=e.onUpdateLinting,function(t,e,n){var o=t.filter(function(t){return"error"===t.severity});i&&i(t,e,n),!_.isEqual(o,s)&&(s=o,r.onChangeLintingErrors&&r.onChangeLintingErrors(o,t,e,n),!n.state.focused||0===s.length||0<a.length)&&c(n)}),e}return{getLintOptions:d,init:function(i){i.on("optionChange",function(t,e){var n,o="CodeMirror-lint-markers";"lint"===e&&(e=i.getOption("gutters")||[],!0===(n=i.getOption("lint"))?(_.contains(e,o)||i.setOption("gutters",[o].concat(e)),i.setOption("lint",d())):n||i.setOption("gutters",_.without(e,o)),i.getOption("lint")&&i.performLint?i.performLint():(s=[],c(i)))}),i.on("blur",c),i.on("startCompletion",function(){i.off("blur",c)}),i.on("endCompletion",function(){i.on("blur",c),_.delay(function(){i.state.focused||c(i)},500)}),u(document.body).on("mousedown",function(t){!i.state.focused||i.getWrapperElement().contains(t.target)||t.target.classList.contains("CodeMirror-hint")||c(i)})},updateErrorNotice:c}}c.codeEditor.defaultSettings={codemirror:{},csslint:{},htmlhint:{},jshint:{},onTabNext:function(){},onTabPrevious:function(){},onChangeLintingErrors:function(){},onUpdateErrorNotice:function(){}},c.codeEditor.initialize=function(t,e){let n;n=u("string"==typeof t?"#"+t:t);t=u.extend(!0,{},c.codeEditor.defaultSettings,e);const o=d(t),s=(t.codemirror&&(t.codemirror.lint=o.getLintOptions()),c.CodeMirror.fromTextArea(n[0],t.codemirror));o.init(s);e={settings:t,codemirror:s,updateErrorNotice:function(){o.updateErrorNotice(s)}};s.showHint&&s.on("inputRead",function(t,e){if(e.origin&&("+input"===e.origin||e.origin.startsWith("*compose"))&&1===e.text.length&&1===e.text[0].length){var e=e.text[0],n=/^[a-zA-Z]$/.test(e);if(!s.state.completionActive||!n){var o=s.getTokenAt(s.getCursor());if("string"!==o.type&&"comment"!==o.type){var i=c.CodeMirror.innerMode(s.getMode(),o.state).mode.name,r=s.getDoc(),r=r.getLine(r.getCursor().line).slice(0,r.getCursor().ch);let t=!1;"html"===i||"xml"===i?t="<"===e||"/"===e&&"tag"===o.type||n&&"tag"===o.type||n&&"attribute"===o.type||"="===e&&!(!o.state.htmlState?.tagName&&!o.state.curState?.htmlState?.tagName):"css"===i?t=n||":"===e||" "===e&&/:\s+$/.test(r):"javascript"===i?t=n||"."===e:"clike"===i&&"php"===s.options.mode&&(t=n&&("keyword"===o.type||"variable"===o.type)),t&&s.showHint({completeSingle:!1})}}}});{var i=s,r=t;const a=u(i.getTextArea());i.on("blur",function(){a.data("next-tab-blurs",!1)}),i.on("keydown",function(t,e){"Escape"===e.key?a.data("next-tab-blurs",!0):"Tab"===e.key&&a.data("next-tab-blurs")&&(e.shiftKey&&r.onTabPrevious?r.onTabPrevious(i,e):!e.shiftKey&&r.onTabNext&&r.onTabNext(i,e),a.data("next-tab-blurs",!1),e.preventDefault())})}return e}}(jQuery,window.wp);