editor.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. ///import core
  2. ///commands 全屏
  3. ///commandsName FullScreen
  4. ///commandsTitle 全屏
  5. (function () {
  6. var utils = baidu.editor.utils,
  7. uiUtils = baidu.editor.ui.uiUtils,
  8. UIBase = baidu.editor.ui.UIBase,
  9. domUtils = baidu.editor.dom.domUtils;
  10. var nodeStack = [];
  11. function EditorUI(options) {
  12. this.initOptions(options);
  13. this.initEditorUI();
  14. }
  15. EditorUI.prototype = {
  16. uiName:'editor',
  17. initEditorUI:function () {
  18. this.editor.ui = this;
  19. this._dialogs = {};
  20. this.initUIBase();
  21. this._initToolbars();
  22. var editor = this.editor,
  23. me = this;
  24. editor.addListener('ready', function () {
  25. //提供getDialog方法
  26. editor.getDialog = function (name) {
  27. return editor.ui._dialogs[name + "Dialog"];
  28. };
  29. domUtils.on(editor.window, 'scroll', function (evt) {
  30. baidu.editor.ui.Popup.postHide(evt);
  31. });
  32. //提供编辑器实时宽高(全屏时宽高不变化)
  33. editor.ui._actualFrameWidth = editor.options.initialFrameWidth;
  34. UE.browser.ie && UE.browser.version === 6 && editor.container.ownerDocument.execCommand("BackgroundImageCache", false, true);
  35. //display bottom-bar label based on config
  36. if (editor.options.elementPathEnabled) {
  37. editor.ui.getDom('elementpath').innerHTML = '<div class="edui-editor-breadcrumb">' + editor.getLang("elementPathTip") + ':</div>';
  38. }
  39. if (editor.options.wordCount) {
  40. function countFn() {
  41. setCount(editor,me);
  42. domUtils.un(editor.document, "click", arguments.callee);
  43. }
  44. domUtils.on(editor.document, "click", countFn);
  45. editor.ui.getDom('wordcount').innerHTML = editor.getLang("wordCountTip");
  46. }
  47. editor.ui._scale();
  48. if (editor.options.scaleEnabled) {
  49. if (editor.autoHeightEnabled) {
  50. editor.disableAutoHeight();
  51. }
  52. me.enableScale();
  53. } else {
  54. me.disableScale();
  55. }
  56. if (!editor.options.elementPathEnabled && !editor.options.wordCount && !editor.options.scaleEnabled) {
  57. editor.ui.getDom('elementpath').style.display = "none";
  58. editor.ui.getDom('wordcount').style.display = "none";
  59. editor.ui.getDom('scale').style.display = "none";
  60. }
  61. if (!editor.selection.isFocus())return;
  62. editor.fireEvent('selectionchange', false, true);
  63. });
  64. editor.addListener('mousedown', function (t, evt) {
  65. var el = evt.target || evt.srcElement;
  66. baidu.editor.ui.Popup.postHide(evt, el);
  67. baidu.editor.ui.ShortCutMenu.postHide(evt);
  68. });
  69. editor.addListener("delcells", function () {
  70. if (UE.ui['edittip']) {
  71. new UE.ui['edittip'](editor);
  72. }
  73. editor.getDialog('edittip').open();
  74. });
  75. var pastePop, isPaste = false, timer;
  76. editor.addListener("afterpaste", function () {
  77. if(editor.queryCommandState('pasteplain'))
  78. return;
  79. if(baidu.editor.ui.PastePicker){
  80. pastePop = new baidu.editor.ui.Popup({
  81. content:new baidu.editor.ui.PastePicker({editor:editor}),
  82. editor:editor,
  83. className:'edui-wordpastepop'
  84. });
  85. pastePop.render();
  86. }
  87. isPaste = true;
  88. });
  89. editor.addListener("afterinserthtml", function () {
  90. clearTimeout(timer);
  91. timer = setTimeout(function () {
  92. if (pastePop && (isPaste || editor.ui._isTransfer)) {
  93. if(pastePop.isHidden()){
  94. var span = domUtils.createElement(editor.document, 'span', {
  95. 'style':"line-height:0px;",
  96. 'innerHTML':'\ufeff'
  97. }),
  98. range = editor.selection.getRange();
  99. range.insertNode(span);
  100. var tmp= getDomNode(span, 'firstChild', 'previousSibling');
  101. tmp && pastePop.showAnchor(tmp.nodeType == 3 ? tmp.parentNode : tmp);
  102. domUtils.remove(span);
  103. }else{
  104. pastePop.show();
  105. }
  106. delete editor.ui._isTransfer;
  107. isPaste = false;
  108. }
  109. }, 200)
  110. });
  111. editor.addListener('contextmenu', function (t, evt) {
  112. baidu.editor.ui.Popup.postHide(evt);
  113. });
  114. editor.addListener('keydown', function (t, evt) {
  115. if (pastePop) pastePop.dispose(evt);
  116. var keyCode = evt.keyCode || evt.which;
  117. if(evt.altKey&&keyCode==90){
  118. UE.ui.buttons['fullscreen'].onclick();
  119. }
  120. });
  121. editor.addListener('wordcount', function (type) {
  122. setCount(this,me);
  123. });
  124. function setCount(editor,ui) {
  125. editor.setOpt({
  126. wordCount:true,
  127. maximumWords:10000,
  128. wordCountMsg:editor.options.wordCountMsg || editor.getLang("wordCountMsg"),
  129. wordOverFlowMsg:editor.options.wordOverFlowMsg || editor.getLang("wordOverFlowMsg")
  130. });
  131. var opt = editor.options,
  132. max = opt.maximumWords,
  133. msg = opt.wordCountMsg ,
  134. errMsg = opt.wordOverFlowMsg,
  135. countDom = ui.getDom('wordcount');
  136. if (!opt.wordCount) {
  137. return;
  138. }
  139. var count = editor.getContentLength(true);
  140. if (count > max) {
  141. countDom.innerHTML = errMsg;
  142. editor.fireEvent("wordcountoverflow");
  143. } else {
  144. countDom.innerHTML = msg.replace("{#leave}", max - count).replace("{#count}", count);
  145. }
  146. }
  147. editor.addListener('selectionchange', function () {
  148. if (editor.options.elementPathEnabled) {
  149. me[(editor.queryCommandState('elementpath') == -1 ? 'dis' : 'en') + 'ableElementPath']()
  150. }
  151. if (editor.options.scaleEnabled) {
  152. me[(editor.queryCommandState('scale') == -1 ? 'dis' : 'en') + 'ableScale']();
  153. }
  154. });
  155. var popup = new baidu.editor.ui.Popup({
  156. editor:editor,
  157. content:'',
  158. className:'edui-bubble',
  159. _onEditButtonClick:function () {
  160. this.hide();
  161. editor.ui._dialogs.linkDialog.open();
  162. },
  163. _onImgEditButtonClick:function (name) {
  164. this.hide();
  165. editor.ui._dialogs[name] && editor.ui._dialogs[name].open();
  166. },
  167. _onImgSetFloat:function (value) {
  168. this.hide();
  169. editor.execCommand("imagefloat", value);
  170. },
  171. _setIframeAlign:function (value) {
  172. var frame = popup.anchorEl;
  173. var newFrame = frame.cloneNode(true);
  174. switch (value) {
  175. case -2:
  176. newFrame.setAttribute("align", "");
  177. break;
  178. case -1:
  179. newFrame.setAttribute("align", "left");
  180. break;
  181. case 1:
  182. newFrame.setAttribute("align", "right");
  183. break;
  184. }
  185. frame.parentNode.insertBefore(newFrame, frame);
  186. domUtils.remove(frame);
  187. popup.anchorEl = newFrame;
  188. popup.showAnchor(popup.anchorEl);
  189. },
  190. _updateIframe:function () {
  191. var frame = editor._iframe = popup.anchorEl;
  192. if(domUtils.hasClass(frame, 'ueditor_baidumap')) {
  193. editor.selection.getRange().selectNode(frame).select();
  194. editor.ui._dialogs.mapDialog.open();
  195. popup.hide();
  196. } else {
  197. editor.ui._dialogs.insertframeDialog.open();
  198. popup.hide();
  199. }
  200. },
  201. _onRemoveButtonClick:function (cmdName) {
  202. editor.execCommand(cmdName);
  203. this.hide();
  204. },
  205. queryAutoHide:function (el) {
  206. if (el && el.ownerDocument == editor.document) {
  207. if (el.tagName.toLowerCase() == 'img' || domUtils.findParentByTagName(el, 'a', true)) {
  208. return el !== popup.anchorEl;
  209. }
  210. }
  211. return baidu.editor.ui.Popup.prototype.queryAutoHide.call(this, el);
  212. }
  213. });
  214. popup.render();
  215. if (editor.options.imagePopup) {
  216. editor.addListener('mouseover', function (t, evt) {
  217. evt = evt || window.event;
  218. var el = evt.target || evt.srcElement;
  219. if (editor.ui._dialogs.insertframeDialog && /iframe/ig.test(el.tagName)) {
  220. var html = popup.formatHtml(
  221. '<nobr>' + editor.getLang("property") + ': <span onclick=$$._setIframeAlign(-2) class="edui-clickable">' + editor.getLang("default") + '</span>&nbsp;&nbsp;<span onclick=$$._setIframeAlign(-1) class="edui-clickable">' + editor.getLang("justifyleft") + '</span>&nbsp;&nbsp;<span onclick=$$._setIframeAlign(1) class="edui-clickable">' + editor.getLang("justifyright") + '</span>&nbsp;&nbsp;' +
  222. ' <span onclick="$$._updateIframe( this);" class="edui-clickable">' + editor.getLang("modify") + '</span></nobr>');
  223. if (html) {
  224. popup.getDom('content').innerHTML = html;
  225. popup.anchorEl = el;
  226. popup.showAnchor(popup.anchorEl);
  227. } else {
  228. popup.hide();
  229. }
  230. }
  231. });
  232. editor.addListener('selectionchange', function (t, causeByUi) {
  233. if (!causeByUi) return;
  234. var html = '', str = "",
  235. img = editor.selection.getRange().getClosedNode(),
  236. dialogs = editor.ui._dialogs;
  237. if (img && img.tagName == 'IMG') {
  238. var dialogName = 'insertimageDialog';
  239. if (img.className.indexOf("edui-faked-video") != -1 || img.className.indexOf("edui-upload-video") != -1) {
  240. dialogName = "insertvideoDialog"
  241. }
  242. if (img.className.indexOf("edui-faked-webapp") != -1) {
  243. dialogName = "webappDialog"
  244. }
  245. if (img.src.indexOf("http://api.map.baidu.com") != -1) {
  246. dialogName = "mapDialog"
  247. }
  248. if (img.className.indexOf("edui-faked-music") != -1) {
  249. dialogName = "musicDialog"
  250. }
  251. if (img.src.indexOf("http://maps.google.com/maps/api/staticmap") != -1) {
  252. dialogName = "gmapDialog"
  253. }
  254. if (img.getAttribute("anchorname")) {
  255. dialogName = "anchorDialog";
  256. html = popup.formatHtml(
  257. '<nobr>' + editor.getLang("property") + ': <span onclick=$$._onImgEditButtonClick("anchorDialog") class="edui-clickable">' + editor.getLang("modify") + '</span>&nbsp;&nbsp;' +
  258. '<span onclick=$$._onRemoveButtonClick(\'anchor\') class="edui-clickable">' + editor.getLang("delete") + '</span></nobr>');
  259. }
  260. if (img.getAttribute("word_img")) {
  261. //todo 放到dialog去做查询
  262. editor.word_img = [img.getAttribute("word_img")];
  263. dialogName = "wordimageDialog"
  264. }
  265. if(domUtils.hasClass(img, 'loadingclass') || domUtils.hasClass(img, 'loaderrorclass')) {
  266. dialogName = "";
  267. }
  268. if (!dialogs[dialogName]) {
  269. return;
  270. }
  271. str = '<nobr>' + editor.getLang("property") + ': '+
  272. '<span onclick=$$._onImgSetFloat("none") class="edui-clickable">' + editor.getLang("default") + '</span>&nbsp;&nbsp;' +
  273. '<span onclick=$$._onImgSetFloat("left") class="edui-clickable">' + editor.getLang("justifyleft") + '</span>&nbsp;&nbsp;' +
  274. '<span onclick=$$._onImgSetFloat("right") class="edui-clickable">' + editor.getLang("justifyright") + '</span>&nbsp;&nbsp;' +
  275. '<span onclick=$$._onImgSetFloat("center") class="edui-clickable">' + editor.getLang("justifycenter") + '</span>&nbsp;&nbsp;'+
  276. '<span onclick="$$._onImgEditButtonClick(\'' + dialogName + '\');" class="edui-clickable">' + editor.getLang("modify") + '</span></nobr>';
  277. !html && (html = popup.formatHtml(str))
  278. }
  279. if (editor.ui._dialogs.linkDialog) {
  280. var link = editor.queryCommandValue('link');
  281. var url;
  282. if (link && (url = (link.getAttribute('_href') || link.getAttribute('href', 2)))) {
  283. var txt = url;
  284. if (url.length > 30) {
  285. txt = url.substring(0, 20) + "...";
  286. }
  287. if (html) {
  288. html += '<div style="height:5px;"></div>'
  289. }
  290. html += popup.formatHtml(
  291. '<nobr>' + editor.getLang("anthorMsg") + ': <a target="_blank" href="' + url + '" title="' + url + '" >' + txt + '</a>' +
  292. ' <span class="edui-clickable" onclick="$$._onEditButtonClick();">' + editor.getLang("modify") + '</span>' +
  293. ' <span class="edui-clickable" onclick="$$._onRemoveButtonClick(\'unlink\');"> ' + editor.getLang("clear") + '</span></nobr>');
  294. popup.showAnchor(link);
  295. }
  296. }
  297. if (html) {
  298. popup.getDom('content').innerHTML = html;
  299. popup.anchorEl = img || link;
  300. popup.showAnchor(popup.anchorEl);
  301. } else {
  302. popup.hide();
  303. }
  304. });
  305. }
  306. },
  307. _initToolbars:function () {
  308. var editor = this.editor;
  309. var toolbars = this.toolbars || [];
  310. var toolbarUis = [];
  311. var extraUIs = [];
  312. for (var i = 0; i < toolbars.length; i++) {
  313. var toolbar = toolbars[i];
  314. var toolbarUi = new baidu.editor.ui.Toolbar({theme:editor.options.theme});
  315. for (var j = 0; j < toolbar.length; j++) {
  316. var toolbarItem = toolbar[j];
  317. var toolbarItemUi = null;
  318. if (typeof toolbarItem == 'string') {
  319. toolbarItem = toolbarItem.toLowerCase();
  320. if (toolbarItem == '|') {
  321. toolbarItem = 'Separator';
  322. }
  323. if(toolbarItem == '||'){
  324. toolbarItem = 'Breakline';
  325. }
  326. var ui = baidu.editor.ui[toolbarItem];
  327. if (ui) {
  328. if(utils.isFunction(ui)){
  329. toolbarItemUi = new baidu.editor.ui[toolbarItem](editor);
  330. }else{
  331. if(ui.id && ui.id != editor.key){
  332. continue;
  333. }
  334. var itemUI = ui.execFn.call(editor,editor,toolbarItem);
  335. if(itemUI){
  336. if(ui.index === undefined){
  337. toolbarUi.add(itemUI);
  338. continue;
  339. }else{
  340. extraUIs.push({
  341. index:ui.index,
  342. itemUI:itemUI
  343. })
  344. }
  345. }
  346. }
  347. }
  348. //fullscreen这里单独处理一下,放到首行去
  349. if (toolbarItem == 'fullscreen') {
  350. if (toolbarUis && toolbarUis[0]) {
  351. toolbarUis[0].items.splice(0, 0, toolbarItemUi);
  352. } else {
  353. toolbarItemUi && toolbarUi.items.splice(0, 0, toolbarItemUi);
  354. }
  355. continue;
  356. }
  357. } else {
  358. toolbarItemUi = toolbarItem;
  359. }
  360. if (toolbarItemUi && toolbarItemUi.id) {
  361. toolbarUi.add(toolbarItemUi);
  362. }
  363. }
  364. toolbarUis[i] = toolbarUi;
  365. }
  366. //接受外部定制的UI
  367. utils.each(extraUIs,function(obj){
  368. toolbarUi.add(obj.itemUI,obj.index)
  369. });
  370. this.toolbars = toolbarUis;
  371. },
  372. getHtmlTpl:function () {
  373. return '<div id="##" class="%%">' +
  374. '<div id="##_toolbarbox" class="%%-toolbarbox">' +
  375. (this.toolbars.length ?
  376. '<div id="##_toolbarboxouter" class="%%-toolbarboxouter"><div class="%%-toolbarboxinner">' +
  377. this.renderToolbarBoxHtml() +
  378. '</div></div>' : '') +
  379. '<div id="##_toolbarmsg" class="%%-toolbarmsg" style="display:none;">' +
  380. '<div id = "##_upload_dialog" class="%%-toolbarmsg-upload" onclick="$$.showWordImageDialog();">' + this.editor.getLang("clickToUpload") + '</div>' +
  381. '<div class="%%-toolbarmsg-close" onclick="$$.hideToolbarMsg();">x</div>' +
  382. '<div id="##_toolbarmsg_label" class="%%-toolbarmsg-label"></div>' +
  383. '<div style="height:0;overflow:hidden;clear:both;"></div>' +
  384. '</div>' +
  385. '<div id="##_message_holder" class="%%-messageholder"></div>' +
  386. '</div>' +
  387. '<div id="##_iframeholder" class="%%-iframeholder">' +
  388. '</div>' +
  389. //modify wdcount by matao
  390. '<div id="##_bottombar" class="%%-bottomContainer"><table><tr>' +
  391. '<td id="##_elementpath" class="%%-bottombar"></td>' +
  392. '<td id="##_wordcount" class="%%-wordcount"></td>' +
  393. '<td id="##_scale" class="%%-scale"><div class="%%-icon"></div></td>' +
  394. '</tr></table></div>' +
  395. '<div id="##_scalelayer"></div>' +
  396. '</div>';
  397. },
  398. showWordImageDialog:function () {
  399. this._dialogs['wordimageDialog'].open();
  400. },
  401. renderToolbarBoxHtml:function () {
  402. var buff = [];
  403. for (var i = 0; i < this.toolbars.length; i++) {
  404. buff.push(this.toolbars[i].renderHtml());
  405. }
  406. return buff.join('');
  407. },
  408. setFullScreen:function (fullscreen) {
  409. var editor = this.editor,
  410. container = editor.container.parentNode.parentNode;
  411. if (this._fullscreen != fullscreen) {
  412. this._fullscreen = fullscreen;
  413. this.editor.fireEvent('beforefullscreenchange', fullscreen);
  414. if (baidu.editor.browser.gecko) {
  415. var bk = editor.selection.getRange().createBookmark();
  416. }
  417. if (fullscreen) {
  418. while (container.tagName != "BODY") {
  419. var position = baidu.editor.dom.domUtils.getComputedStyle(container, "position");
  420. nodeStack.push(position);
  421. container.style.position = "static";
  422. container = container.parentNode;
  423. }
  424. this._bakHtmlOverflow = document.documentElement.style.overflow;
  425. this._bakBodyOverflow = document.body.style.overflow;
  426. this._bakAutoHeight = this.editor.autoHeightEnabled;
  427. this._bakScrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
  428. this._bakEditorContaninerWidth = editor.iframe.parentNode.offsetWidth;
  429. if (this._bakAutoHeight) {
  430. //当全屏时不能执行自动长高
  431. editor.autoHeightEnabled = false;
  432. this.editor.disableAutoHeight();
  433. }
  434. document.documentElement.style.overflow = 'hidden';
  435. //修复,滚动条不收起的问题
  436. window.scrollTo(0,window.scrollY);
  437. this._bakCssText = this.getDom().style.cssText;
  438. this._bakCssText1 = this.getDom('iframeholder').style.cssText;
  439. editor.iframe.parentNode.style.width = '';
  440. this._updateFullScreen();
  441. } else {
  442. while (container.tagName != "BODY") {
  443. container.style.position = nodeStack.shift();
  444. container = container.parentNode;
  445. }
  446. this.getDom().style.cssText = this._bakCssText;
  447. this.getDom('iframeholder').style.cssText = this._bakCssText1;
  448. if (this._bakAutoHeight) {
  449. editor.autoHeightEnabled = true;
  450. this.editor.enableAutoHeight();
  451. }
  452. document.documentElement.style.overflow = this._bakHtmlOverflow;
  453. document.body.style.overflow = this._bakBodyOverflow;
  454. editor.iframe.parentNode.style.width = this._bakEditorContaninerWidth + 'px';
  455. window.scrollTo(0, this._bakScrollTop);
  456. }
  457. if (browser.gecko && editor.body.contentEditable === 'true') {
  458. var input = document.createElement('input');
  459. document.body.appendChild(input);
  460. editor.body.contentEditable = false;
  461. setTimeout(function () {
  462. input.focus();
  463. setTimeout(function () {
  464. editor.body.contentEditable = true;
  465. editor.fireEvent('fullscreenchanged', fullscreen);
  466. editor.selection.getRange().moveToBookmark(bk).select(true);
  467. baidu.editor.dom.domUtils.remove(input);
  468. fullscreen && window.scroll(0, 0);
  469. }, 0)
  470. }, 0)
  471. }
  472. if(editor.body.contentEditable === 'true'){
  473. this.editor.fireEvent('fullscreenchanged', fullscreen);
  474. this.triggerLayout();
  475. }
  476. }
  477. },
  478. _updateFullScreen:function () {
  479. if (this._fullscreen) {
  480. var vpRect = uiUtils.getViewportRect();
  481. this.getDom().style.cssText = 'border:0;position:absolute;left:0;top:' + (this.editor.options.topOffset || 0) + 'px;width:' + vpRect.width + 'px;height:' + vpRect.height + 'px;z-index:' + (this.getDom().style.zIndex * 1 + 100);
  482. uiUtils.setViewportOffset(this.getDom(), { left:0, top:this.editor.options.topOffset || 0 });
  483. this.editor.setHeight(vpRect.height - this.getDom('toolbarbox').offsetHeight - this.getDom('bottombar').offsetHeight - (this.editor.options.topOffset || 0),true);
  484. //不手动调一下,会导致全屏失效
  485. if(browser.gecko){
  486. try{
  487. window.onresize();
  488. }catch(e){
  489. }
  490. }
  491. }
  492. },
  493. _updateElementPath:function () {
  494. var bottom = this.getDom('elementpath'), list;
  495. if (this.elementPathEnabled && (list = this.editor.queryCommandValue('elementpath'))) {
  496. var buff = [];
  497. for (var i = 0, ci; ci = list[i]; i++) {
  498. buff[i] = this.formatHtml('<span unselectable="on" onclick="$$.editor.execCommand(&quot;elementpath&quot;, &quot;' + i + '&quot;);">' + ci + '</span>');
  499. }
  500. bottom.innerHTML = '<div class="edui-editor-breadcrumb" onmousedown="return false;">' + this.editor.getLang("elementPathTip") + ': ' + buff.join(' &gt; ') + '</div>';
  501. } else {
  502. bottom.style.display = 'none'
  503. }
  504. },
  505. disableElementPath:function () {
  506. var bottom = this.getDom('elementpath');
  507. bottom.innerHTML = '';
  508. bottom.style.display = 'none';
  509. this.elementPathEnabled = false;
  510. },
  511. enableElementPath:function () {
  512. var bottom = this.getDom('elementpath');
  513. bottom.style.display = '';
  514. this.elementPathEnabled = true;
  515. this._updateElementPath();
  516. },
  517. _scale:function () {
  518. var doc = document,
  519. editor = this.editor,
  520. editorHolder = editor.container,
  521. editorDocument = editor.document,
  522. toolbarBox = this.getDom("toolbarbox"),
  523. bottombar = this.getDom("bottombar"),
  524. scale = this.getDom("scale"),
  525. scalelayer = this.getDom("scalelayer");
  526. var isMouseMove = false,
  527. position = null,
  528. minEditorHeight = 0,
  529. minEditorWidth = editor.options.minFrameWidth,
  530. pageX = 0,
  531. pageY = 0,
  532. scaleWidth = 0,
  533. scaleHeight = 0;
  534. function down() {
  535. position = domUtils.getXY(editorHolder);
  536. if (!minEditorHeight) {
  537. minEditorHeight = editor.options.minFrameHeight + toolbarBox.offsetHeight + bottombar.offsetHeight;
  538. }
  539. scalelayer.style.cssText = "position:absolute;left:0;display:;top:0;background-color:#41ABFF;opacity:0.4;filter: Alpha(opacity=40);width:" + editorHolder.offsetWidth + "px;height:"
  540. + editorHolder.offsetHeight + "px;z-index:" + (editor.options.zIndex + 1);
  541. domUtils.on(doc, "mousemove", move);
  542. domUtils.on(editorDocument, "mouseup", up);
  543. domUtils.on(doc, "mouseup", up);
  544. }
  545. var me = this;
  546. //by xuheng 全屏时关掉缩放
  547. this.editor.addListener('fullscreenchanged', function (e, fullScreen) {
  548. if (fullScreen) {
  549. me.disableScale();
  550. } else {
  551. if (me.editor.options.scaleEnabled) {
  552. me.enableScale();
  553. var tmpNode = me.editor.document.createElement('span');
  554. me.editor.body.appendChild(tmpNode);
  555. me.editor.body.style.height = Math.max(domUtils.getXY(tmpNode).y, me.editor.iframe.offsetHeight - 20) + 'px';
  556. domUtils.remove(tmpNode)
  557. }
  558. }
  559. });
  560. function move(event) {
  561. clearSelection();
  562. var e = event || window.event;
  563. pageX = e.pageX || (doc.documentElement.scrollLeft + e.clientX);
  564. pageY = e.pageY || (doc.documentElement.scrollTop + e.clientY);
  565. scaleWidth = pageX - position.x;
  566. scaleHeight = pageY - position.y;
  567. if (scaleWidth >= minEditorWidth) {
  568. isMouseMove = true;
  569. scalelayer.style.width = scaleWidth + 'px';
  570. }
  571. if (scaleHeight >= minEditorHeight) {
  572. isMouseMove = true;
  573. scalelayer.style.height = scaleHeight + "px";
  574. }
  575. }
  576. function up() {
  577. if (isMouseMove) {
  578. isMouseMove = false;
  579. editor.ui._actualFrameWidth = scalelayer.offsetWidth - 2;
  580. editorHolder.style.width = editor.ui._actualFrameWidth + 'px';
  581. editor.setHeight(scalelayer.offsetHeight - bottombar.offsetHeight - toolbarBox.offsetHeight - 2,true);
  582. }
  583. if (scalelayer) {
  584. scalelayer.style.display = "none";
  585. }
  586. clearSelection();
  587. domUtils.un(doc, "mousemove", move);
  588. domUtils.un(editorDocument, "mouseup", up);
  589. domUtils.un(doc, "mouseup", up);
  590. }
  591. function clearSelection() {
  592. if (browser.ie)
  593. doc.selection.clear();
  594. else
  595. window.getSelection().removeAllRanges();
  596. }
  597. this.enableScale = function () {
  598. //trace:2868
  599. if (editor.queryCommandState("source") == 1) return;
  600. scale.style.display = "";
  601. this.scaleEnabled = true;
  602. domUtils.on(scale, "mousedown", down);
  603. };
  604. this.disableScale = function () {
  605. scale.style.display = "none";
  606. this.scaleEnabled = false;
  607. domUtils.un(scale, "mousedown", down);
  608. };
  609. },
  610. isFullScreen:function () {
  611. return this._fullscreen;
  612. },
  613. postRender:function () {
  614. UIBase.prototype.postRender.call(this);
  615. for (var i = 0; i < this.toolbars.length; i++) {
  616. this.toolbars[i].postRender();
  617. }
  618. var me = this;
  619. var timerId,
  620. domUtils = baidu.editor.dom.domUtils,
  621. updateFullScreenTime = function () {
  622. clearTimeout(timerId);
  623. timerId = setTimeout(function () {
  624. me._updateFullScreen();
  625. });
  626. };
  627. domUtils.on(window, 'resize', updateFullScreenTime);
  628. me.addListener('destroy', function () {
  629. domUtils.un(window, 'resize', updateFullScreenTime);
  630. clearTimeout(timerId);
  631. })
  632. },
  633. showToolbarMsg:function (msg, flag) {
  634. this.getDom('toolbarmsg_label').innerHTML = msg;
  635. this.getDom('toolbarmsg').style.display = '';
  636. //
  637. if (!flag) {
  638. var w = this.getDom('upload_dialog');
  639. w.style.display = 'none';
  640. }
  641. },
  642. hideToolbarMsg:function () {
  643. this.getDom('toolbarmsg').style.display = 'none';
  644. },
  645. mapUrl:function (url) {
  646. return url ? url.replace('~/', this.editor.options.UEDITOR_HOME_URL || '') : ''
  647. },
  648. triggerLayout:function () {
  649. var dom = this.getDom();
  650. if (dom.style.zoom == '1') {
  651. dom.style.zoom = '100%';
  652. } else {
  653. dom.style.zoom = '1';
  654. }
  655. }
  656. };
  657. utils.inherits(EditorUI, baidu.editor.ui.UIBase);
  658. var instances = {};
  659. UE.ui.Editor = function (options) {
  660. var editor = new UE.Editor(options);
  661. editor.options.editor = editor;
  662. utils.loadFile(document, {
  663. href:editor.options.themePath + editor.options.theme + "/_css/ueditor.css",
  664. tag:"link",
  665. type:"text/css",
  666. rel:"stylesheet"
  667. });
  668. var oldRender = editor.render;
  669. editor.render = function (holder) {
  670. if (holder.constructor === String) {
  671. editor.key = holder;
  672. instances[holder] = editor;
  673. }
  674. utils.domReady(function () {
  675. editor.langIsReady ? renderUI() : editor.addListener("langReady", renderUI);
  676. function renderUI() {
  677. editor.setOpt({
  678. labelMap:editor.options.labelMap || editor.getLang('labelMap')
  679. });
  680. new EditorUI(editor.options);
  681. if (holder) {
  682. if (holder.constructor === String) {
  683. holder = document.getElementById(holder);
  684. }
  685. holder && holder.getAttribute('name') && ( editor.options.textarea = holder.getAttribute('name'));
  686. if (holder && /script|textarea/ig.test(holder.tagName)) {
  687. var newDiv = document.createElement('div');
  688. holder.parentNode.insertBefore(newDiv, holder);
  689. var cont = holder.value || holder.innerHTML;
  690. editor.options.initialContent = /^[\t\r\n ]*$/.test(cont) ? editor.options.initialContent :
  691. cont.replace(/>[\n\r\t]+([ ]{4})+/g, '>')
  692. .replace(/[\n\r\t]+([ ]{4})+</g, '<')
  693. .replace(/>[\n\r\t]+</g, '><');
  694. holder.className && (newDiv.className = holder.className);
  695. holder.style.cssText && (newDiv.style.cssText = holder.style.cssText);
  696. if (/textarea/i.test(holder.tagName)) {
  697. editor.textarea = holder;
  698. editor.textarea.style.display = 'none';
  699. } else {
  700. holder.parentNode.removeChild(holder);
  701. }
  702. if(holder.id){
  703. newDiv.id = holder.id;
  704. domUtils.removeAttributes(holder,'id');
  705. }
  706. holder = newDiv;
  707. holder.innerHTML = '';
  708. }
  709. }
  710. domUtils.addClass(holder, "edui-" + editor.options.theme);
  711. editor.ui.render(holder);
  712. var opt = editor.options;
  713. //给实例添加一个编辑器的容器引用
  714. editor.container = editor.ui.getDom();
  715. var parents = domUtils.findParents(holder,true);
  716. var displays = [];
  717. for(var i = 0 ,ci;ci=parents[i];i++){
  718. displays[i] = ci.style.display;
  719. ci.style.display = 'block'
  720. }
  721. if (opt.initialFrameWidth) {
  722. opt.minFrameWidth = opt.initialFrameWidth;
  723. } else {
  724. opt.minFrameWidth = opt.initialFrameWidth = holder.offsetWidth;
  725. var styleWidth = holder.style.width;
  726. if(/%$/.test(styleWidth)) {
  727. opt.initialFrameWidth = styleWidth;
  728. }
  729. }
  730. if (opt.initialFrameHeight) {
  731. opt.minFrameHeight = opt.initialFrameHeight;
  732. } else {
  733. opt.initialFrameHeight = opt.minFrameHeight = holder.offsetHeight;
  734. }
  735. for(var i = 0 ,ci;ci=parents[i];i++){
  736. ci.style.display = displays[i]
  737. }
  738. //编辑器最外容器设置了高度,会导致,编辑器不占位
  739. //todo 先去掉,没有找到原因
  740. if(holder.style.height){
  741. holder.style.height = ''
  742. }
  743. editor.container.style.width = opt.initialFrameWidth + (/%$/.test(opt.initialFrameWidth) ? '' : 'px');
  744. editor.container.style.zIndex = opt.zIndex;
  745. oldRender.call(editor, editor.ui.getDom('iframeholder'));
  746. editor.fireEvent("afteruiready");
  747. }
  748. })
  749. };
  750. return editor;
  751. };
  752. /**
  753. * @file
  754. * @name UE
  755. * @short UE
  756. * @desc UEditor的顶部命名空间
  757. */
  758. /**
  759. * @name getEditor
  760. * @since 1.2.4+
  761. * @grammar UE.getEditor(id,[opt]) => Editor实例
  762. * @desc 提供一个全局的方法得到编辑器实例
  763. *
  764. * * ''id'' 放置编辑器的容器id, 如果容器下的编辑器已经存在,就直接返回
  765. * * ''opt'' 编辑器的可选参数
  766. * @example
  767. * UE.getEditor('containerId',{onready:function(){//创建一个编辑器实例
  768. * this.setContent('hello')
  769. * }});
  770. * UE.getEditor('containerId'); //返回刚创建的实例
  771. *
  772. */
  773. UE.getEditor = function (id, opt) {
  774. var editor = instances[id];
  775. if (!editor) {
  776. editor = instances[id] = new UE.ui.Editor(opt);
  777. editor.render(id);
  778. }
  779. return editor;
  780. };
  781. UE.delEditor = function (id) {
  782. var editor;
  783. if (editor = instances[id]) {
  784. editor.key && editor.destroy();
  785. delete instances[id]
  786. }
  787. };
  788. UE.registerUI = function(uiName,fn,index,editorId){
  789. utils.each(uiName.split(/\s+/), function (name) {
  790. baidu.editor.ui[name] = {
  791. id : editorId,
  792. execFn:fn,
  793. index:index
  794. };
  795. })
  796. }
  797. })();