nicedit.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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) {
  32. if (typeof params != 'undefined') {
  33. var instance = nicEditors.findEditor(params.field);
  34. if (instance) {
  35. instance.ne.removeInstance(params.field);
  36. instance.ne.removePanel();
  37. }
  38. }
  39. else {
  40. for (var e in nicEditors.editors) {
  41. // Save contents of all editors back into textareas.
  42. var instances = nicEditors.editors[e].nicInstances;
  43. for (var i = 0; i < instances.length; i++) {
  44. instances[i].remove();
  45. }
  46. // Remove all editor instances.
  47. nicEditors.editors[e].nicInstances = [];
  48. }
  49. }
  50. };
  51. /**
  52. * Instance methods for nicEdit.
  53. */
  54. Drupal.wysiwyg.editor.instance.nicedit = {
  55. insert: function (content) {
  56. var instance = nicEditors.findEditor(this.field);
  57. var editingArea = instance.getElm();
  58. var sel = instance.getSel();
  59. // IE.
  60. if (document.selection) {
  61. editingArea.focus();
  62. sel.createRange().text = content;
  63. }
  64. else {
  65. // Convert selection to a range.
  66. var range;
  67. // W3C compatible.
  68. if (sel.getRangeAt) {
  69. range = sel.getRangeAt(0);
  70. }
  71. // Safari.
  72. else {
  73. range = editingArea.ownerDocument.createRange();
  74. range.setStart(sel.anchorNode, sel.anchorOffset);
  75. range.setEnd(sel.focusNode, userSeletion.focusOffset);
  76. }
  77. // The code below doesn't work in IE, but it never gets here.
  78. var fragment = editingArea.ownerDocument.createDocumentFragment();
  79. // Fragments don't support innerHTML.
  80. var wrapper = editingArea.ownerDocument.createElement('div');
  81. wrapper.innerHTML = content;
  82. while (wrapper.firstChild) {
  83. fragment.appendChild(wrapper.firstChild);
  84. }
  85. range.deleteContents();
  86. // Only fragment children are inserted.
  87. range.insertNode(fragment);
  88. }
  89. }
  90. };
  91. })(jQuery);