{"id":461,"date":"2014-06-14T20:00:53","date_gmt":"2014-06-14T20:00:53","guid":{"rendered":"http:\/\/localhost\/__sites\/3d_blog\/?p=461"},"modified":"2014-06-17T11:52:29","modified_gmt":"2014-06-17T11:52:29","slug":"post-editor-with-tab-support","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=461","title":{"rendered":"post editor with TAB support and ctrl+s to update post"},"content":{"rendered":"<blockquote><p>Goal: to use the <strong>TAB<\/strong> key in the post editor<\/p><\/blockquote>\n<p>1. The following code catching the TAB key and replace it with 2 spaces.<br \/>\nThe second effect is to replace all the TAB occurences (hold the SHIFT key while press a TAB down).<\/p>\n<p>2. You can update your post with alt+p (this is the default), but there is a key combination in my hand ctrl+s to save. I&#8217;ve implemented this as well. This is the third part of this code.<\/p>\n<p>My <strong>wp_editor_extend.js<\/strong> file is:<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\n\/*\r\n  wp editor extender by SilverTerra \r\n  1.0.0 - 06.06.2014 - initial version\r\n  1.0.1 - 14.06.2014 - ctrl+s hot key combination for update added\r\n*\/\r\n\r\n(function($){\r\n\r\n  $(document).ready(function(){\r\n  \r\n    \/\/ 1. catch tab and replace it to 2 spaces\r\n    \/\/ 2. shift+tab - replace all tabs to 2 spaces\r\n    $.fn.tabCorrector = function () {\r\n      this.keydown(function (e) {      \r\n        if (e.keyCode === 9) {\r\n        var val = this.value,\r\n            start = this.selectionStart,\r\n            end = this.selectionEnd;          \r\n          if(e.shiftKey){\r\n            this.value = val.replace(\/\\t\/g, '  ');\r\n          }\r\n          else{\r\n            this.value = val.substring(0, start) + '  ' + val.substring(end);\r\n            this.selectionStart = this.selectionEnd = start + 2;          \r\n          }\r\n          return false;\r\n        }\r\n        return true;\r\n      });\r\n      return this;\r\n    };\r\n\r\n    $('.wp-editor-area').tabCorrector();\r\n    \r\n    \/\/ ctrl+s for update\r\n    $('.wp-editor-area').keydown(function (e) {      \r\n      if (e.keyCode === 83 &amp;&amp; e.ctrlKey) {\r\n        $('#publish').click();\r\n        return false;\r\n      }\r\n      return true;\r\n    });\r\n\r\n  });\r\n\r\n})(jQuery);\r\n<\/pre>\n<p>To call my script from the wp-admin just edit the <strong>wp-admin\\includes\\misc.php<\/strong> file.<br \/>\nPut this to the end of the file.<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\nfunction plugin_admin_head_js() {\r\nprint &quot;&lt;script type='text\/javascript' src='&quot; . get_bloginfo('template_directory') . &quot;\/js\/wp_editor_extend.js'&gt;&lt;\/script&gt;&quot;;\r\n}\r\nadd_action('admin_head', 'plugin_admin_head_js');\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Goal: to use the TAB key in the post editor 1. The following code catching the TAB key and replace it with 2 spaces. The second effect is to replace all the TAB occurences (hold the SHIFT key while press a TAB down). 2. You can update your post with alt+p (this is the default), [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-461","post","type-post","status-publish","format-standard","hentry","category-javascript","category-wordpress"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=461"}],"version-history":[{"count":13,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/461\/revisions"}],"predecessor-version":[{"id":643,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/461\/revisions\/643"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}