PrettyPrint.js 3.7 KB

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