editorui.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. //ui跟编辑器的适配層
  2. //那个按钮弹出是dialog,是下拉筐等都是在这个js中配置
  3. //自己写的ui也要在这里配置,放到baidu.editor.ui下边,当编辑器实例化的时候会根据ueditor.config中的toolbars找到相应的进行实例化
  4. (function () {
  5. var utils = baidu.editor.utils;
  6. var editorui = baidu.editor.ui;
  7. var _Dialog = editorui.Dialog;
  8. editorui.buttons = {};
  9. editorui.Dialog = function (options) {
  10. var dialog = new _Dialog(options);
  11. dialog.addListener('hide', function () {
  12. if (dialog.editor) {
  13. var editor = dialog.editor;
  14. try {
  15. if (browser.gecko) {
  16. var y = editor.window.scrollY,
  17. x = editor.window.scrollX;
  18. editor.body.focus();
  19. editor.window.scrollTo(x, y);
  20. } else {
  21. editor.focus();
  22. }
  23. } catch (ex) {
  24. }
  25. }
  26. });
  27. return dialog;
  28. };
  29. var iframeUrlMap = {
  30. 'anchor':'~/dialogs/anchor/anchor.html',
  31. 'insertimage':'~/dialogs/image/image.html',
  32. 'link':'~/dialogs/link/link.html',
  33. 'spechars':'~/dialogs/spechars/spechars.html',
  34. 'searchreplace':'~/dialogs/searchreplace/searchreplace.html',
  35. 'map':'~/dialogs/map/map.html',
  36. 'gmap':'~/dialogs/gmap/gmap.html',
  37. 'insertvideo':'~/dialogs/video/video.html',
  38. 'help':'~/dialogs/help/help.html',
  39. 'preview':'~/dialogs/preview/preview.html',
  40. 'emotion':'~/dialogs/emotion/emotion.html',
  41. 'wordimage':'~/dialogs/wordimage/wordimage.html',
  42. 'attachment':'~/dialogs/attachment/attachment.html',
  43. 'insertframe':'~/dialogs/insertframe/insertframe.html',
  44. 'edittip':'~/dialogs/table/edittip.html',
  45. 'edittable':'~/dialogs/table/edittable.html',
  46. 'edittd':'~/dialogs/table/edittd.html',
  47. 'webapp':'~/dialogs/webapp/webapp.html',
  48. 'snapscreen':'~/dialogs/snapscreen/snapscreen.html',
  49. 'scrawl':'~/dialogs/scrawl/scrawl.html',
  50. 'music':'~/dialogs/music/music.html',
  51. 'template':'~/dialogs/template/template.html',
  52. 'background':'~/dialogs/background/background.html',
  53. 'charts': '~/dialogs/charts/charts.html',
  54. 'subtitle': '~/dialogs/subtitle/subtitle.html'
  55. };
  56. //为工具栏添加按钮,以下都是统一的按钮触发命令,所以写在一起
  57. var btnCmds = ['undo', 'redo', 'formatmatch',
  58. 'bold', 'italic', 'underline', 'fontborder', 'touppercase', 'tolowercase',
  59. 'strikethrough', 'subscript', 'superscript', 'source', 'indent', 'outdent',
  60. 'blockquote', 'pasteplain', 'pagebreak',
  61. 'selectall', 'print','horizontal', 'removeformat', 'time', 'date', 'unlink',
  62. 'insertparagraphbeforetable', 'insertrow', 'insertcol', 'mergeright', 'mergedown', 'deleterow',
  63. 'deletecol', 'splittorows', 'splittocols', 'splittocells', 'mergecells', 'deletetable', 'drafts'];
  64. for (var i = 0, ci; ci = btnCmds[i++];) {
  65. ci = ci.toLowerCase();
  66. editorui[ci] = function (cmd) {
  67. return function (editor) {
  68. var ui = new editorui.Button({
  69. className:'edui-for-' + cmd,
  70. title:editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd) || '',
  71. onclick:function () {
  72. editor.execCommand(cmd);
  73. },
  74. theme:editor.options.theme,
  75. showText:false
  76. });
  77. editorui.buttons[cmd] = ui;
  78. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  79. var state = editor.queryCommandState(cmd);
  80. if (state == -1) {
  81. ui.setDisabled(true);
  82. ui.setChecked(false);
  83. } else {
  84. if (!uiReady) {
  85. ui.setDisabled(false);
  86. ui.setChecked(state);
  87. }
  88. }
  89. });
  90. return ui;
  91. };
  92. }(ci);
  93. }
  94. //清除文档
  95. editorui.cleardoc = function (editor) {
  96. var ui = new editorui.Button({
  97. className:'edui-for-cleardoc',
  98. title:editor.options.labelMap.cleardoc || editor.getLang("labelMap.cleardoc") || '',
  99. theme:editor.options.theme,
  100. onclick:function () {
  101. if (confirm(editor.getLang("confirmClear"))) {
  102. editor.execCommand('cleardoc');
  103. }
  104. }
  105. });
  106. editorui.buttons["cleardoc"] = ui;
  107. editor.addListener('selectionchange', function () {
  108. ui.setDisabled(editor.queryCommandState('cleardoc') == -1);
  109. });
  110. return ui;
  111. };
  112. //排版,图片排版,文字方向
  113. var typeset = {
  114. 'justify':['left', 'right', 'center', 'justify'],
  115. 'imagefloat':['none', 'left', 'center', 'right'],
  116. 'directionality':['ltr', 'rtl']
  117. };
  118. for (var p in typeset) {
  119. (function (cmd, val) {
  120. for (var i = 0, ci; ci = val[i++];) {
  121. (function (cmd2) {
  122. editorui[cmd.replace('float', '') + cmd2] = function (editor) {
  123. var ui = new editorui.Button({
  124. className:'edui-for-' + cmd.replace('float', '') + cmd2,
  125. title:editor.options.labelMap[cmd.replace('float', '') + cmd2] || editor.getLang("labelMap." + cmd.replace('float', '') + cmd2) || '',
  126. theme:editor.options.theme,
  127. onclick:function () {
  128. editor.execCommand(cmd, cmd2);
  129. }
  130. });
  131. editorui.buttons[cmd] = ui;
  132. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  133. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  134. ui.setChecked(editor.queryCommandValue(cmd) == cmd2 && !uiReady);
  135. });
  136. return ui;
  137. };
  138. })(ci)
  139. }
  140. })(p, typeset[p])
  141. }
  142. //字体颜色和背景颜色
  143. for (var i = 0, ci; ci = ['backcolor', 'forecolor'][i++];) {
  144. editorui[ci] = function (cmd) {
  145. return function (editor) {
  146. var ui = new editorui.ColorButton({
  147. className:'edui-for-' + cmd,
  148. color:'default',
  149. title:editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd) || '',
  150. editor:editor,
  151. onpickcolor:function (t, color) {
  152. editor.execCommand(cmd, color);
  153. },
  154. onpicknocolor:function () {
  155. editor.execCommand(cmd, 'default');
  156. this.setColor('transparent');
  157. this.color = 'default';
  158. },
  159. onbuttonclick:function () {
  160. editor.execCommand(cmd, this.color);
  161. }
  162. });
  163. editorui.buttons[cmd] = ui;
  164. editor.addListener('selectionchange', function () {
  165. ui.setDisabled(editor.queryCommandState(cmd) == -1);
  166. });
  167. return ui;
  168. };
  169. }(ci);
  170. }
  171. var dialogBtns = {
  172. noOk:['searchreplace', 'help', 'spechars', 'webapp','preview'],
  173. ok:['attachment', 'anchor', 'link', 'insertimage', 'map', 'gmap', 'insertframe', 'wordimage',
  174. 'insertvideo', 'insertframe', 'edittip', 'edittable', 'edittd', 'scrawl', 'template', 'music', 'background', 'charts', 'subtitle']
  175. };
  176. for (var p in dialogBtns) {
  177. (function (type, vals) {
  178. for (var i = 0, ci; ci = vals[i++];) {
  179. //todo opera下存在问题
  180. if (browser.opera && ci === "searchreplace") {
  181. continue;
  182. }
  183. (function (cmd) {
  184. editorui[cmd] = function (editor, iframeUrl, title) {
  185. iframeUrl = iframeUrl || (editor.options.iframeUrlMap || {})[cmd] || iframeUrlMap[cmd];
  186. title = editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd) || '';
  187. var dialog;
  188. //没有iframeUrl不创建dialog
  189. if (iframeUrl) {
  190. dialog = new editorui.Dialog(utils.extend({
  191. iframeUrl:editor.ui.mapUrl(iframeUrl),
  192. editor:editor,
  193. className:'edui-for-' + cmd,
  194. title:title,
  195. holdScroll: cmd === 'insertimage',
  196. fullscreen: /charts|preview/.test(cmd),
  197. closeDialog:editor.getLang("closeDialog")
  198. }, type == 'ok' ? {
  199. buttons:[
  200. {
  201. className:'edui-okbutton',
  202. label:editor.getLang("ok"),
  203. editor:editor,
  204. onclick:function () {
  205. dialog.close(true);
  206. }
  207. },
  208. {
  209. className:'edui-cancelbutton',
  210. label:editor.getLang("cancel"),
  211. editor:editor,
  212. onclick:function () {
  213. dialog.close(false);
  214. }
  215. }
  216. ]
  217. } : {}));
  218. editor.ui._dialogs[cmd + "Dialog"] = dialog;
  219. }
  220. var ui = new editorui.Button({
  221. className:'edui-for-' + cmd,
  222. title:title,
  223. onclick:function () {
  224. if (dialog) {
  225. switch (cmd) {
  226. case "wordimage":
  227. var images = editor.execCommand("wordimage");
  228. if (images && images.length) {
  229. dialog.render();
  230. dialog.open();
  231. }
  232. break;
  233. case "scrawl":
  234. if (editor.queryCommandState("scrawl") != -1) {
  235. dialog.render();
  236. dialog.open();
  237. }
  238. break;
  239. default:
  240. dialog.render();
  241. dialog.open();
  242. }
  243. }
  244. },
  245. theme:editor.options.theme,
  246. disabled:(cmd == 'scrawl' && editor.queryCommandState("scrawl") == -1) || ( cmd == 'charts' )
  247. });
  248. editorui.buttons[cmd] = ui;
  249. editor.addListener('selectionchange', function () {
  250. //只存在于右键菜单而无工具栏按钮的ui不需要检测状态
  251. var unNeedCheckState = {'edittable':1};
  252. if (cmd in unNeedCheckState)return;
  253. var state = editor.queryCommandState(cmd);
  254. if (ui.getDom()) {
  255. ui.setDisabled(state == -1);
  256. ui.setChecked(state);
  257. }
  258. });
  259. return ui;
  260. };
  261. })(ci.toLowerCase())
  262. }
  263. })(p, dialogBtns[p]);
  264. }
  265. editorui.snapscreen = function (editor, iframeUrl, title) {
  266. title = editor.options.labelMap['snapscreen'] || editor.getLang("labelMap.snapscreen") || '';
  267. var ui = new editorui.Button({
  268. className:'edui-for-snapscreen',
  269. title:title,
  270. onclick:function () {
  271. editor.execCommand("snapscreen");
  272. },
  273. theme:editor.options.theme
  274. });
  275. editorui.buttons['snapscreen'] = ui;
  276. iframeUrl = iframeUrl || (editor.options.iframeUrlMap || {})["snapscreen"] || iframeUrlMap["snapscreen"];
  277. if (iframeUrl) {
  278. var dialog = new editorui.Dialog({
  279. iframeUrl:editor.ui.mapUrl(iframeUrl),
  280. editor:editor,
  281. className:'edui-for-snapscreen',
  282. title:title,
  283. buttons:[
  284. {
  285. className:'edui-okbutton',
  286. label:editor.getLang("ok"),
  287. editor:editor,
  288. onclick:function () {
  289. dialog.close(true);
  290. }
  291. },
  292. {
  293. className:'edui-cancelbutton',
  294. label:editor.getLang("cancel"),
  295. editor:editor,
  296. onclick:function () {
  297. dialog.close(false);
  298. }
  299. }
  300. ]
  301. });
  302. dialog.render();
  303. editor.ui._dialogs["snapscreenDialog"] = dialog;
  304. }
  305. editor.addListener('selectionchange', function () {
  306. ui.setDisabled(editor.queryCommandState('snapscreen') == -1);
  307. });
  308. return ui;
  309. };
  310. editorui.insertcode = function (editor, list, title) {
  311. list = editor.options['insertcode'] || [];
  312. title = editor.options.labelMap['insertcode'] || editor.getLang("labelMap.insertcode") || '';
  313. // if (!list.length) return;
  314. var items = [];
  315. utils.each(list,function(key,val){
  316. items.push({
  317. label:key,
  318. value:val,
  319. theme:editor.options.theme,
  320. renderLabelHtml:function () {
  321. return '<div class="edui-label %%-label" >' + (this.label || '') + '</div>';
  322. }
  323. });
  324. });
  325. var ui = new editorui.Combox({
  326. editor:editor,
  327. items:items,
  328. onselect:function (t, index) {
  329. editor.execCommand('insertcode', this.items[index].value);
  330. },
  331. onbuttonclick:function () {
  332. this.showPopup();
  333. },
  334. title:title,
  335. initValue:title,
  336. className:'edui-for-insertcode',
  337. indexByValue:function (value) {
  338. if (value) {
  339. for (var i = 0, ci; ci = this.items[i]; i++) {
  340. if (ci.value.indexOf(value) != -1)
  341. return i;
  342. }
  343. }
  344. return -1;
  345. }
  346. });
  347. editorui.buttons['insertcode'] = ui;
  348. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  349. if (!uiReady) {
  350. var state = editor.queryCommandState('insertcode');
  351. if (state == -1) {
  352. ui.setDisabled(true);
  353. } else {
  354. ui.setDisabled(false);
  355. var value = editor.queryCommandValue('insertcode');
  356. if(!value){
  357. ui.setValue(title);
  358. return;
  359. }
  360. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  361. value && (value = value.replace(/['"]/g, '').split(',')[0]);
  362. ui.setValue(value);
  363. }
  364. }
  365. });
  366. return ui;
  367. };
  368. editorui.fontfamily = function (editor, list, title) {
  369. list = editor.options['fontfamily'] || [];
  370. title = editor.options.labelMap['fontfamily'] || editor.getLang("labelMap.fontfamily") || '';
  371. if (!list.length) return;
  372. for (var i = 0, ci, items = []; ci = list[i]; i++) {
  373. var langLabel = editor.getLang('fontfamily')[ci.name] || "";
  374. (function (key, val) {
  375. items.push({
  376. label:key,
  377. value:val,
  378. theme:editor.options.theme,
  379. renderLabelHtml:function () {
  380. return '<div class="edui-label %%-label" style="font-family:' +
  381. utils.unhtml(this.value) + '">' + (this.label || '') + '</div>';
  382. }
  383. });
  384. })(ci.label || langLabel, ci.val)
  385. }
  386. var ui = new editorui.Combox({
  387. editor:editor,
  388. items:items,
  389. onselect:function (t, index) {
  390. editor.execCommand('FontFamily', this.items[index].value);
  391. },
  392. onbuttonclick:function () {
  393. this.showPopup();
  394. },
  395. title:title,
  396. initValue:title,
  397. className:'edui-for-fontfamily',
  398. indexByValue:function (value) {
  399. if (value) {
  400. for (var i = 0, ci; ci = this.items[i]; i++) {
  401. if (ci.value.indexOf(value) != -1)
  402. return i;
  403. }
  404. }
  405. return -1;
  406. }
  407. });
  408. editorui.buttons['fontfamily'] = ui;
  409. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  410. if (!uiReady) {
  411. var state = editor.queryCommandState('FontFamily');
  412. if (state == -1) {
  413. ui.setDisabled(true);
  414. } else {
  415. ui.setDisabled(false);
  416. var value = editor.queryCommandValue('FontFamily');
  417. //trace:1871 ie下从源码模式切换回来时,字体会带单引号,而且会有逗号
  418. value && (value = value.replace(/['"]/g, '').split(',')[0]);
  419. ui.setValue(value);
  420. }
  421. }
  422. });
  423. return ui;
  424. };
  425. editorui.fontsize = function (editor, list, title) {
  426. title = editor.options.labelMap['fontsize'] || editor.getLang("labelMap.fontsize") || '';
  427. list = list || editor.options['fontsize'] || [];
  428. if (!list.length) return;
  429. var items = [];
  430. for (var i = 0; i < list.length; i++) {
  431. var size = list[i] + 'px';
  432. items.push({
  433. label:size,
  434. value:size,
  435. theme:editor.options.theme,
  436. renderLabelHtml:function () {
  437. return '<div class="edui-label %%-label" style="line-height:1;font-size:' +
  438. this.value + '">' + (this.label || '') + '</div>';
  439. }
  440. });
  441. }
  442. var ui = new editorui.Combox({
  443. editor:editor,
  444. items:items,
  445. title:title,
  446. initValue:title,
  447. onselect:function (t, index) {
  448. editor.execCommand('FontSize', this.items[index].value);
  449. },
  450. onbuttonclick:function () {
  451. this.showPopup();
  452. },
  453. className:'edui-for-fontsize'
  454. });
  455. editorui.buttons['fontsize'] = ui;
  456. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  457. if (!uiReady) {
  458. var state = editor.queryCommandState('FontSize');
  459. if (state == -1) {
  460. ui.setDisabled(true);
  461. } else {
  462. ui.setDisabled(false);
  463. ui.setValue(editor.queryCommandValue('FontSize'));
  464. }
  465. }
  466. });
  467. return ui;
  468. };
  469. editorui.paragraph = function (editor, list, title) {
  470. title = editor.options.labelMap['paragraph'] || editor.getLang("labelMap.paragraph") || '';
  471. list = editor.options['paragraph'] || [];
  472. if (utils.isEmptyObject(list)) return;
  473. var items = [];
  474. for (var i in list) {
  475. items.push({
  476. value:i,
  477. label:list[i] || editor.getLang("paragraph")[i],
  478. theme:editor.options.theme,
  479. renderLabelHtml:function () {
  480. return '<div class="edui-label %%-label"><span class="edui-for-' + this.value + '">' + (this.label || '') + '</span></div>';
  481. }
  482. })
  483. }
  484. var ui = new editorui.Combox({
  485. editor:editor,
  486. items:items,
  487. title:title,
  488. initValue:title,
  489. className:'edui-for-paragraph',
  490. onselect:function (t, index) {
  491. editor.execCommand('Paragraph', this.items[index].value);
  492. },
  493. onbuttonclick:function () {
  494. this.showPopup();
  495. }
  496. });
  497. editorui.buttons['paragraph'] = ui;
  498. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  499. if (!uiReady) {
  500. var state = editor.queryCommandState('Paragraph');
  501. if (state == -1) {
  502. ui.setDisabled(true);
  503. } else {
  504. ui.setDisabled(false);
  505. var value = editor.queryCommandValue('Paragraph');
  506. var index = ui.indexByValue(value);
  507. if (index != -1) {
  508. ui.setValue(value);
  509. } else {
  510. ui.setValue(ui.initValue);
  511. }
  512. }
  513. }
  514. });
  515. return ui;
  516. };
  517. //自定义标题
  518. editorui.customstyle = function (editor) {
  519. var list = editor.options['customstyle'] || [],
  520. title = editor.options.labelMap['customstyle'] || editor.getLang("labelMap.customstyle") || '';
  521. if (!list.length)return;
  522. var langCs = editor.getLang('customstyle');
  523. for (var i = 0, items = [], t; t = list[i++];) {
  524. (function (t) {
  525. var ck = {};
  526. ck.label = t.label ? t.label : langCs[t.name];
  527. ck.style = t.style;
  528. ck.className = t.className;
  529. ck.tag = t.tag;
  530. items.push({
  531. label:ck.label,
  532. value:ck,
  533. theme:editor.options.theme,
  534. renderLabelHtml:function () {
  535. return '<div class="edui-label %%-label">' + '<' + ck.tag + ' ' + (ck.className ? ' class="' + ck.className + '"' : "")
  536. + (ck.style ? ' style="' + ck.style + '"' : "") + '>' + ck.label + "<\/" + ck.tag + ">"
  537. + '</div>';
  538. }
  539. });
  540. })(t);
  541. }
  542. var ui = new editorui.Combox({
  543. editor:editor,
  544. items:items,
  545. title:title,
  546. initValue:title,
  547. className:'edui-for-customstyle',
  548. onselect:function (t, index) {
  549. editor.execCommand('customstyle', this.items[index].value);
  550. },
  551. onbuttonclick:function () {
  552. this.showPopup();
  553. },
  554. indexByValue:function (value) {
  555. for (var i = 0, ti; ti = this.items[i++];) {
  556. if (ti.label == value) {
  557. return i - 1
  558. }
  559. }
  560. return -1;
  561. }
  562. });
  563. editorui.buttons['customstyle'] = ui;
  564. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  565. if (!uiReady) {
  566. var state = editor.queryCommandState('customstyle');
  567. if (state == -1) {
  568. ui.setDisabled(true);
  569. } else {
  570. ui.setDisabled(false);
  571. var value = editor.queryCommandValue('customstyle');
  572. var index = ui.indexByValue(value);
  573. if (index != -1) {
  574. ui.setValue(value);
  575. } else {
  576. ui.setValue(ui.initValue);
  577. }
  578. }
  579. }
  580. });
  581. return ui;
  582. };
  583. editorui.inserttable = function (editor, iframeUrl, title) {
  584. title = editor.options.labelMap['inserttable'] || editor.getLang("labelMap.inserttable") || '';
  585. var ui = new editorui.TableButton({
  586. editor:editor,
  587. title:title,
  588. className:'edui-for-inserttable',
  589. onpicktable:function (t, numCols, numRows) {
  590. editor.execCommand('InsertTable', {numRows:numRows, numCols:numCols, border:1});
  591. },
  592. onbuttonclick:function () {
  593. this.showPopup();
  594. }
  595. });
  596. editorui.buttons['inserttable'] = ui;
  597. editor.addListener('selectionchange', function () {
  598. ui.setDisabled(editor.queryCommandState('inserttable') == -1);
  599. });
  600. return ui;
  601. };
  602. editorui.lineheight = function (editor) {
  603. var val = editor.options.lineheight || [];
  604. if (!val.length)return;
  605. for (var i = 0, ci, items = []; ci = val[i++];) {
  606. items.push({
  607. //todo:写死了
  608. label:ci,
  609. value:ci,
  610. theme:editor.options.theme,
  611. onclick:function () {
  612. editor.execCommand("lineheight", this.value);
  613. }
  614. })
  615. }
  616. var ui = new editorui.MenuButton({
  617. editor:editor,
  618. className:'edui-for-lineheight',
  619. title:editor.options.labelMap['lineheight'] || editor.getLang("labelMap.lineheight") || '',
  620. items:items,
  621. onbuttonclick:function () {
  622. var value = editor.queryCommandValue('LineHeight') || this.value;
  623. editor.execCommand("LineHeight", value);
  624. }
  625. });
  626. editorui.buttons['lineheight'] = ui;
  627. editor.addListener('selectionchange', function () {
  628. var state = editor.queryCommandState('LineHeight');
  629. if (state == -1) {
  630. ui.setDisabled(true);
  631. } else {
  632. ui.setDisabled(false);
  633. var value = editor.queryCommandValue('LineHeight');
  634. value && ui.setValue((value + '').replace(/cm/, ''));
  635. ui.setChecked(state)
  636. }
  637. });
  638. return ui;
  639. };
  640. var rowspacings = ['top', 'bottom'];
  641. for (var r = 0, ri; ri = rowspacings[r++];) {
  642. (function (cmd) {
  643. editorui['rowspacing' + cmd] = function (editor) {
  644. var val = editor.options['rowspacing' + cmd] || [];
  645. if (!val.length) return null;
  646. for (var i = 0, ci, items = []; ci = val[i++];) {
  647. items.push({
  648. label:ci,
  649. value:ci,
  650. theme:editor.options.theme,
  651. onclick:function () {
  652. editor.execCommand("rowspacing", this.value, cmd);
  653. }
  654. })
  655. }
  656. var ui = new editorui.MenuButton({
  657. editor:editor,
  658. className:'edui-for-rowspacing' + cmd,
  659. title:editor.options.labelMap['rowspacing' + cmd] || editor.getLang("labelMap.rowspacing" + cmd) || '',
  660. items:items,
  661. onbuttonclick:function () {
  662. var value = editor.queryCommandValue('rowspacing', cmd) || this.value;
  663. editor.execCommand("rowspacing", value, cmd);
  664. }
  665. });
  666. editorui.buttons[cmd] = ui;
  667. editor.addListener('selectionchange', function () {
  668. var state = editor.queryCommandState('rowspacing', cmd);
  669. if (state == -1) {
  670. ui.setDisabled(true);
  671. } else {
  672. ui.setDisabled(false);
  673. var value = editor.queryCommandValue('rowspacing', cmd);
  674. value && ui.setValue((value + '').replace(/%/, ''));
  675. ui.setChecked(state)
  676. }
  677. });
  678. return ui;
  679. }
  680. })(ri)
  681. }
  682. //有序,无序列表
  683. var lists = ['insertorderedlist', 'insertunorderedlist'];
  684. for (var l = 0, cl; cl = lists[l++];) {
  685. (function (cmd) {
  686. editorui[cmd] = function (editor) {
  687. var vals = editor.options[cmd],
  688. _onMenuClick = function () {
  689. editor.execCommand(cmd, this.value);
  690. }, items = [];
  691. for (var i in vals) {
  692. items.push({
  693. label:vals[i] || editor.getLang()[cmd][i] || "",
  694. value:i,
  695. theme:editor.options.theme,
  696. onclick:_onMenuClick
  697. })
  698. }
  699. var ui = new editorui.MenuButton({
  700. editor:editor,
  701. className:'edui-for-' + cmd,
  702. title:editor.getLang("labelMap." + cmd) || '',
  703. 'items':items,
  704. onbuttonclick:function () {
  705. var value = editor.queryCommandValue(cmd) || this.value;
  706. editor.execCommand(cmd, value);
  707. }
  708. });
  709. editorui.buttons[cmd] = ui;
  710. editor.addListener('selectionchange', function () {
  711. var state = editor.queryCommandState(cmd);
  712. if (state == -1) {
  713. ui.setDisabled(true);
  714. } else {
  715. ui.setDisabled(false);
  716. var value = editor.queryCommandValue(cmd);
  717. ui.setValue(value);
  718. ui.setChecked(state)
  719. }
  720. });
  721. return ui;
  722. };
  723. })(cl)
  724. }
  725. editorui.fullscreen = function (editor, title) {
  726. title = editor.options.labelMap['fullscreen'] || editor.getLang("labelMap.fullscreen") || '';
  727. var ui = new editorui.Button({
  728. className:'edui-for-fullscreen',
  729. title:title,
  730. theme:editor.options.theme,
  731. onclick:function () {
  732. if (editor.ui) {
  733. editor.ui.setFullScreen(!editor.ui.isFullScreen());
  734. }
  735. this.setChecked(editor.ui.isFullScreen());
  736. }
  737. });
  738. editorui.buttons['fullscreen'] = ui;
  739. editor.addListener('selectionchange', function () {
  740. var state = editor.queryCommandState('fullscreen');
  741. ui.setDisabled(state == -1);
  742. ui.setChecked(editor.ui.isFullScreen());
  743. });
  744. return ui;
  745. };
  746. // 表情
  747. editorui["emotion"] = function (editor, iframeUrl) {
  748. var cmd = "emotion";
  749. var ui = new editorui.MultiMenuPop({
  750. title:editor.options.labelMap[cmd] || editor.getLang("labelMap." + cmd + "") || '',
  751. editor:editor,
  752. className:'edui-for-' + cmd,
  753. iframeUrl:editor.ui.mapUrl(iframeUrl || (editor.options.iframeUrlMap || {})[cmd] || iframeUrlMap[cmd])
  754. });
  755. editorui.buttons[cmd] = ui;
  756. editor.addListener('selectionchange', function () {
  757. ui.setDisabled(editor.queryCommandState(cmd) == -1)
  758. });
  759. return ui;
  760. };
  761. editorui.autotypeset = function (editor) {
  762. var ui = new editorui.AutoTypeSetButton({
  763. editor:editor,
  764. title:editor.options.labelMap['autotypeset'] || editor.getLang("labelMap.autotypeset") || '',
  765. className:'edui-for-autotypeset',
  766. onbuttonclick:function () {
  767. editor.execCommand('autotypeset')
  768. }
  769. });
  770. editorui.buttons['autotypeset'] = ui;
  771. editor.addListener('selectionchange', function () {
  772. ui.setDisabled(editor.queryCommandState('autotypeset') == -1);
  773. });
  774. return ui;
  775. };
  776. /* 简单上传插件 */
  777. editorui["simpleupload"] = function (editor) {
  778. var name = 'simpleupload',
  779. ui = new editorui.Button({
  780. className:'edui-for-' + name,
  781. title:editor.options.labelMap[name] || editor.getLang("labelMap." + name) || '',
  782. onclick:function () {},
  783. theme:editor.options.theme,
  784. showText:false
  785. });
  786. editorui.buttons[name] = ui;
  787. editor.addListener('ready', function() {
  788. var b = ui.getDom('body'),
  789. iconSpan = b.children[0];
  790. editor.fireEvent('simpleuploadbtnready', iconSpan);
  791. });
  792. editor.addListener('selectionchange', function (type, causeByUi, uiReady) {
  793. var state = editor.queryCommandState(name);
  794. if (state == -1) {
  795. ui.setDisabled(true);
  796. ui.setChecked(false);
  797. } else {
  798. if (!uiReady) {
  799. ui.setDisabled(false);
  800. ui.setChecked(state);
  801. }
  802. }
  803. });
  804. return ui;
  805. };
  806. })();