custom-code-editor-a3

所属分类:代码编辑器
开发工具:Vue
文件大小:0KB
下载次数:0
上传日期:2023-08-09 22:14:26
上 传 者sh-1993
说明:  ApostopheCMS 3自定义模式(Ace编辑器),
(ApostropheCMS 3 Custom Schema (Ace Editor),)

文件列表:
.eslintignore (140, 2023-08-12)
.eslintrc (1988, 2023-08-12)
.stylelintrc (93, 2023-08-12)
CHANGELOG.md (1505, 2023-08-12)
aceTypes.js (6234, 2023-08-12)
conf.json (521, 2023-08-12)
defaultMode.js (2150, 2023-08-12)
getAceBuilds.js (1851, 2023-08-12)
index.js (4907, 2023-08-12)
lib/ (0, 2023-08-12)
lib/browser.js (481, 2023-08-12)
lib/routes.js (2081, 2023-08-12)
lib/routesMethod.js (1625, 2023-08-12)
lib/schema.js (717, 2023-08-12)
package.json (1709, 2023-08-12)
tests/ (0, 2023-08-12)
tests/build-test-package-json.js (822, 2023-08-12)
tests/test-asset.test.js (7130, 2023-08-12)
tests/test-code.test.js (3855, 2023-08-12)
tests/test-override.test.js (6122, 2023-08-12)
tests/test-route.test.js (14147, 2023-08-12)
tests/utils.js (2725, 2023-08-12)
tsconfig.json (595, 2023-08-12)
ui/ (0, 2023-08-12)
ui/apos/ (0, 2023-08-12)
ui/apos/components/ (0, 2023-08-12)
ui/apos/components/CustomCodeEditor.vue (25875, 2023-08-12)
ui/apos/components/OptionsContainer.vue (61374, 2023-08-12)
ui/apos/mixins/ (0, 2023-08-12)
ui/apos/mixins/AfterInit.js (217, 2023-08-12)
ui/apos/mixins/BeforeInit.js (218, 2023-08-12)
ui/apos/mixins/CustomCodeEditorMixin.js (19665, 2023-08-12)
ui/apos/scss/ (0, 2023-08-12)
ui/apos/scss/editor.scss (4887, 2023-08-12)
... ...

