editorui.js 29 KB

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