nicedit.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. (function($) {
  2. /**
  3. * Attach this editor to a target element.
  4. */
  5. Drupal.wysiwyg.editor.attach.nicedit = function(context, params, settings) {
  6. // Intercept and ignore submit handlers or they will revert changes made
  7. // since the instance was removed. The handlers are anonymous and hidden out
  8. // of scope in a closure so we can't unbind them. The same operations are
  9. // performed when the instance is detached anyway.
  10. var oldAddEvent = bkLib.addEvent;
  11. bkLib.addEvent = function(obj, type, fn) {
  12. if (type != 'submit') {
  13. oldAddEvent(obj, type, fn);
  14. }
  15. }
  16. // Attach editor.
  17. var editor = new nicEditor(settings);
  18. editor.panelInstance(params.field);
  19. // The old addEvent() must be restored after creating a new instance, as
  20. // plugins with dialogs use it to bind submit handlers to their forms.
  21. bkLib.addEvent = oldAddEvent;
  22. editor.addEvent('focus', function () {
  23. Drupal.wysiwyg.activeId = params.field;
  24. });
  25. };
  26. /**
  27. * Detach a single or all editors.
  28. *
  29. * See Drupal.wysiwyg.editor.detach.none() for a full description of this hook.
  30. */
  31. Drupal.wysiwyg.editor.detach.nicedit = function (context, params, trigger) {
  32. if (typeof params != 'undefined') {
  33. var instance = nicEditors.findEditor(params.field);
  34. if (instance) {
  35. if (trigger == 'serialize') {
  36. instance.saveContent();
  37. }
  38. else {
  39. instance.ne.removeInstance(params.field);
  40. instance.ne.removePanel();
  41. }
  42. }
  43. }
  44. else {
  45. for (var e in nicEditors.editors) {
  46. // Save contents of all editors back into textareas.
  47. var instances = nicEditors.editors[e].nicInstances;
  48. for (var i = 0; i < instances.length; i++) {
  49. if (trigger == 'serialize') {
  50. instances[i].saveContent();
  51. }
  52. else {
  53. instances[i].remove();
  54. }
  55. }
  56. // Remove all editor instances.
  57. if (trigger != 'serialize') {
  58. nicEditors.editors[e].nicInstances = [];
  59. }
  60. }
  61. }
  62. };
  63. /**
  64. * Instance methods for nicEdit.
  65. */
  66. Drupal.wysiwyg.editor.instance.nicedit = {
  67. insert: function (content) {
  68. var instance = nicEditors.findEditor(this.field);
  69. var editingArea = instance.getElm();
  70. var sel = instance.getSel();
  71. // IE.
  72. if (document.selection) {
  73. editingArea.focus();
  74. sel.createRange().pasteHTML(content);
  75. }
  76. else {
  77. // Convert selection to a range.
  78. var range;
  79. // W3C compatible.
  80. if (sel.getRangeAt) {
  81. range = sel.getRangeAt(0);
  82. }
  83. // Safari.
  84. else {
  85. range = editingArea.ownerDocument.createRange();
  86. range.setStart(sel.anchorNode, sel.anchorOffset);
  87. range.setEnd(sel.focusNode, userSeletion.focusOffset);
  88. }
  89. // The code below doesn't work in IE, but it never gets here.
  90. var fragment = editingArea.ownerDocument.createDocumentFragment();
  91. // Fragments don't support innerHTML.
  92. var wrapper = editingArea.ownerDocument.createElement('div');
  93. wrapper.innerHTML = content;
  94. while (wrapper.firstChild) {
  95. fragment.appendChild(wrapper.firstChild);
  96. }
  97. range.deleteContents();
  98. // Only fragment children are inserted.
  99. range.insertNode(fragment);
  100. }
  101. },
  102. setContent: function (content) {
  103. nicEditors.findEditor(this.field).setContent(content);
  104. },
  105. getContent: function () {
  106. return nicEditors.findEditor(this.field).getContent();
  107. }
  108. };
  109. })(jQuery);