# Custom-Code-Editor-A3 ApostropheCMS 3 Schema [![test-custom-code-editor-a3](https://github.com/ammein/custom-code-editor-a3/actions/workflows/main.yml/badge.svg)](https://github.com/ammein/custom-code-editor-a3/actions/workflows/main.yml) An ApostropheCMS Custom Schema for your own custom-code-editor-a3 field. This extension adds a full-featured code editor that is perfect for coding tutorials, documentation containing code examples, or any other type of page that needs to display code. This schema uses the open-source Ace Editor library that you may found here [Ace Editor](https://ace.c9.io/) ![Ace Editor Example](https://thumbs.gfycat.com/DecisiveThickGilamonster-size_restricted.gif) Falling in love with custom-code-editor-a3 module ? Send love through Paypal here :
[Paypal.me/AminShazrin](https://paypal.me/AminShazrin?locale.x=en_US) # Install From within your apostrophe project `npm install --save custom-code-editor-a3` Include in app.js: ```javascript // In app.js modules: { 'custom-code-editor-a3': {}, // ... other modules } ``` # Enable Code Editor Schema Simple : ```javascript fields: { add: { myCode: { type: 'custom-code-editor-a3', label: 'First Code' } } } ``` ### Getting the `custom-code-editor-a3` values This custom-code-editor schema returns an object composed of a `code` string containing the formatted code along with a `type` string that is derived from the type of editor used to input the code. ```javascript { code : '', type : '' } ``` Following the example above, in your template HTML file you can simply get an object like this : ```twig {{ data.page.myCode.code }} {{ data.page.myCode.type }} ``` or you can simply use `apos.log()` to see what's available on `custom-code-editor-a3` objects : ```twig {{ apos.log(data.page.myCode) }} ``` ### Displaying the custom code It is up to the developer to format the code string supplied to the template by the `custom-code-editor`. We recommend using a package like [`highlight.js`](https://highlightjs.org/), but there are a number of other similar packages out there. For the `highlight.js` package you will need to add the package script and styling, plus a small additional script to activate it on the page. It is typically sufficient to surround the editor code string with `
` and `` tags, but you can also supply the `type` if needed.

```twig
  
    {{ data.page.myCode.code }}
  
``` # Custom-Code-Editor-A3 Options Available The custom-code-editor has a number of options available. You can customize your editor experience by creating your own `index.js` file in the `modules/custom-code-editor` folder of your project to extend the existing options. ```javascript // in modules/custom-code-editor-a3/index.js module.exports = { options: { ace : { theme : 'tomorrow', // themes available : https://github.com/ajaxorg/ace/tree/master/lib/ace/theme (Case Sensitive) defaultMode : 'javascript', options : { // All options available in : https://github.com/ajaxorg/ace/wiki/Configuring-Ace }, modes : [ { title : 'Javascript', // Title to Override Name name : 'javascript', // Name of the mode (Case-Sensitive) snippet : '// Content Start Here \n print(\"Hello World\") \n @code-here', // Default Value (String) disableSnippet : true // Disable default snippet value when switching (OPTIONAL - Boolean) } ], config: { fontSize: 16, // Editor Font Size (Number or String) editorHeight: 500, // Editor Height (Number or String) dropdown: { enable: true, // Enable it for switching modes button (Boolean) height: 30, // Height Dropdown (Number or String) borderRadius: 5, // Border Radius Dropdown (Number or String) fontFamily: "Mont-Regular", // Font Family Dropdown (String) backgroundColor : "Orange", // Background Color Dropdown (String) textColor : "white", // Text Color Dropdown (String) fontSize: 16, // Font Size Dropdown (Number or String) position: { // All top , left , right , bottom dropdown position enable for configs bottom: 20, right: 20 }, arrowColor: "blue" // To change arrow color in dropdown (String) } } } } } ``` # List of default modes available - ActionScript - Bash - C++ - C# - PHP - HTML - Javascript # Name Of The Modes References ### [List of all Modes](https://github.com/ajaxorg/ace-builds/blob/6526ea4a845a3682789a9224f319271496051873/src/ext-modelist.js#L33) # How to Override Existing Mode ? Simple , make sure the name of the mode is similar to default modes provided. ### Default Mode By default , `defaultMode : 'javascript'` is enable. But you can choose a default mode by yourself! Just add the name of any mode available to you. Lets say you want 'css' to be the default mode. ```javascript ace : { defaultMode : 'css' // Same as `name : 'css' in mode : []` } ``` > This will select `css` a the starting mode, then you can choose a different mode by choosing from the dropdown if needed. ### Enable Snippet To enable your snippet to be added automatically when this schema is open, you have to enable the dropdown ```javascript ace : { config : { dropdown : { enable : true } } } ``` WARNING ! Once enabled, your dropdown will be in the upper left corner. This will block your code input so we recommend that at the same time you enable the dropdown you add a custom position. For example, to position the dropdown on bottom left corner with an offset of 30px from the container edge use: ```javascript ace : { config : { dropdown : { enable : true position : { bottom : 30, left : 30 } } } } ``` ### Disable Snippet If you want to disable snippet addition for a specific mode, write the `name` of the mode and set the `disableSnippet` property to `true`: ```javascript ace : { modes : [ { name : "html", disableSnippet : true // This will not automatically change snippet when you change mode on dropdown } ] } ``` ### Override Snippet Also , if you want to override the default snippet for specific mode, write the `name` of the mode and insert your `snippet` : ```javascript ace : { modes : [ { name: 'javascript', // name must be the same as existing default mode snippet: "document.addEventListener(\"DOMContentLoaded\" , function(){\n @code-here\n });" } ] } ``` ### `@code-here` in a Snippet What is this syntax for? Well, whenever you change your mode on dropdown, the existing codes in the schema will be replaced automatically into the new snippet in place of `@code-here`. Amazing right? If you did not provide this, your existing value in the editor schema will be lost. Let's make a new override snippet that has our own `@code-here` in it: ```javascript ace: { modes : [ { name :'javascript', snippet : "// Content Start Here \n print(\"Hello World\") \n @code-here" } ], } ``` ### Title of Dropdown By default, the name of a dropdown will be in the `name` property. But some of the names don't make sense! Can I change it? Yes you can! Simply add the `title` property with your prefered name For example, rename the existing `sh` mode to be called **`Bash`** : ```javascript ace : { modes : [ { name : 'sh', title : 'Bash' // This will make dropdown name as Bash instead of Sh } ] } ``` ### Clear Default Modes What if I want to clear all default modes and define them myself? Easy, add the `clearModes : true` setting: ```javascript ace : { clearModes : true } ``` Once you clear your modes, you can define your own modes without considering any overrides mode. Doesn't this makes your life easier? ```javascript ace : { modes : [ { // List of all modes that you want to define. The options you may write like this title : '', name : '<name of your mode (case sensitive)>', snippet : '<code snippet>' } ] } ``` > Don't worry about the indent in your Snippet , Ace will automatically beautify the code whenever you enter your new content. # Insert My Own Theme By default , `theme : 'chrome'` . If you wish to change the theme (Case Sensitive), you can find all available themes here [All Ace Editor Themes Available](https://github.com/ajaxorg/ace-builds/blob/6526ea4a845a3682789a9224f319271496051873/src/ext-themelist.js#L9) : ```javascript ace : { theme : 'monokai' } ``` # Ace Editor Options Now this one , I just extend it for you to enable ace editor options. Reference : [Ace Editor Options](https://github.com/ajaxorg/ace/wiki/Configuring-Ace) ```javascript ace : { options : { // Same property and value as in : https://github.com/ajaxorg/ace/wiki/Configuring-Ace // Example : cursorStyle: "smooth", useSoftTabs: true } } ``` ## Enable Emmet Option By default, emmet is not enable and you need to configure it yourself. But Ace Editor provides a simple options to enable emmet. However, you need a library to load it to Ace Editor. You can find any emmet libraries available online but I provide some sample to you below that works : ```javascript ace : { options : { enableEmmet : true } } ``` Then load emmet library in your template views : ```html <!-- load emmet code and snippets compiled for browser --> <script src="https://cloud9ide.github.io/emmet-core/emmet.js"></script> ``` # Custom-Code-Editor-A3 & Dropdown Configurations You also can customize your own dropdown/ace editor css styles. All the dropdown configurations available for you are listed in this example: ```javascript module.exports = { options: { ace : { config : { fontSize : '<Number or String>', // Editor Font Size editorHeight : '<Number or String>', // Editor Height dropdown : { enable : '<Boolean>', // Enable it for switching modes button height : '<Number or String>', // Height Dropdown - Default : 30 borderRadius : '<Number or String>', // Border Radius Dropdown fontFamily : '<String>', // Font Family Dropdown fontSize : '<Number or String>' , // Font Size Dropdown backgroundColor : "<String>", // Background Color Dropdown (String) textColor : "<String>", // Text Color Dropdown (String) position : { top : '<Number or String>', bottom : '<Number or String>', right : '<Number or String>', left : '<Number or String>' }, arrowColor : '<String or Hex or RGB or RGBA>' // To change arrow color in dropdown - Default : "black" } } } } } ``` You must be thinking , why are `fontSize` and `editorHeight` available for editor options ? While we could do it at the `options` level: ```javascript ace : { options : { // all editor options } } ``` > Because we have a css issue with `!important` to override apostrophe css default normalize. So, I made it for you to easily override in the `config` settings. Or maybe you can push your own file to override it. Either way, both are possible override options :) # Specific Field Customization Well, I know some of you don't want some specific editor to have the same options in all custom-code-editor-a3 field instances, right? To make it backward compatibility, only some of the options can be overridden on your schema fields. Here is an example : ```javascript fields: { add: { code: { type: 'custom-code-editor-a3', label: 'Your code here', ace: { defaultMode: 'c_cpp', config: { dropdown: { backgroundColor: '#040303', textColor: '#fffcf2', arrowColor: '#fffcf2' } } } } } } ``` > Why are `modes` and `theme` not available to override? This will go against the rule optimizing webpack feature that only project level options module by your own defined modes and theme get setup in the browser. All `options` values must be configure in project level module `index.js` or directly on `app.js` in `modules: {}` ### If you wish to disable some options, just set it to `null` on that property option. It will removed from your specific field option. For example : ```javascript fields: { add: { code: { type: 'custom-code-editor-a3', label: 'Your code here', ace: { config: { saveCommand: null, } } } } } ``` > Warning ! If you did not set any config value, config will not be available on specific field. To use existing config, simply set it as empty object `config : {}` # How To ### Search Bar Ace got its own search bar. Simply hit `Ctrl + F` ! ![Search Function](https://thumbs.gfycat.com/HandsomeHopefulFallowdeer-size_restricted.gif) ### Save Selection Now this one is a new function ONLY for ApostropheCMS . If you hit `Ctrl + Shift + S` while selecting new code, it will replace an existing highlighted text previously when you change your mode. Don't believe me? Check it out! ![Save Feature](https://thumbs.gfycat.com/ReliableHastyGrouper-size_restricted.gif) Wait ! Can I change save command ? Yup , you can. Add options like this : ```javascript ace : { config : { saveCommand : { win : 'Ctrl-Shift-F', // Windows Key mac : 'Command-Shift-F',// Mac Key, message : 'Your Selected Text Saved ! ' // Your custom save message } } } ``` # Options Customizer Have you ever lamented that you are tired of testing options by restarting the app and adjusting your options all over again? Now we have the Options Customizer that helps you more easily tweak your editor options configuration. ![Options Customizer](https://thumbs.gfycat.com/GrossShoddyIbisbill-size_restricted.gif) ### What does it do ? It brings you more features that you can't live without! All options available for you to modify can now be saved for each logged in user or even you could copy all the desired options and paste it to your project level module! Here are four core features for Options Customizer : - Copy Options - Undo Options - Save Options - Reset Options > These options will make your editor change live upon options modified. ## Copy Options You can copy your modified options and paste it on your project level module that will apply to all! The copy features uses [Clipboard JS](https://clipboardjs.com/) to make it work. Below are the demonstration on how to use it : ![Copy Options](https://thumbs.gfycat.com/WarmheartedGlossyCow-size_restricted.gif) > NOTE : It only copies from modified changes, not the entirty of the options. If your module options are already configured, it will not copy your module options. Instead, it will copy all your changes options that you did through On/Off Toggle(s), Select Input(s) and Range Input(s). ## Undo Options You can undo your modified options to the default settings. This will help you reset your changes to default options. ![Undo Options](https://thumbs.gfycat.com/ElementaryGeneralCentipede-size_restricted.gif) > NOTE : This will not undo saved options to the default setting. If you wish to reset from saved options, refer to section "Reset Options" below. ## Save Options You can also saves all your modified options. This will keep all your modified options apply to all custom-code-editor-a3 types ! ![Save Options](https://thumbs.gfycat.com/KlutzyAridCavy-size_restricted.gif) In MongoDB, you will see this data directly in `type : apostrophe-user`: ```json "customCodeEditor" : { "highlightActiveLine" : false } ``` > NOTE : Save options will not apply to all users. It will load current users saved options and apply it to all editors. This will allow each users to their own desired options. ## Reset Options You can also reset all options. This will remove current saves options and change it to the default module options settings. Let say you have follows save options demonstration above, you simply click `Reset` like example below : ![Reset Options](https://thumbs.gfycat.com/BlandWelloffAngwantibo-size_restricted.gif) > NOTE : This will affect the currently logged in user only. It will not remove any other user's options. ## Modify Options What if you want to add your own help text? You could simply done it in project level module like this : ```javascript // In custom-code-editor-a3/index.js : module.exports = { options: { ace : { optionsTypes : [ { name : "highlightActiveLine", help : "This will highlight active line cursor position" } ] } } } ``` > NOTE : If you wish to add more options, take a look at `aceTypes.js` in `node_modules/custom-code-editor-a3/aceTypes.js` to see how it is done. And MAKE SURE you do it in an ARRAY like the example above. ## Disable Options Customizer You wish to remove the options customizer? You don't like it? Don't worry, just set it to `enable : false` like this : ```javascript // In custom-code-editor-a3/index.js : module.exports = { options: { ace : { config : { optionsCustomizer : { enable : false // More configuration coming soon } } } } } ``` ### Why are some other options missing ? Well, some other options will break apostrophe's UI and are also against the rule of pushing assets. For instance, we cannot set other themes via the Options Customizer because we will only push desired theme configuration from your project level module. This is because Ace contains more than 10 js files for each modes and themes available. # Browser ### Browser Object How can I get the schema browser object for `custom-code-editor-a3` ? Simply you can find it on : ```javascript apos.customCodeEditor ``` > I keep it similar `apos.customCodeEditor` object from ApostropheCMS version 2 so that you can copy paste your previous code from ApostropheCMS version 2 into ApostropheCMS version 3 easily without breaking change. ### Get Editor Browser Object How can I get from the one that defined in javascript browser at `var editor = ... ... </div> <hr> <div><B>近期下载者</B>:</div> <div id="download-users"></div> <hr> <div><B>相关文件</B>:</div> <div id="relate-items"></div> <hr> <div><B>评论</B>:[<a href=/Download/comment/id/1691675022651488.html data-toggle=modal data-target="#myModal">我要评论</a>] [<a class='pop-a' href=/Download/report/id/1691675022651488.html>举报此文件</a>]</div> <div id="file-comments"></div> <hr> <div><B>收藏者</B>:</div> <div id="favor-users"></div> <p></p> </div> <div class="col-xs-12 col-md-4"> <div class="ad-sidebar text-center"> <div class="ad-300"> </div> </div> </div> </div> </div> </div> <div class="my-footer"> <div class="container"> <div class="pull-right"> </div> <div> <a href="http://www.pudn.com" target=_blank>© 联合开发网 from 2004</a> | <a href="/Index/contact.html">联系站长</a> | <a href=" https://beian.miit.gov.cn" target=_blank>湘ICP备2023001425号</a> | <a href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=43010502000604" target=_blank>网安备43010502000604</a> | </div> </div> </div> </div><!-- /wrapper--> <!-- page specific plugin scripts --> <!-- inline scripts related to this page --> <div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> </div> </div> </div><!-- /.modal-table --> </body> <script type="text/javascript"> $(document).on('click', '.list-more', function(){ var id=$(this).attr('data-id'); $('#list-'+id).css('max-height',$('#list-'+id)[0].scrollHeight); $(this).removeClass('list-more'); $(this).addClass('list-hide'); $(this).html('<i class="fa fa-angle-double-up"></i>'); //$(this).hide(); return false; }); $(document).on('click', '.list-hide', function(){ var id=$(this).attr('data-id'); $('#list-'+id).css('max-height','100px'); $(this).removeClass('list-hide'); $(this).addClass('list-more'); $(this).html('<i class="fa fa-angle-double-down"></i>'); return false; }); $("#myModal").on("hidden.bs.modal", function() { $(this).removeData(); }); $(document).on("click",".keyword",function(){ var keyword=$(this).attr("keyword"); var type_id=$(this).attr("type_id"); if(typeof(type_id) =="undefined" || type_id =="") type_id="0"; location.href="http://search.pudn.com/Download/index?keyword="+keyword; return false; }); </script> <script type="text/javascript" src="/js/time.js"></script> <script type="text/javascript" src="/js/marked.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $('.hide-list').each(function(){ if ($(this)[0].offsetHeight < $(this)[0].scrollHeight){ var id=$(this).attr('id'); id =id.substring(5); $(this).after('<div style="text-align:center"><a href="" class="list-more" data-id="' + id + '"><i class="fa fa-angle-double-down"></i></a></div>'); } }); }); var g_id="1691675022651488"; var keywords =new Array(); function get_download_user(){ var url ="/Download/get_download_user/id/"+g_id+".html"; $.get(url,function(ret){ if(ret.length ==0) return; var html=''; for(i in ret){ html =html + '<a href=/User/profile/id/'+ret[i].user_new_id+'.html>'+ret[i].name+'</a> '; } $('#download-users').append(html); }); } function get_relate_item(){ var url ="/Download/get_relate_item/id/"+g_id+'.html'; $.get(url,function(ret){ if(ret.length ==0) return; var html=''; var intro; for(i in ret){ intro =ret[i].intro; for(j in keywords){ var reg =new RegExp(keywords[j],'gmi'); intro =intro.replace(reg,'<font color=brown>'+keywords[j]+'</font>'); } html =html + '[<a href=/Download/item/id/'+ret[i].new_id+'.html>'+ret[i].name+'</a>]  '+intro+'<BR>'; } $('#relate-items').append(html); }); } function get_score_name(score){ switch(score){ case '100': return '很好,推荐下载'; case '85': return '还不错'; case '75': return '一般,勉强可用'; case '50': return '差'; case '3': return '纯粹是垃圾'; case '40': return '和说明完全不符'; case '20': return '文件不全'; case '10': return '不是源代码或资料'; case '5': return '文件有密码,不知道密码'; case '0': return '不能解压或下载失败'; } return ''; } function get_comment(){ var url ="/Download/get_comments/id/"+g_id+'.html'; $.get(url,function(ret){ if(ret.length ==0) return; var total_count =ret.total_count; var data =ret.data; var html=''; for(i in data){ html =html + '<a href="/User/profile/id/'+data[i].user_new_id+'.html" class="uploader">'+data[i].user_name+'</a>: <span class="comment-score">'+get_score_name(data[i].score)+'</span>, '+data[i].content+'<BR>'; } $('#file-comments').append(html); }); } function get_favor(){ var url ="/Download/get_item_favors/id/"+g_id+".html"; $.get(url,function(ret){ if(ret.length ==0) return; var html=''; for(i in ret){ html =html + '<a href=/User/profile/id/'+ret[i].user_new_id+'.html class=user>'+ret[i].name+'</a> '; } $('#favor-users').html(html); }); } // 得到下载这个的用户又下载了什么 function get_more_download(){ } // 得到下载这个的用户又搜索了什么 function get_more_keyword(){ } // 得到论坛相关问题 function get_bbs(){ } // 得到软件工场相关内容 function get_works(){ } // 得到相关聊天室 function get_chat(){ } // 得到相关软件商城信息 function get_shop(){ } // 得到job // 得到学习内容 // 数据 get_download_user(); //get_relate_item(); get_comment(); get_favor(); $('.vote-up').click(function(){ var url="/Download/vote/t/up/id/"+g_id; $.get(url,function(ret){ if(ret.status==0){ alert(ret.info); if(ret.url.length >0) location.href=ret.url; } else{ var s =$('#vote-up-count').html(); if(s =='') s="0"; var count =parseInt(s)+1; $('#vote-up-count').html(count); } }) return false; }); $('.vote-down').click(function(){ var url="/Download/vote/t/down/id/"+g_id; $.get(url,function(ret){ if(ret.status==0) alert(ret.info); else{ var s =$('#vote-down-count').html(); if(s =='') s="0"; var count =parseInt(s)+1; $('#vote-down-count').html(count); } }) return false; }); $('.favor-item').click(function(){ var url="/Favor/add/t/0/id/"+g_id; $.get(url,function(ret){ if(ret.status==0){ alert(ret.info); } else{ var s =$('#favor-count').html(); if(s =='') s="0"; var count =parseInt(s)+1; $('#favor-count').html(count); } }) return false; }); $('.pop-a').click(function(){ var url=$(this).attr('href'); $.get(url,function(ret){ alert(ret.info); //location.reload(); }) return false; }); document.getElementById('readme').innerHTML =marked.parse(document.getElementById('readme').innerHTML); </script> </html>