autosave.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. module('plugins.autosave');
  2. test('自动保存', function () {
  3. expect(8);
  4. var container = te.obj[0].container,
  5. editor = null,
  6. count = 0;
  7. UE.delEditor(te.obj[0]);
  8. container.parentNode.removeChild(container);
  9. container = document.createElement("div");
  10. container.id = "container";
  11. document.body.appendChild(container);
  12. editor = UE.getEditor("container", {
  13. initialContent: "",
  14. //无限制
  15. saveInterval: 0
  16. });
  17. editor.addListener("beforeautosave", function (type, data) {
  18. data.content = data.content.toLowerCase();
  19. equal(true, true, "成功触发beforeautosave事件");
  20. equal(data.content === "<p>http://www.baidu.com</p>" || data.content === "<p>disable</p>", true, "事件携带数据正确");
  21. });
  22. editor.addListener("beforeautosave", function (type, data) {
  23. data.content = data.content.toLowerCase();
  24. if (data.content === "<p>disable</p>") {
  25. return false;
  26. }
  27. count++;
  28. });
  29. editor.addListener("afterautosave", function (type, data) {
  30. data.content = data.content.toLowerCase();
  31. equal(data.content, "<p>http://www.baidu.com</p>", "成功触发afterautosave事件");
  32. equal(editor.execCommand("getlocaldata") !== null, true, "getlocaldata命令正常");
  33. editor.execCommand("clearlocaldata");
  34. equal(editor.execCommand("getlocaldata") === "", true, "clearlocaldata命令正常");
  35. });
  36. stop();
  37. window.setTimeout(function () {
  38. editor.setContent('<p>disable</p>');
  39. window.setTimeout(function () {
  40. editor.setContent('<p>http://www.baidu.com</p>');
  41. window.setTimeout(function () {
  42. equal(count, 1, "触发事件次数");
  43. start();
  44. }, 500);
  45. UE.delEditor("container");
  46. container.parentNode.removeChild(container);
  47. }, 50);
  48. }, 500);
  49. });
  50. test('重建编辑器,加载草稿箱', function () {
  51. UE.delEditor(te.obj[0]);
  52. te.obj[0].container.parentNode.removeChild(te.obj[0].container);
  53. var div = document.body.appendChild(document.createElement('div'));
  54. div.id = 'ue';
  55. var editor = UE.getEditor('ue', {saveInterval: 0});
  56. setTimeout(function () {
  57. var content = '<p>内容</p>';
  58. editor.setContent(content);
  59. setTimeout(function () {
  60. UE.delEditor('ue');
  61. document.getElementById('ue') && document.getElementById('ue').parentNode.removeChild(document.getElementById('ue'));
  62. var div = document.body.appendChild(document.createElement('div'));
  63. div.id = 'ue';
  64. var editor2 = UE.getEditor('ue');
  65. setTimeout(function () {
  66. equal(editor2.queryCommandState('drafts'), 0, '草稿箱可用');
  67. editor2.execCommand('drafts');
  68. ua.checkSameHtml(editor2.body.innerHTML, content, '内容加载正确');
  69. UE.delEditor('ue');
  70. document.getElementById('ue') && document.getElementById('ue').parentNode.removeChild(document.getElementById('ue'));
  71. start();
  72. }, 500);
  73. }, 200);
  74. }, 500);
  75. stop();
  76. });