space.vue.subassembly.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. /**
  2. * 图片上传
  3. */
  4. Vue.component('spaceUploadImg', {
  5. data: function () {
  6. return {
  7. id: "spaceUploadImg" + space.utils.random(6),
  8. }
  9. },
  10. name: "spaceUploadImg",
  11. model: {
  12. prop: 'patharry',
  13. evnet: 'click'
  14. },
  15. /**
  16. * patharry: v-model 绑定的值,非空,字符串数组
  17. * type: 上传按钮的风格,可空,缺省值为 button ,可选值为button / img
  18. * width: 图片裁剪的宽度,可空
  19. * height: 图片裁剪的高度,可空
  20. * maxsize: 最大上传的图片数量,可空,缺省值为1
  21. * tip: 提示语,可空
  22. * color: 图片背景,可空,值的类型与css的color值类型一样
  23. * source: 是否可上传原图
  24. */
  25. props: {
  26. patharry: {
  27. type: Array,
  28. required: true
  29. },
  30. type: {
  31. type: String,
  32. default: "button"
  33. },
  34. width: [String, Number],
  35. height: [String, Number],
  36. maxsize: {
  37. type: [String, Number],
  38. default: 1
  39. },
  40. tip: [String, Number],
  41. color: [String, Number],
  42. source: {
  43. type: Boolean,
  44. default: false,
  45. }
  46. },
  47. created: function () {
  48. // 对初始化值进行处理,排除掉空数据
  49. if (!this.patharry) {
  50. this.patharry = [];
  51. }
  52. var newAry = [];
  53. if (typeof this.patharry == "string") {
  54. if (space.utils.notNull(this.patharry)) {
  55. newAry.push(this.patharry);
  56. }
  57. } else {
  58. for (let index = 0; index < this.patharry.length; index++) {
  59. const element = this.patharry[index];
  60. if (space.utils.notNull(element)) {
  61. newAry.push(element);
  62. }
  63. }
  64. }
  65. this.patharry = newAry;
  66. this.$emit("input", newAry);
  67. },
  68. mounted: function () {
  69. var that = this;
  70. space.plugins.cropper.init({
  71. el: $("#" + that.id),
  72. photoSize: { // 裁剪的图片大小,会自动计算裁剪比例
  73. width: that.width,
  74. height: that.height,
  75. source: that.source,
  76. },
  77. show: function (setting) { //打开裁剪框前的回调
  78. setting.photoSize = {
  79. width: that.width,
  80. height: that.height,
  81. source: that.source,
  82. }
  83. },
  84. success: function (path) { // 成功后的回调方法,path为裁剪后的img路径
  85. that.patharry.push(path);
  86. that.$emit("input", that.patharry);
  87. },
  88. });
  89. },
  90. methods: {
  91. uploadClick: function () {
  92. if (this.patharry.length >= this.maxsize) {
  93. space.message.warning("最多上传" + this.maxsize + "张图片");
  94. return false;
  95. }
  96. $("#" + this.id).click();
  97. },
  98. removeCropperImg: function (index) {
  99. this.patharry.splice(index, 1);
  100. space.message.success("图片已删除");
  101. this.$emit("input", this.patharry);
  102. },
  103. },
  104. template: `
  105. <div class="space-upload-img">
  106. <el-button size="small" icon="el-icon-upload" @click="uploadClick" :disabled="patharry.length >= maxsize" v-if="type == 'button' || space.utils.isNull(type)">上传</el-button>
  107. <span v-show="patharry.length >= maxsize" style="color: #C0C4CC;margin-left: 6px;">
  108. 最多上传{{maxsize}}张图片
  109. </span>
  110. <span v-show="tip" style="color: #888;margin-left: 6px;">
  111. {{tip}}
  112. </span>
  113. <input type="file" accept="image/*" :id="id" style="display: none;" />
  114. <div>
  115. <div v-show="patharry" class="cropper-img-content" v-for="(item, index) in patharry" :key="index"">
  116. <el-image class="cropper-img" fit="contain" :style="{'background-color': color}"
  117. :src="item" @click="space.plugins.elPreview.show(item, patharry)">
  118. </el-image>
  119. <i class="el-icon-close" @click="removeCropperImg(index)"></i>
  120. </div>
  121. <div class="uploadButton-img" v-if="type == 'img'" @click="uploadClick" v-show="patharry.length < maxsize">
  122. <i class="el-icon-plus"></i>
  123. </div>
  124. </div>
  125. </div>
  126. `
  127. })
  128. /**
  129. * 图片预览
  130. */
  131. Vue.component('spacePreviewImg', {
  132. data: function () {
  133. return {
  134. newAry: [],
  135. }
  136. },
  137. name: "spacePreviewImg",
  138. /**
  139. * paths: 绑定的值,非空,字符串/数组
  140. * color: 图片背景色,可空,值的类型与css的color值类型一样
  141. */
  142. props: {
  143. paths: {
  144. type: [Array, String],
  145. required: true
  146. },
  147. color: {
  148. type: String,
  149. }
  150. },
  151. created: function () {
  152. // 对初始化值进行处理,排除掉空数据
  153. if (!this.paths) {
  154. this.paths = [];
  155. }
  156. if (typeof this.paths == "string") {
  157. if (space.utils.notNull(this.paths)) {
  158. this.newAry.push(this.paths);
  159. }
  160. } else {
  161. for (let index = 0; index < this.paths.length; index++) {
  162. const element = this.paths[index];
  163. if (space.utils.notNull(element)) {
  164. this.newAry.push(element);
  165. }
  166. }
  167. }
  168. this.$emit("input", this.newAry);
  169. },
  170. mounted: function () {
  171. },
  172. methods: {
  173. },
  174. template: `
  175. <div class="space-preview-img">
  176. <div class="cropper-img-content" v-for="(item, index) in newAry" :key="index">
  177. <el-image class="cropper-img" fit="contain" :style="{'background-color': color}"
  178. :src="item" @click="space.plugins.elPreview.show(item, newAry)">
  179. </el-image>
  180. </div>
  181. </div>
  182. `
  183. })
  184. /**
  185. * 对象选择器
  186. */
  187. Vue.component('spaceSelect', {
  188. data: function () {
  189. return {
  190. id: "spaceSelect" + space.utils.random(6),
  191. randomNum: space.utils.random(6),
  192. options: [],
  193. isOpen: true, // 是否打开
  194. }
  195. },
  196. name: "spaceSelect",
  197. model: {
  198. prop: 'modelAry',
  199. evnet: 'click'
  200. },
  201. /**
  202. * modelAry: 绑定的值,非空,字符串/数组
  203. * predefine: 预定义对象,非空,可选值有person、department、post、role
  204. * datasize: 最多选择的数量,可空,默认值为1
  205. * method:自定义方法
  206. */
  207. props: ['modelAry', "predefine", "datasize", "method"],
  208. watch: {
  209. modelAry: function (newVal, oldVal) {
  210. // 没有打开时,则可以初始化选择数据集合
  211. if (!this.isOpen) {
  212. this.isOpen = true;
  213. this.initSelectedDataList();
  214. } else {
  215. this.isOpen = false;
  216. }
  217. }
  218. },
  219. created: function () {
  220. if (this.predefine && this.method) {
  221. space.message.error('predefine 与 method 不能同时存在');
  222. }
  223. if (!this.isPredefine()) {
  224. if (!this.method) {
  225. space.message.warning('预定义选择器不存在:' + this.predefine);
  226. }
  227. }
  228. },
  229. mounted: function () {
  230. var that = this;
  231. if (!this.method) {
  232. // 初始化选择窗
  233. space.choose[that.predefine].init({
  234. el: $("#choose" + that.predefine + that.randomNum), // 点击弹出选择窗的元素
  235. selectAry: function () {
  236. return that.modelAry;
  237. }, // 已选的数组
  238. selectCountNum: that.datasize || 1, // 最多选择的数量
  239. success: function (param) { // 成功后的回调
  240. that.options = param;
  241. that.modelAry = [];
  242. that.options.forEach(function (item) {
  243. that.modelAry.push(item["id"]);
  244. });
  245. that.$emit("input", that.modelAry);
  246. }
  247. })
  248. }
  249. /* 监听容器高度 */
  250. const erd = elementResizeDetectorMaker();
  251. var eles = $("#" + that.id).find("div.el-input--suffix")[0];
  252. erd.listenTo(eles, element => {
  253. const height = element.offsetHeight;
  254. $("#choose" + that.predefine + that.randomNum).css("height", height);
  255. });
  256. this.initSelectedDataList();
  257. },
  258. methods: {
  259. removeFun: function () {
  260. this.$emit("input", this.modelAry);
  261. },
  262. /**
  263. * 获取最大限制绑定的值
  264. */
  265. getMaxSelectedModelAry: function () {
  266. var tmpAry = [];
  267. var _this = this;
  268. this.options.forEach(function (item) {
  269. tmpAry.push(item["id"]);
  270. });
  271. if (this.datasize == undefined || this.datasize == 1) {
  272. return tmpAry.length > 0 ? [tmpAry[tmpAry.length - 1]] : [];
  273. } else if (this.datasize > 1) {
  274. var selectedAry = [];
  275. tmpAry.forEach(function (item, index) {
  276. if ((index + 1) > _this.datasize) {
  277. return;
  278. }
  279. selectedAry.push(item);
  280. });
  281. return selectedAry;
  282. } else if (this.datasize == '*') {
  283. return tmpAry;
  284. } else {
  285. return [];
  286. }
  287. },
  288. /**
  289. * 是否预定义选择器,false:不是预定义选择器,true:是预定义选择器
  290. */
  291. isPredefine: function () {
  292. if (this.predefine != "person" && this.predefine != "department" && this.predefine != "post" && this.predefine != "role") {
  293. return false; // 不是预定义选择器
  294. } else {
  295. return true; // 预定义选择器
  296. }
  297. },
  298. /**
  299. * 获取预定义的服务
  300. */
  301. getPredefineService: function () {
  302. var map = new Map();
  303. map.set("person", "/apps?ark=personnel.person_getChoolsePersonInfoApi");
  304. map.set("department", "/apps?ark=sys.department_getChoolseDepartmentInfoApi");
  305. map.set("post", "/apps?ark=sys.post_getChoolsePostInfoApi");
  306. map.set("role", "/apps?ark=sys.role_getChoolseRoleInfoApi");
  307. return map.get(this.predefine);
  308. },
  309. /**
  310. * 初始化选择数据集合
  311. */
  312. initSelectedDataList: function () {
  313. if (!this.isPredefine() || this.method) {
  314. return;
  315. }
  316. if (!this.modelAry || this.modelAry.length == 0) {
  317. this.isOpen = false; // 没有数据时,则关闭
  318. return;
  319. }
  320. var url = this.getPredefineService();
  321. var ids = typeof (this.modelAry) == 'string' || typeof (this.modelAry) == 'number' ? this.modelAry : this.modelAry.join(',');
  322. var _this = this;
  323. // 查询选中的列表
  324. space.utils.ajax({
  325. url: url,
  326. data: {
  327. "ids": ids
  328. },
  329. dataType: 'json',
  330. callback: function (data) {
  331. _this.options = data.data;
  332. var tmpAry = _this.getMaxSelectedModelAry();
  333. _this.$emit("input", tmpAry);
  334. }
  335. })
  336. },
  337. },
  338. template: `
  339. <div class="space-select" :id="id" >
  340. <el-select class="choose-obj" v-model="modelAry" multiple placeholder="请选择" :value-key="id" clearable
  341. :popper-append-to-body="false" style="width: calc(100% - 60px);" :disabled="!modelAry.length>0" @remove-tag="removeFun" @clear="removeFun">
  342. <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
  343. </el-option>
  344. </el-select>
  345. <el-button type="button" size="medium" class="choose-btn" :id="'choose' + predefine + randomNum" icon="el-icon-search"></el-button>
  346. </div>
  347. `
  348. })
  349. /**
  350. * Editor 编辑器
  351. */
  352. Vue.component('spaceEditor', {
  353. data: function () {
  354. return {
  355. id: "spaceEditor" + space.utils.random(6),
  356. editor: undefined,
  357. // 编辑器需要的 js 文件
  358. jsUrl: [
  359. '/apps/space/plug-in/ckeditor5/build/ckeditor.js',
  360. '/apps/space/plug-in/ckeditor5/src/plugins/upload/src/myUploadAdapter.js'
  361. ],
  362. // 标识,全部为 true 则代表全部加载完成
  363. jsUrlFlag: [],
  364. notLoadEditor: true, // 未加载完 editor 编辑器,true:未加载完,false:已加载完
  365. imageQuality: 'standard',
  366. uploading: false,
  367. uploadStatus: '',
  368. previewVisible: false,
  369. previewMode: 'pc',
  370. previewUrl: '',
  371. previewLoading: false,
  372. }
  373. },
  374. name: "spaceEditor",
  375. model: {
  376. prop: 'htmlContent',
  377. evnet: 'click'
  378. },
  379. /**
  380. * htmlContent: v-model 绑定的值,非空,html 字符串
  381. * size: 编辑器的大小,small、medium、large
  382. * width: 编辑器的宽度,可空
  383. * height: 编辑器的高度,可空
  384. * placeholder:占位符
  385. * type:编辑器工具栏类型
  386. * isfullscreen:是否有全屏,
  387. */
  388. props: ['htmlContent', 'size', 'width', 'height', 'placeholder', 'type', 'isfullscreen', 'articlepreview', 'previewdata', 'previewurl'],
  389. computed: {
  390. previewFrameStyle: function () {
  391. var widthMap = {
  392. pc: '1200px',
  393. pad: '768px',
  394. mobile: '390px'
  395. };
  396. return {
  397. width: widthMap[this.previewMode] || widthMap.pc,
  398. height: '680px'
  399. };
  400. }
  401. },
  402. watch: {
  403. height: function (newVal, oldVal) {
  404. if (this.height && this.height.toString().toLocaleLowerCase().indexOf('px') == -1) {
  405. this.height += 'px';
  406. } else {
  407. this.setEditorHeight();
  408. }
  409. },
  410. editor: function (newVal, oldVal) {
  411. this.setEditorHeight();
  412. },
  413. jsUrlFlag: function (newVal, oldVal) {
  414. if (this.jsUrl.length > 0 && this.jsUrl.length == this.jsUrlFlag.length) {
  415. if (this.jsUrlFlag.indexOf('false') != -1) {
  416. return;
  417. }
  418. this.initEditor();
  419. this.notLoadEditor = false;
  420. this.setEditorHeight();
  421. }
  422. },
  423. notLoadEditor: function (newVal, oldVal) {
  424. this.setEditorHeight();
  425. },
  426. },
  427. created: function () {
  428. var _this = this;
  429. for (var i = 0; i < _this.jsUrl.length; i++) {
  430. _this.loadJS(_this.jsUrl[i], function () {
  431. _this.jsUrlFlag.push('true');
  432. });
  433. }
  434. },
  435. methods: {
  436. loadJS: function (url, callback) {
  437. for (var i = 0; i < document.getElementsByTagName('head')[0].getElementsByTagName('script').length; i++) {
  438. var src = document.getElementsByTagName('head')[0].getElementsByTagName('script')[i].getAttribute('src');
  439. if (url == src) {
  440. // 防止一个页面有多个编辑器时,编辑器加载失败
  441. if (space.vars.editorReady != true) {
  442. var winInterval = setInterval(function () {
  443. if (space.vars.editorReady != true) {
  444. return;
  445. }
  446. window.clearInterval(winInterval);
  447. if (callback) {
  448. callback();
  449. }
  450. }, 100)
  451. }
  452. return;
  453. }
  454. }
  455. var script = document.createElement('script');
  456. script.type = 'text/javascript';
  457. script.onload = function () {
  458. if (callback) {
  459. callback();
  460. }
  461. };
  462. script.src = url;
  463. document.getElementsByTagName('head')[0].appendChild(script);
  464. },
  465. isArticlePreview: function () {
  466. return this.articlepreview === true || this.articlepreview === 'true';
  467. },
  468. showEditorTools: function () {
  469. return this.type != 'flow' && this.isArticlePreview();
  470. },
  471. getUploadOptions: function () {
  472. return {
  473. imageQuality: this.imageQuality,
  474. imageMaxWidth: 1920
  475. };
  476. },
  477. myCustomUploadAdapterPlugin: function (editor) {
  478. this.installUploadAdapter(editor);
  479. },
  480. installUploadAdapter: function (editor) {
  481. var _this = this;
  482. editor.plugins.get('FileRepository').createUploadAdapter = (loader) => {
  483. // 第二个参数设置上传图片的地址
  484. return new MyUploadAdapter(loader, _this.getUploadOptions());
  485. };
  486. },
  487. uploadPlainFileByAdapter: function (file, options, onProgress) {
  488. if (typeof MyUploadAdapter == "undefined") {
  489. return Promise.reject("上传组件未加载完成,请刷新页面后重试");
  490. }
  491. if (typeof MyUploadAdapter.uploadPlainFile == "function") {
  492. return MyUploadAdapter.uploadPlainFile(file, options, onProgress);
  493. }
  494. var adapter = new MyUploadAdapter(null, options || {});
  495. if (typeof adapter.uploadPlainFile == "function") {
  496. return adapter.uploadPlainFile(file, onProgress);
  497. }
  498. return Promise.reject("上传组件未加载完成,请刷新页面后重试");
  499. },
  500. /**
  501. * 获取高度
  502. */
  503. getHeight: function () {
  504. if (!this.height) {
  505. if (this.size == undefined || this.size == "small") {
  506. return '200px';
  507. } else if (this.size == "medium") {
  508. return '400px';
  509. } else if (this.size == "large") {
  510. return '600px';
  511. }
  512. } else if (this.height) {
  513. if (this.height.toString().toLocaleLowerCase().indexOf('px') == -1) {
  514. return this.height + 'px';
  515. } else {
  516. return this.height;
  517. }
  518. }
  519. return "";
  520. },
  521. /**
  522. * 设置编辑器高度
  523. */
  524. setEditorHeight: function () {
  525. if (this.notLoadEditor) {
  526. return;
  527. }
  528. var height = this.getHeight();
  529. if (!this.editor || !height) {
  530. return;
  531. }
  532. var _this = this;
  533. // 设置编辑器高度
  534. _this.editor.editing.view.change(writer => {
  535. writer.setStyle(
  536. "height",
  537. height,
  538. _this.editor.editing.view.document.getRoot()
  539. );
  540. });
  541. },
  542. /**
  543. * 获取工具栏
  544. */
  545. getToolbar: function () {
  546. var items = [];
  547. // 是否有全屏工具
  548. if (this.isfullscreen == false) {
  549. // 没有全屏工具栏
  550. items = [
  551. 'fontfamily', 'fontsize', 'fontColor', 'fontBackgroundColor', 'blockQuote', 'bold', 'italic', 'link', '|',
  552. 'alignment', 'horizontalLine', '|', 'indent', 'outdent', '|', 'imageUpload', 'wordUpload', 'insertTable', 'mediaEmbed', 'phrase', 'undo', 'redo'
  553. ];
  554. } else {
  555. items = [
  556. 'fontfamily', 'fontsize', 'fontColor', 'fontBackgroundColor', 'blockQuote', 'bold', 'italic', 'link', '|',
  557. 'alignment', 'horizontalLine', '|', 'indent', 'outdent', '|', 'imageUpload', 'wordUpload', 'insertTable', 'mediaEmbed', 'phrase', 'undo', 'redo', 'fullScreen'
  558. ];
  559. }
  560. var toolbar = {
  561. items: items
  562. };
  563. if (!this.type) {
  564. return toolbar;
  565. }
  566. var map = new Map();
  567. map.set("flow", ['phrase']);
  568. if (map.get(this.type)) {
  569. return {
  570. items: map.get(this.type)
  571. }
  572. }
  573. return toolbar;
  574. },
  575. alignFullScreenToolbarButton: function () {
  576. if (!this.editor || !this.editor.ui || !this.editor.ui.view || !this.editor.ui.view.element) {
  577. return;
  578. }
  579. var toolbarItems = this.editor.ui.view.element.querySelector(".ck-toolbar__items");
  580. if (!toolbarItems) {
  581. return;
  582. }
  583. var buttons = toolbarItems.querySelectorAll(".ck-button");
  584. for (var i = 0; i < buttons.length; i++) {
  585. var label = (buttons[i].getAttribute("aria-label") || buttons[i].getAttribute("title") || buttons[i].textContent || "").replace(/\s/g, "");
  586. if (label.indexOf("全屏") != -1) {
  587. buttons[i].style.marginLeft = "auto";
  588. toolbarItems.appendChild(buttons[i]);
  589. return;
  590. }
  591. }
  592. },
  593. formatContent: function () {
  594. if (!this.editor) {
  595. return;
  596. }
  597. var html = this.editor.getData();
  598. if (space.utils.isNull(html)) {
  599. space.message.warning("正文内容为空");
  600. return;
  601. }
  602. var wrapper = document.createElement("div");
  603. wrapper.innerHTML = html;
  604. var all = wrapper.querySelectorAll("*");
  605. for (var i = 0; i < all.length; i++) {
  606. var node = all[i];
  607. node.removeAttribute("lang");
  608. node.removeAttribute("width");
  609. node.removeAttribute("height");
  610. node.removeAttribute("onload");
  611. node.removeAttribute("border");
  612. node.removeAttribute("vspace");
  613. node.removeAttribute("hspace");
  614. var style = node.getAttribute("style") || "";
  615. if (/mso-|font-family|text-indent:\s*0|background:\s*transparent/i.test(style)) {
  616. node.removeAttribute("style");
  617. }
  618. }
  619. var inlineNodes = wrapper.querySelectorAll("span, font");
  620. for (var s = inlineNodes.length - 1; s >= 0; s--) {
  621. var inlineNode = inlineNodes[s];
  622. if (inlineNode.attributes.length == 0 && inlineNode.parentNode) {
  623. while (inlineNode.firstChild) {
  624. inlineNode.parentNode.insertBefore(inlineNode.firstChild, inlineNode);
  625. }
  626. inlineNode.parentNode.removeChild(inlineNode);
  627. }
  628. }
  629. var emptyBlocks = wrapper.querySelectorAll("p, div");
  630. for (var e = emptyBlocks.length - 1; e >= 0; e--) {
  631. var block = emptyBlocks[e];
  632. var hasMedia = block.querySelector("img, video, iframe, table");
  633. var text = (block.textContent || "").replace(/\u00a0/g, "").trim();
  634. if (!hasMedia && text == "" && block.parentNode) {
  635. block.parentNode.removeChild(block);
  636. }
  637. }
  638. var images = wrapper.querySelectorAll("img");
  639. for (var imgIndex = 0; imgIndex < images.length; imgIndex++) {
  640. images[imgIndex].style.maxWidth = "100%";
  641. images[imgIndex].style.height = "auto";
  642. images[imgIndex].style.display = "block";
  643. images[imgIndex].style.margin = "12px auto";
  644. }
  645. var videos = wrapper.querySelectorAll("video, iframe");
  646. for (var v = 0; v < videos.length; v++) {
  647. videos[v].style.maxWidth = "100%";
  648. if (videos[v].tagName.toLowerCase() == "video") {
  649. videos[v].setAttribute("controls", "controls");
  650. videos[v].setAttribute("playsinline", "playsinline");
  651. videos[v].style.display = "block";
  652. videos[v].style.margin = "12px auto";
  653. }
  654. }
  655. var tables = wrapper.querySelectorAll("table");
  656. for (var t = 0; t < tables.length; t++) {
  657. tables[t].style.width = "100%";
  658. tables[t].style.borderCollapse = "collapse";
  659. }
  660. this.editor.setData(wrapper.innerHTML);
  661. this.$emit("input", this.editor.getData());
  662. space.message.success("正文已完成排版整理");
  663. },
  664. insertHtml: function (html) {
  665. if (!this.editor || space.utils.isNull(html)) {
  666. return;
  667. }
  668. var viewFragment = this.editor.data.processor.toView(html);
  669. var modelFragment = this.editor.data.toModel(viewFragment);
  670. this.editor.model.insertContent(modelFragment);
  671. this.$emit("input", this.editor.getData());
  672. },
  673. openVideoPicker: function () {
  674. if (this.$refs.videoInput) {
  675. this.$refs.videoInput.click();
  676. }
  677. },
  678. handleVideoSelect: function (event) {
  679. var files = Array.prototype.slice.call(event.target.files || []);
  680. event.target.value = "";
  681. if (files.length > 0) {
  682. this.uploadVideoFile(files[0]);
  683. }
  684. },
  685. uploadVideoFile: function (file) {
  686. if (!file || !file.type || file.type.indexOf("video/") !== 0) {
  687. space.message.warning("请选择视频文件");
  688. return;
  689. }
  690. var _this = this;
  691. _this.uploading = true;
  692. _this.uploadStatus = "正在上传视频";
  693. _this.uploadPlainFileByAdapter(file, {
  694. imageQuality: 'origin'
  695. }, function (loaded, total) {
  696. if (total > 0) {
  697. _this.uploadStatus = "正在上传视频 " + Math.round(loaded * 100 / total) + "%";
  698. }
  699. }).then(function (res) {
  700. if (_this.editor && _this.editor.commands.get('mediaEmbed')) {
  701. _this.editor.execute('mediaEmbed', res.default);
  702. } else {
  703. _this.insertHtml('<p><video controls playsinline preload="metadata" src="' + res.default + '"></video></p>');
  704. }
  705. _this.$emit("input", _this.editor.getData());
  706. space.message.success("视频已插入正文");
  707. }).catch(function (msg) {
  708. space.message.error(msg || "视频上传失败");
  709. }).finally(function () {
  710. _this.uploading = false;
  711. _this.uploadStatus = "";
  712. });
  713. },
  714. bindVideoPasteUpload: function () {
  715. if (!this.editor || !this.editor.ui || !this.editor.ui.getEditableElement) {
  716. return;
  717. }
  718. var editable = this.editor.ui.getEditableElement();
  719. if (!editable || editable.getAttribute("data-space-video-upload") == "true") {
  720. return;
  721. }
  722. editable.setAttribute("data-space-video-upload", "true");
  723. var _this = this;
  724. var handler = function (event) {
  725. var dataTransfer = event.clipboardData || event.dataTransfer;
  726. if (!dataTransfer || !dataTransfer.files || dataTransfer.files.length == 0) {
  727. return;
  728. }
  729. var files = Array.prototype.slice.call(dataTransfer.files);
  730. var videoFile = files.find(function (file) {
  731. return file && file.type && file.type.indexOf("video/") === 0;
  732. });
  733. if (videoFile) {
  734. event.preventDefault();
  735. _this.uploadVideoFile(videoFile);
  736. }
  737. };
  738. editable.addEventListener("paste", handler);
  739. editable.addEventListener("drop", handler);
  740. },
  741. getPreviewPostData: function () {
  742. var data = this.previewdata || {};
  743. return {
  744. title: data.title || '',
  745. source: data.source || '',
  746. navigationid: data.navigationid || '',
  747. desc: data.desc || '',
  748. body: this.editor ? this.editor.getData() : (this.htmlContent || '')
  749. };
  750. },
  751. openArticlePreview: function () {
  752. if (!this.editor) {
  753. return;
  754. }
  755. var postData = this.getPreviewPostData();
  756. if (space.utils.isNull(postData.navigationid)) {
  757. space.message.warning("请先选择发布栏目");
  758. return;
  759. }
  760. if (space.utils.isNull(postData.body)) {
  761. space.message.warning("正文内容为空");
  762. return;
  763. }
  764. var _this = this;
  765. _this.previewLoading = true;
  766. $.ajax({
  767. url: _this.previewurl || "/apps?ark=website.article_preview",
  768. type: "POST",
  769. data: postData,
  770. dataType: "json",
  771. success: function (res) {
  772. if (res.code != "success") {
  773. space.message.error(res.msg || "生成预览失败");
  774. return;
  775. }
  776. var data = res.data || {};
  777. var baseUrl = data.url || data.previewUrl || "";
  778. if (space.utils.isNull(baseUrl)) {
  779. space.message.error("生成预览失败");
  780. return;
  781. }
  782. _this.previewUrl = baseUrl + (baseUrl.indexOf("?") == -1 ? "?" : "&") + "_t=" + new Date().getTime();
  783. _this.previewVisible = true;
  784. },
  785. error: function () {
  786. space.message.error("生成预览失败");
  787. },
  788. complete: function () {
  789. _this.previewLoading = false;
  790. }
  791. });
  792. },
  793. /**
  794. * 初始化编辑器
  795. */
  796. initEditor: function () {
  797. var toolbar = this.getToolbar();
  798. var _this = this;
  799. ClassicEditor.create(document.querySelector("#" + _this.id), {
  800. extraPlugins: [function (editor) {
  801. _this.installUploadAdapter(editor);
  802. }], // 添加上传适配器
  803. toolbar: toolbar || {},
  804. language: 'zh-cn',
  805. placeholder: _this.placeholder || '',
  806. mediaEmbed: {
  807. previewsInData: true,
  808. providers: [{
  809. name: 'spaceVideo',
  810. url: [
  811. /\.(mp4|webm|ogg)(\?.*)?$/i
  812. ],
  813. html: match => {
  814. const input = match['input'];
  815. return (
  816. `<video controls playsinline preload="metadata" src="${input}" style="max-width:100%;"></video>`
  817. );
  818. }
  819. }, {
  820. name: 'myprovider',
  821. url: [
  822. /^lizzy.*\.com.*\/media\/(\w+)/,
  823. /^www\.lizzy.*/,
  824. /^.*/
  825. ],
  826. html: match => {
  827. //获取媒体url
  828. const input = match['input'];
  829. return (
  830. `<iframe src="${input}" allowFullScreen="true" frameborder="0"></iframe>`
  831. );
  832. }
  833. }]
  834. }
  835. }).then(editor => {
  836. space.vars.editorReady = true;
  837. _this.editor = editor;
  838. _this.alignFullScreenToolbarButton();
  839. _this.bindVideoPasteUpload();
  840. if (_this.htmlContent != undefined) {
  841. _this.editor.setData(_this.htmlContent);
  842. }
  843. /**
  844. * 禁止在编辑器内回车事件
  845. */
  846. editor.ui.view.element.onkeydown = function (event) {
  847. // 回车键
  848. if (event.keyCode == 13) {
  849. event.stopPropagation();
  850. return false;
  851. }
  852. // tab 转空格
  853. if (event.keyCode == 9) {
  854. const model = editor.model;
  855. model.change(writer => {
  856. const ele = writer.createElement('span');
  857. writer.insertText(" ", ele);
  858. // 一定要 Element
  859. model.insertContent(ele);
  860. });
  861. event.stopPropagation();
  862. return false;
  863. }
  864. _this.$emit("input", editor.getData());
  865. };
  866. // 设置编辑器宽度
  867. if (_this.width) {
  868. $(editor.ui.view.element).css({
  869. 'width': _this.width
  870. })
  871. }
  872. // 监听编辑器的内容变动事件
  873. editor.model.document.on('change:data', function () {
  874. _this.$emit("input", editor.getData());
  875. });
  876. }).catch(error => {
  877. console.log(error);
  878. });
  879. }
  880. },
  881. template: `
  882. <div class="space-editor-wrap">
  883. <div class="space-editor-toolstrip" v-if="showEditorTools()">
  884. <el-button class="space-editor-tool-btn" size="small" plain icon="el-icon-magic-stick" @click="formatContent">一键排版</el-button>
  885. <el-button class="space-editor-tool-btn" size="small" plain icon="el-icon-video-camera" @click="openVideoPicker" :loading="uploading">视频上传</el-button>
  886. <span class="space-editor-tool-divider"></span>
  887. <el-select class="space-editor-quality" v-model="imageQuality" size="small" placeholder="图片质量">
  888. <el-option label="原图" value="origin"></el-option>
  889. <el-option label="高清 85%" value="high"></el-option>
  890. <el-option label="标准 70%" value="standard"></el-option>
  891. <el-option label="低流量 50%" value="low"></el-option>
  892. </el-select>
  893. <el-button class="space-editor-tool-btn space-editor-preview-btn" size="small" type="primary" plain icon="el-icon-view" @click="openArticlePreview" :loading="previewLoading">三端预览</el-button>
  894. <span class="space-editor-upload-status" v-if="uploadStatus">{{uploadStatus}}</span>
  895. </div>
  896. <input ref="videoInput" type="file" accept="video/*" style="display:none;" @change="handleVideoSelect">
  897. <div :id="id">
  898. <script type="text/plain"></script>
  899. </div>
  900. <el-dialog title="正文预览" :visible.sync="previewVisible" width="90%" top="4vh" append-to-body custom-class="space-editor-preview-dialog">
  901. <div class="space-editor-preview-toolbar">
  902. <el-radio-group v-model="previewMode" size="small">
  903. <el-radio-button label="pc">PC端</el-radio-button>
  904. <el-radio-button label="pad">平板端</el-radio-button>
  905. <el-radio-button label="mobile">手机端</el-radio-button>
  906. </el-radio-group>
  907. <el-button size="small" icon="el-icon-refresh" @click="openArticlePreview" :loading="previewLoading">刷新预览</el-button>
  908. </div>
  909. <div class="space-editor-preview-stage">
  910. <iframe v-if="previewUrl" :key="previewMode + previewUrl" :src="previewUrl" :style="previewFrameStyle" frameborder="0"></iframe>
  911. </div>
  912. </el-dialog>
  913. </div>
  914. `
  915. })
  916. /**
  917. * CMS
  918. */
  919. Vue.component('spaceCms', {
  920. data: function () {
  921. return {
  922. fileCount: 0, // 文件总数
  923. }
  924. },
  925. name: "spaceCms",
  926. model: {
  927. prop: 'cmsid',
  928. evnet: 'click'
  929. },
  930. props: {
  931. // v-model
  932. cmsid: {
  933. type: [String],
  934. required: true
  935. },
  936. // 文件类型
  937. accept: {
  938. type: String
  939. },
  940. // 最大上传数量,默认199
  941. maxsize: {
  942. type: [String, Number],
  943. default: 199
  944. },
  945. },
  946. created: function () {
  947. this.getCmsFileCount(this.cmsid);
  948. },
  949. watch: {
  950. cmsid: function (newVal, oldVal) {
  951. this.getCmsFileCount(newVal);
  952. }
  953. },
  954. methods: {
  955. open: function () {
  956. var that = this;
  957. space.plugins.cms.openCMS({
  958. cmsid: that.cmsid,
  959. accept: that.accept,
  960. maxsize: that.maxsize
  961. }, that.callback);
  962. },
  963. callback: function (data, callback2) {
  964. this.$emit("input", data.cmsid);
  965. this.cmsid = data.cmsid;
  966. this.fileCount = data.fileCount;
  967. callback2();
  968. },
  969. getCmsFileCount: function (cmsid) {
  970. if (space.utils.isNull(cmsid)) {
  971. return;
  972. }
  973. var that = this;
  974. space.utils.ajax({
  975. url: '/apps?ark=sys.cms_getCmsInfo',
  976. data: {
  977. "idcard": cmsid
  978. },
  979. dataType: 'json',
  980. loadLayer: false,
  981. autoTip: false,
  982. async: false, // 同步请求
  983. callback: function (data) {
  984. if (data.code == "success") {
  985. that.fileCount = data.data.filecount;
  986. }
  987. }
  988. });
  989. }
  990. },
  991. template: `<div class="cms-upload">
  992. <el-badge :value="fileCount" type="primary" v-show="fileCount > 0">
  993. <el-button icon="el-icon-upload2" @click="open" >上传文件</el-button>
  994. </el-badge>
  995. <el-button icon="el-icon-upload2" v-show="fileCount == 0" @click="open" >上传文件</el-button>
  996. </div>`
  997. })
  998. /**
  999. * CMS预览
  1000. */
  1001. Vue.component('spaceCmsView', {
  1002. data: function () {
  1003. return {
  1004. thumbnail: "",
  1005. filename: "",
  1006. filecount: 0,
  1007. isShowEl: true, //是否显示整个组件元素
  1008. }
  1009. },
  1010. name: "spaceCmsView",
  1011. /**
  1012. * cmsid: 绑定的值,非空
  1013. * thumbnail: 缩略图
  1014. * filename: 文件名
  1015. * filecount: 文件数
  1016. */
  1017. props: {
  1018. cmsid: {
  1019. type: [String, Number],
  1020. required: true
  1021. }
  1022. },
  1023. created: function () {
  1024. var that = this;
  1025. if (space.utils.isNull(that.cmsid)) {
  1026. // 不显示整个组件元素
  1027. that.isShowEl = false;
  1028. return;
  1029. }
  1030. space.utils.ajax({
  1031. url: '/apps?ark=sys.cms_getCmsInfo',
  1032. data: {
  1033. "idcard": that.cmsid
  1034. },
  1035. dataType: 'json',
  1036. loadLayer: false,
  1037. autoTip: false,
  1038. async: false, // 同步请求
  1039. callback: function (data) {
  1040. if (data.code == "success") {
  1041. that.thumbnail = data.data.thumbnail;
  1042. that.filename = data.data.filename;
  1043. that.filecount = data.data.filecount;
  1044. } else if (data.code == "warning") {
  1045. // 不显示整个组件元素
  1046. that.isShowEl = false;
  1047. }
  1048. }
  1049. });
  1050. },
  1051. methods: {
  1052. open: function () {
  1053. var that = this;
  1054. space.plugins.cms.openCMSView({
  1055. cmsid: that.cmsid
  1056. });
  1057. },
  1058. },
  1059. template: `
  1060. <div class="space-cms-view" v-if="isShowEl">
  1061. <div class="space-cms-view-wrap" @click="open">
  1062. <div class="space-cms-view-main">
  1063. <el-image :src="thumbnail">
  1064. <div slot="error" class="image-slot">
  1065. <i class="el-icon-picture-outline"></i>
  1066. </div>
  1067. </el-image>
  1068. <span>{{filename}}</span>
  1069. </div>
  1070. <div v-show="filecount >=2 " class="more-file"></div>
  1071. <div v-show="filecount >=2 " class="more-file more-file-bottom"></div>
  1072. </div>
  1073. </div>
  1074. `
  1075. })
  1076. /**
  1077. * 业务流转
  1078. */
  1079. Vue.component('spacePluginFlow', {
  1080. data: function () {
  1081. return {
  1082. isOpenFlowDrawer: false, // 是否打开抽屉
  1083. isLoadShow: true, // 是否显示加载动画
  1084. frameID: space.utils.randomStringAndNumber(6) + 'Frm', // frame 窗口ID
  1085. closeBtnID: space.utils.randomStringAndNumber(6) + 'Close', // 关闭按钮的ID
  1086. url: '/app/flow/time/', // url
  1087. title: '业务流转', // 标题
  1088. closeTitle: '收起', // 关闭按钮文本内容
  1089. }
  1090. },
  1091. name: "spacePluginFlow",
  1092. /**
  1093. * cmsid: 绑定的值,非空
  1094. * size: 抽屉大小,可传px、%,默认30%
  1095. */
  1096. props: {
  1097. audit: {
  1098. type: String,
  1099. required: true
  1100. },
  1101. apply: {
  1102. type: String,
  1103. required: true
  1104. },
  1105. open: {
  1106. type: Boolean,
  1107. },
  1108. size: {
  1109. type: String
  1110. },
  1111. },
  1112. created: function () {
  1113. this.url = this.url + "?auditid=" + this.audit + "&applyid=" + this.apply;
  1114. this.size = space.utils.isNull(this.size) ? '360px' : this.size;
  1115. this.isOpenFlowDrawer = space.utils.isNull(this.open) ? false : this.open;
  1116. this.showLoad();
  1117. },
  1118. mounted: function () {
  1119. var that = this;
  1120. // 监听窗口变化,计算关闭按钮的位置
  1121. window.onresize = function () {
  1122. // 抽屉打开状态
  1123. if (that.isOpenFlowDrawer) {
  1124. that.initCloseBtnLocation();
  1125. }
  1126. }
  1127. },
  1128. methods: {
  1129. /**
  1130. * 计算关闭按钮的坐标
  1131. */
  1132. initCloseBtnLocation: function () {
  1133. var offset = space.utils.getDomPosition(document.getElementById(this.frameID));
  1134. $("#" + this.closeBtnID).css("left", (offset.left - $("#" + this.closeBtnID).width()) + "px");
  1135. },
  1136. /**
  1137. * 显示抽屉
  1138. */
  1139. showDrawer: function () {
  1140. var that = this;
  1141. that.initCloseBtnLocation();
  1142. $("#" + that.closeBtnID).fadeIn(200);
  1143. // 为了更好的动画体验
  1144. setTimeout(() => {
  1145. $("#" + that.closeBtnID).css("z-index", "999999999999999");
  1146. }, 200);
  1147. },
  1148. /**
  1149. * 控制加载动画
  1150. */
  1151. showLoad: function () {
  1152. var that = this;
  1153. var loadInterval = setInterval(() => {
  1154. var frameEle = document.getElementById(that.frameID);
  1155. console.log("loadInterval");
  1156. frameEle.onload = function () {
  1157. window.clearInterval(loadInterval);
  1158. that.isLoadShow = false;
  1159. }
  1160. }, 100);
  1161. }
  1162. },
  1163. watch: {
  1164. isOpenFlowDrawer: function (newVal, oldVal) {
  1165. var that = this;
  1166. // 打开
  1167. if (newVal) {
  1168. var getFrameInterval = setInterval(() => {
  1169. var frameEle = document.getElementById(that.frameID);
  1170. if (!frameEle) {
  1171. return;
  1172. }
  1173. window.clearInterval(getFrameInterval);
  1174. that.showDrawer();
  1175. }, 200);
  1176. }
  1177. // 关闭
  1178. else {
  1179. $("#" + that.closeBtnID).hide();
  1180. $("#" + that.closeBtnID).css("z-index", "");
  1181. }
  1182. },
  1183. open: function (newVal, oldVal) {
  1184. this.isOpenFlowDrawer = space.utils.isNull(newVal) ? false : newVal;
  1185. }
  1186. },
  1187. template: `
  1188. <div class="space-app-plugin">
  1189. <div class="space-app-plugin-open disable-select animate__animated animate__slideInRight" v-show="!isOpenFlowDrawer" @click="isOpenFlowDrawer = true">{{title}}</div>
  1190. <div :id="closeBtnID" class="space-app-plugin-close disable-select animate__animated animate__slideInRight" @click="isOpenFlowDrawer = false">{{closeTitle}}</div>
  1191. <el-drawer :modal="false" :visible.sync="isOpenFlowDrawer" :size="size" :with-header="false" :wrapper-closable="false" >
  1192. <iframe :id="frameID" :src="url"></iframe>
  1193. <div class="space-app-plugin-load" v-show="isLoadShow" ><i class="el-icon-loading" ></i>加载中</div>
  1194. </el-drawer>
  1195. </div>
  1196. `
  1197. })