PrettyPrint.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.editor.plugins.PrettyPrint"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.editor.plugins.PrettyPrint"] = true;
  8. dojo.provide("dojox.editor.plugins.PrettyPrint");
  9. dojo.require("dijit._editor._Plugin");
  10. dojo.require("dojox.html.format");
  11. dojo.declare("dojox.editor.plugins.PrettyPrint",dijit._editor._Plugin,{
  12. // summary:
  13. // This plugin provides a mechanism by which to 'beautify HTML'
  14. // generated by the editor. It is by no means perfect.
  15. // indentBy [public] Integer
  16. // The indentBy property configures if the plugin should use a
  17. // set number of spaces for indent (between 1-5), or just tab.
  18. // The default value is -1, which means tab.
  19. indentBy: -1,
  20. // lineLength [public] Integer
  21. // The lineLength property configures if the plugin should break up long
  22. // text lines into N lines of 'lineLength' length. This parameter does not
  23. // take into account indention depth, only text line length. The default is -1
  24. // which means unlimited line length.
  25. lineLength: -1,
  26. // useDefaultCommand [protected]
  27. // Over-ride indicating that the command processing is done all by this plugin.
  28. useDefaultCommand: false,
  29. // map [public] Array
  30. // An array of arrays that define out entity character to encoding mappings.
  31. // see the dojox.html.entities definitions for more details. The default is
  32. // HTML + cent, pound, yen, ellipsis, copyright, registered trademark.
  33. entityMap: null,
  34. // xhtml: [public] boolean
  35. // Flag to denote that the PrettyPrint plugin try to generate XHTML compliant
  36. // markup.
  37. _initButton: function(){
  38. // summary:
  39. // Over-ride for creation of the resize button.
  40. delete this.command;
  41. },
  42. setToolbar: function(toolbar){
  43. // summary:
  44. // Over-ride to do nothing.
  45. // We don't want to append a button, we take over getValue.
  46. },
  47. setEditor: function(editor){
  48. // summary:
  49. // Over-ride to take over getValue of editor so that
  50. // we can 'pretty' the output.
  51. this.inherited(arguments);
  52. var self = this;
  53. this.editor.onLoadDeferred.addCallback(function(){
  54. self.editor._prettyprint_getValue = self.editor.getValue;
  55. self.editor.getValue = function(){
  56. var val = self.editor._prettyprint_getValue(arguments);
  57. return dojox.html.format.prettyPrint(val, self.indentBy, self.lineLength, self.entityMap, self.xhtml);
  58. };
  59. // The following are implemented as 'performance' functions. Don't prettyprint
  60. // content on internal state changes, just on calls to actually get values.
  61. self.editor._prettyprint_endEditing = self.editor._endEditing;
  62. self.editor._prettyprint_onBlur = self.editor._onBlur;
  63. self.editor._endEditing = function(ignore_caret){
  64. var v = self.editor._prettyprint_getValue(true);
  65. self.editor._undoedSteps=[];//clear undoed steps
  66. self.editor._steps.push({text: v, bookmark: self.editor._getBookmark()});
  67. }
  68. self.editor._onBlur = function(e){
  69. this.inherited("_onBlur", arguments);
  70. var _c=self.editor._prettyprint_getValue(true);
  71. if(_c!=self.editor.savedContent){
  72. self.editor.onChange(_c);
  73. self.editor.savedContent=_c;
  74. }
  75. }
  76. });
  77. }
  78. });
  79. // Register this plugin.
  80. dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
  81. if(o.plugin){ return; }
  82. var name = o.args.name.toLowerCase();
  83. if(name === "prettyprint"){
  84. o.plugin = new dojox.editor.plugins.PrettyPrint({
  85. indentBy: ("indentBy" in o.args)?o.args.indentBy:-1,
  86. lineLength: ("lineLength" in o.args)?o.args.lineLength:-1,
  87. entityMap: ("entityMap" in o.args)?o.args.entityMap:dojox.html.entities.html.concat([
  88. ["\u00A2","cent"],["\u00A3","pound"],["\u20AC","euro"],
  89. ["\u00A5","yen"],["\u00A9","copy"],["\u00A7","sect"],
  90. ["\u2026","hellip"],["\u00AE","reg"]
  91. ]),
  92. xhtml: ("xhtml" in o.args)?o.args.xhtml:false
  93. });
  94. }
  95. });
  96. }