space.vue.subassembly.js 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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?v=20260628-pdf'
  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. videoPlaybackSyncTimer: undefined,
  373. }
  374. },
  375. name: "spaceEditor",
  376. model: {
  377. prop: 'htmlContent',
  378. evnet: 'click'
  379. },
  380. /**
  381. * htmlContent: v-model 绑定的值,非空,html 字符串
  382. * size: 编辑器的大小,small、medium、large
  383. * width: 编辑器的宽度,可空
  384. * height: 编辑器的高度,可空
  385. * placeholder:占位符
  386. * type:编辑器工具栏类型
  387. * isfullscreen:是否有全屏,
  388. */
  389. props: ['htmlContent', 'size', 'width', 'height', 'placeholder', 'type', 'isfullscreen', 'articlepreview', 'previewdata', 'previewurl'],
  390. computed: {
  391. previewFrameStyle: function () {
  392. var widthMap = {
  393. pc: '1200px',
  394. pad: '768px',
  395. mobile: '390px'
  396. };
  397. return {
  398. width: widthMap[this.previewMode] || widthMap.pc,
  399. height: '680px'
  400. };
  401. }
  402. },
  403. watch: {
  404. height: function (newVal, oldVal) {
  405. if (this.height && this.height.toString().toLocaleLowerCase().indexOf('px') == -1) {
  406. this.height += 'px';
  407. } else {
  408. this.setEditorHeight();
  409. }
  410. },
  411. editor: function (newVal, oldVal) {
  412. this.setEditorHeight();
  413. },
  414. jsUrlFlag: function (newVal, oldVal) {
  415. if (this.jsUrl.length > 0 && this.jsUrl.length == this.jsUrlFlag.length) {
  416. if (this.jsUrlFlag.indexOf('false') != -1) {
  417. return;
  418. }
  419. this.initEditor();
  420. this.notLoadEditor = false;
  421. this.setEditorHeight();
  422. }
  423. },
  424. notLoadEditor: function (newVal, oldVal) {
  425. this.setEditorHeight();
  426. },
  427. },
  428. created: function () {
  429. var _this = this;
  430. for (var i = 0; i < _this.jsUrl.length; i++) {
  431. _this.loadJS(_this.jsUrl[i], function () {
  432. _this.jsUrlFlag.push('true');
  433. });
  434. }
  435. },
  436. methods: {
  437. loadJS: function (url, callback) {
  438. for (var i = 0; i < document.getElementsByTagName('head')[0].getElementsByTagName('script').length; i++) {
  439. var src = document.getElementsByTagName('head')[0].getElementsByTagName('script')[i].getAttribute('src');
  440. if (url == src) {
  441. // 防止一个页面有多个编辑器时,编辑器加载失败
  442. if (space.vars.editorReady != true) {
  443. var winInterval = setInterval(function () {
  444. if (space.vars.editorReady != true) {
  445. return;
  446. }
  447. window.clearInterval(winInterval);
  448. if (callback) {
  449. callback();
  450. }
  451. }, 100)
  452. }
  453. return;
  454. }
  455. }
  456. var script = document.createElement('script');
  457. script.type = 'text/javascript';
  458. script.onload = function () {
  459. if (callback) {
  460. callback();
  461. }
  462. };
  463. script.src = url;
  464. document.getElementsByTagName('head')[0].appendChild(script);
  465. },
  466. isArticlePreview: function () {
  467. return this.articlepreview === true || this.articlepreview === 'true';
  468. },
  469. showEditorTools: function () {
  470. return this.type != 'flow' && this.isArticlePreview();
  471. },
  472. getUploadOptions: function () {
  473. return {
  474. imageQuality: this.imageQuality,
  475. imageMaxWidth: 1920,
  476. imageMaxSize: 20 * 1024 * 1024,
  477. videoMaxSize: 1024 * 1024 * 1024,
  478. pdfMaxSize: 100 * 1024 * 1024,
  479. fileMaxSize: 10 * 1024 * 1024
  480. };
  481. },
  482. myCustomUploadAdapterPlugin: function (editor) {
  483. this.installUploadAdapter(editor);
  484. },
  485. installUploadAdapter: function (editor) {
  486. var _this = this;
  487. editor.plugins.get('FileRepository').createUploadAdapter = (loader) => {
  488. // 第二个参数设置上传图片的地址
  489. return new MyUploadAdapter(loader, _this.getUploadOptions());
  490. };
  491. },
  492. installPdfViewerPlugin: function (editor) {
  493. editor.model.schema.register("spacePdfViewer", {
  494. isObject: true,
  495. allowWhere: "$block",
  496. allowAttributes: ["src", "title"]
  497. });
  498. var createPdfViewerView = function (modelItem, viewWriter) {
  499. var src = modelItem.getAttribute("src") || "";
  500. var title = modelItem.getAttribute("title") || "PDF文件";
  501. var figure = viewWriter.createContainerElement("figure", {
  502. "class": "space-editor-pdf-viewer media",
  503. "data-space-pdf": "true"
  504. });
  505. var iframe = viewWriter.createRawElement("iframe", {
  506. "class": "space-editor-pdf-frame",
  507. "src": src,
  508. "title": title,
  509. "loading": "lazy",
  510. "frameborder": "0"
  511. });
  512. var caption = viewWriter.createContainerElement("figcaption");
  513. var link = viewWriter.createContainerElement("a", {
  514. "href": src,
  515. "target": "_blank",
  516. "rel": "noopener"
  517. });
  518. var text = viewWriter.createText("打开 PDF:" + title);
  519. viewWriter.insert(viewWriter.createPositionAt(link, 0), text);
  520. viewWriter.insert(viewWriter.createPositionAt(caption, 0), link);
  521. viewWriter.insert(viewWriter.createPositionAt(figure, 0), iframe);
  522. viewWriter.insert(viewWriter.createPositionAt(figure, "end"), caption);
  523. return figure;
  524. };
  525. editor.conversion.for("editingDowncast").elementToElement({
  526. model: "spacePdfViewer",
  527. view: function (modelItem, conversionApi) {
  528. return createPdfViewerView(modelItem, conversionApi.writer);
  529. }
  530. });
  531. editor.conversion.for("dataDowncast").elementToElement({
  532. model: "spacePdfViewer",
  533. view: function (modelItem, conversionApi) {
  534. return createPdfViewerView(modelItem, conversionApi.writer);
  535. }
  536. });
  537. editor.conversion.for("upcast").elementToElement({
  538. view: {
  539. name: "iframe",
  540. classes: "space-editor-pdf-frame"
  541. },
  542. model: function (viewElement, conversionApi) {
  543. return conversionApi.writer.createElement("spacePdfViewer", {
  544. src: viewElement.getAttribute("src") || "",
  545. title: viewElement.getAttribute("title") || "PDF文件"
  546. });
  547. }
  548. });
  549. },
  550. uploadPlainFileByAdapter: function (file, options, onProgress) {
  551. if (typeof MyUploadAdapter == "undefined") {
  552. return Promise.reject("上传组件未加载完成,请刷新页面后重试");
  553. }
  554. if (typeof MyUploadAdapter.uploadPlainFile == "function") {
  555. return MyUploadAdapter.uploadPlainFile(file, options, onProgress);
  556. }
  557. var adapter = new MyUploadAdapter(null, options || {});
  558. if (typeof adapter.uploadPlainFile == "function") {
  559. return adapter.uploadPlainFile(file, onProgress);
  560. }
  561. return Promise.reject("上传组件未加载完成,请刷新页面后重试");
  562. },
  563. /**
  564. * 获取高度
  565. */
  566. getHeight: function () {
  567. if (!this.height) {
  568. if (this.size == undefined || this.size == "small") {
  569. return '200px';
  570. } else if (this.size == "medium") {
  571. return '400px';
  572. } else if (this.size == "large") {
  573. return '600px';
  574. }
  575. } else if (this.height) {
  576. if (this.height.toString().toLocaleLowerCase().indexOf('px') == -1) {
  577. return this.height + 'px';
  578. } else {
  579. return this.height;
  580. }
  581. }
  582. return "";
  583. },
  584. /**
  585. * 设置编辑器高度
  586. */
  587. setEditorHeight: function () {
  588. if (this.notLoadEditor) {
  589. return;
  590. }
  591. var height = this.getHeight();
  592. if (!this.editor || !height) {
  593. return;
  594. }
  595. var _this = this;
  596. // 设置编辑器高度
  597. _this.editor.editing.view.change(writer => {
  598. writer.setStyle(
  599. "height",
  600. height,
  601. _this.editor.editing.view.document.getRoot()
  602. );
  603. });
  604. },
  605. /**
  606. * 获取工具栏
  607. */
  608. getToolbar: function () {
  609. var items = [];
  610. // 是否有全屏工具
  611. if (this.isfullscreen == false) {
  612. // 没有全屏工具栏
  613. items = [
  614. 'fontfamily', 'fontsize', 'fontColor', 'fontBackgroundColor', 'blockQuote', 'bold', 'italic', 'link', '|',
  615. 'alignment', 'horizontalLine', '|', 'indent', 'outdent', '|', 'imageUpload', 'wordUpload', 'insertTable', 'mediaEmbed', 'phrase', 'undo', 'redo'
  616. ];
  617. } else {
  618. items = [
  619. 'fontfamily', 'fontsize', 'fontColor', 'fontBackgroundColor', 'blockQuote', 'bold', 'italic', 'link', '|',
  620. 'alignment', 'horizontalLine', '|', 'indent', 'outdent', '|', 'imageUpload', 'wordUpload', 'insertTable', 'mediaEmbed', 'phrase', 'undo', 'redo', 'fullScreen'
  621. ];
  622. }
  623. var toolbar = {
  624. items: items
  625. };
  626. if (!this.type) {
  627. return toolbar;
  628. }
  629. var map = new Map();
  630. map.set("flow", ['phrase']);
  631. if (map.get(this.type)) {
  632. return {
  633. items: map.get(this.type)
  634. }
  635. }
  636. return toolbar;
  637. },
  638. alignFullScreenToolbarButton: function () {
  639. if (!this.editor || !this.editor.ui || !this.editor.ui.view || !this.editor.ui.view.element) {
  640. return;
  641. }
  642. var toolbarItems = this.editor.ui.view.element.querySelector(".ck-toolbar__items");
  643. if (!toolbarItems) {
  644. return;
  645. }
  646. var buttons = toolbarItems.querySelectorAll(".ck-button");
  647. for (var i = 0; i < buttons.length; i++) {
  648. var label = (buttons[i].getAttribute("aria-label") || buttons[i].getAttribute("title") || buttons[i].textContent || "").replace(/\s/g, "");
  649. if (label.indexOf("全屏") != -1) {
  650. buttons[i].style.marginLeft = "auto";
  651. toolbarItems.appendChild(buttons[i]);
  652. return;
  653. }
  654. }
  655. },
  656. mountVideoUploadToolbarButton: function (retryCount) {
  657. if (!this.editor || !this.editor.ui || !this.editor.ui.view || !this.editor.ui.view.element) {
  658. return;
  659. }
  660. var toolbarItems = this.editor.ui.view.element.querySelector(".ck-toolbar__items");
  661. if (!toolbarItems) {
  662. return;
  663. }
  664. if (toolbarItems.querySelector(".space-editor-video-upload-ck-button")) {
  665. return;
  666. }
  667. var dropdowns = toolbarItems.querySelectorAll(".ck-dropdown");
  668. var mediaDropdown = null;
  669. for (var i = 0; i < dropdowns.length; i++) {
  670. var button = dropdowns[i].querySelector(".ck-button");
  671. var label = button ? (button.getAttribute("aria-label") || button.getAttribute("title") || button.textContent || "") : "";
  672. label = label.replace(/\s/g, "").toLowerCase();
  673. if (label.indexOf("媒体") != -1 || label.indexOf("media") != -1) {
  674. mediaDropdown = dropdowns[i];
  675. break;
  676. }
  677. }
  678. if (!mediaDropdown) {
  679. if ((retryCount || 0) < 3) {
  680. var _this = this;
  681. setTimeout(function () {
  682. _this.mountVideoUploadToolbarButton((retryCount || 0) + 1);
  683. }, 200);
  684. }
  685. return;
  686. }
  687. var _this = this;
  688. var videoButton = document.createElement("button");
  689. videoButton.type = "button";
  690. videoButton.className = "ck ck-button ck-off space-editor-video-upload-ck-button";
  691. videoButton.setAttribute("aria-label", "上传视频");
  692. videoButton.setAttribute("title", "上传视频");
  693. videoButton.innerHTML = '<svg class="ck ck-icon ck-button__icon space-editor-video-upload-icon" viewBox="0 0 20 20" aria-hidden="true"><path d="M3.2 5.4c-.9 0-1.6.7-1.6 1.6v6.2c0 .9.7 1.6 1.6 1.6h8.2c.9 0 1.6-.7 1.6-1.6V7c0-.9-.7-1.6-1.6-1.6H3.2zm.2 1.8h7.8V13H3.4V7.2z"></path><path d="M13.9 8.2l2.9-2c.7-.5 1.6 0 1.6.9v6c0 .8-.9 1.3-1.6.9l-2.9-2V8.2z"></path><path d="M7.6 12.2h1.8V9.3l1.1 1.1 1.2-1.2-3.2-3-3.2 3 1.2 1.2 1.1-1.1v2.9z"></path></svg><span class="ck ck-button__label">上传视频</span>';
  694. videoButton.addEventListener("click", function (event) {
  695. event.preventDefault();
  696. event.stopPropagation();
  697. _this.openVideoPicker();
  698. });
  699. mediaDropdown.classList.add("space-editor-media-link-dropdown");
  700. mediaDropdown.parentNode.insertBefore(videoButton, mediaDropdown);
  701. },
  702. mountPdfUploadToolbarButton: function (retryCount) {
  703. if (!this.editor || !this.editor.ui || !this.editor.ui.view || !this.editor.ui.view.element) {
  704. return;
  705. }
  706. var toolbarItems = this.editor.ui.view.element.querySelector(".ck-toolbar__items");
  707. if (!toolbarItems) {
  708. return;
  709. }
  710. if (toolbarItems.querySelector(".space-editor-pdf-upload-ck-button")) {
  711. return;
  712. }
  713. var dropdowns = toolbarItems.querySelectorAll(".ck-dropdown");
  714. var mediaDropdown = null;
  715. for (var i = 0; i < dropdowns.length; i++) {
  716. var button = dropdowns[i].querySelector(".ck-button");
  717. var label = button ? (button.getAttribute("aria-label") || button.getAttribute("title") || button.textContent || "") : "";
  718. label = label.replace(/\s/g, "").toLowerCase();
  719. if (label.indexOf("媒体") != -1 || label.indexOf("media") != -1) {
  720. mediaDropdown = dropdowns[i];
  721. break;
  722. }
  723. }
  724. if (!mediaDropdown) {
  725. if ((retryCount || 0) < 3) {
  726. var _this = this;
  727. setTimeout(function () {
  728. _this.mountPdfUploadToolbarButton((retryCount || 0) + 1);
  729. }, 200);
  730. }
  731. return;
  732. }
  733. var _this = this;
  734. var pdfButton = document.createElement("button");
  735. pdfButton.type = "button";
  736. pdfButton.className = "ck ck-button ck-off space-editor-upload-ck-button space-editor-pdf-upload-ck-button";
  737. pdfButton.setAttribute("aria-label", "上传PDF");
  738. pdfButton.setAttribute("title", "上传PDF");
  739. pdfButton.innerHTML = '<svg class="ck ck-icon ck-button__icon space-editor-pdf-upload-icon" viewBox="0 0 20 20" aria-hidden="true"><path d="M4 1.8h7.2L16 6.6v11.6H4V1.8zm7.6 1.9v3.5h3.5L11.6 3.7zM6 9.4h8v1.4H6V9.4zm0 2.7h8v1.4H6v-1.4zm0 2.7h5.2v1.4H6v-1.4z"></path><path d="M7.1 6.9h1.3c.8 0 1.3-.4 1.3-1.1 0-.7-.5-1.1-1.3-1.1H6v3.9h1.1V6.9zm0-.8v-.6h1.1c.2 0 .4.1.4.3 0 .2-.1.3-.4.3H7.1z"></path></svg><span class="ck ck-button__label">上传PDF</span>';
  740. pdfButton.addEventListener("click", function (event) {
  741. event.preventDefault();
  742. event.stopPropagation();
  743. _this.openPdfPicker();
  744. });
  745. mediaDropdown.classList.add("space-editor-media-link-dropdown");
  746. mediaDropdown.parentNode.insertBefore(pdfButton, mediaDropdown);
  747. },
  748. formatContent: function () {
  749. if (!this.editor) {
  750. return;
  751. }
  752. var html = this.editor.getData();
  753. if (space.utils.isNull(html)) {
  754. space.message.warning("正文内容为空");
  755. return;
  756. }
  757. var wrapper = document.createElement("div");
  758. wrapper.innerHTML = html;
  759. var all = wrapper.querySelectorAll("*");
  760. for (var i = 0; i < all.length; i++) {
  761. var node = all[i];
  762. node.removeAttribute("lang");
  763. node.removeAttribute("width");
  764. node.removeAttribute("height");
  765. node.removeAttribute("onload");
  766. node.removeAttribute("border");
  767. node.removeAttribute("vspace");
  768. node.removeAttribute("hspace");
  769. var style = node.getAttribute("style") || "";
  770. if (/mso-|font-family|text-indent:\s*0|background:\s*transparent/i.test(style)) {
  771. node.removeAttribute("style");
  772. }
  773. }
  774. var inlineNodes = wrapper.querySelectorAll("span, font");
  775. for (var s = inlineNodes.length - 1; s >= 0; s--) {
  776. var inlineNode = inlineNodes[s];
  777. if (inlineNode.attributes.length == 0 && inlineNode.parentNode) {
  778. while (inlineNode.firstChild) {
  779. inlineNode.parentNode.insertBefore(inlineNode.firstChild, inlineNode);
  780. }
  781. inlineNode.parentNode.removeChild(inlineNode);
  782. }
  783. }
  784. var emptyBlocks = wrapper.querySelectorAll("p, div");
  785. for (var e = emptyBlocks.length - 1; e >= 0; e--) {
  786. var block = emptyBlocks[e];
  787. var hasMedia = block.querySelector("img, video, iframe, object, embed, table, .space-editor-pdf-viewer");
  788. var text = (block.textContent || "").replace(/\u00a0/g, "").trim();
  789. if (!hasMedia && text == "" && block.parentNode) {
  790. block.parentNode.removeChild(block);
  791. }
  792. }
  793. var headings = wrapper.querySelectorAll("h1, h2, h3, h4, h5, h6");
  794. for (var headingIndex = 0; headingIndex < headings.length; headingIndex++) {
  795. headings[headingIndex].style.textAlign = "center";
  796. headings[headingIndex].style.textIndent = "0";
  797. }
  798. var findFirstTextNode = function (node) {
  799. for (var child = node.firstChild; child; child = child.nextSibling) {
  800. if (child.nodeType == 3 && child.nodeValue.replace(/[\s\u00a0\u3000]/g, "") != "") {
  801. return child;
  802. }
  803. if (child.nodeType == 1 && !/^(img|video|iframe|object|embed|table)$/i.test(child.tagName)) {
  804. var textNode = findFirstTextNode(child);
  805. if (textNode) {
  806. return textNode;
  807. }
  808. }
  809. }
  810. return null;
  811. };
  812. var trimFirstTextIndent = function (node) {
  813. var textNode = findFirstTextNode(node);
  814. if (textNode) {
  815. textNode.nodeValue = textNode.nodeValue.replace(/^[\s\u00a0\u3000]+/, "");
  816. }
  817. };
  818. var textBlocks = wrapper.querySelectorAll("p, div");
  819. for (var blockIndex = 0; blockIndex < textBlocks.length; blockIndex++) {
  820. var textBlock = textBlocks[blockIndex];
  821. var blockHasMedia = textBlock.querySelector("img, video, iframe, object, embed, table, .space-editor-pdf-viewer");
  822. var blockText = (textBlock.textContent || "").replace(/\u00a0/g, "").trim();
  823. if (blockHasMedia) {
  824. textBlock.style.textAlign = "center";
  825. textBlock.style.textIndent = "0";
  826. } else if (blockText != "") {
  827. textBlock.style.textAlign = "justify";
  828. textBlock.style.textIndent = "2em";
  829. textBlock.style.lineHeight = "1.8";
  830. trimFirstTextIndent(textBlock);
  831. }
  832. }
  833. var images = wrapper.querySelectorAll("img");
  834. for (var imgIndex = 0; imgIndex < images.length; imgIndex++) {
  835. images[imgIndex].style.maxWidth = "100%";
  836. images[imgIndex].style.height = "auto";
  837. images[imgIndex].style.display = "block";
  838. images[imgIndex].style.margin = "12px auto";
  839. }
  840. var videos = wrapper.querySelectorAll("video, iframe, object, embed");
  841. for (var v = 0; v < videos.length; v++) {
  842. videos[v].style.maxWidth = "100%";
  843. if (videos[v].tagName.toLowerCase() == "video") {
  844. videos[v].setAttribute("controls", "controls");
  845. videos[v].setAttribute("playsinline", "playsinline");
  846. videos[v].style.display = "block";
  847. videos[v].style.margin = "12px auto";
  848. }
  849. }
  850. var tables = wrapper.querySelectorAll("table");
  851. for (var t = 0; t < tables.length; t++) {
  852. tables[t].style.width = "100%";
  853. tables[t].style.borderCollapse = "collapse";
  854. }
  855. this.editor.setData(wrapper.innerHTML);
  856. this.$emit("input", this.editor.getData());
  857. this.deferSyncEditorPlayableVideos();
  858. space.message.success("正文已完成排版整理");
  859. },
  860. insertHtml: function (html) {
  861. if (!this.editor || space.utils.isNull(html)) {
  862. return;
  863. }
  864. var viewFragment = this.editor.data.processor.toView(html);
  865. var modelFragment = this.editor.data.toModel(viewFragment);
  866. this.editor.model.insertContent(modelFragment);
  867. this.$emit("input", this.editor.getData());
  868. this.deferSyncEditorPlayableVideos();
  869. },
  870. escapeHtmlAttr: function (value) {
  871. return String(value || "").replace(/[&<>"']/g, function (text) {
  872. return {
  873. "&": "&amp;",
  874. "<": "&lt;",
  875. ">": "&gt;",
  876. "\"": "&quot;",
  877. "'": "&#39;"
  878. }[text];
  879. });
  880. },
  881. normalizeLocalUploadUrl: function (url) {
  882. var text = String(url || "").trim().replace(/\\/g, "/");
  883. var match = text.match(/^(?:https?:)?\/\/[^/]*(\/web\/upload\/.*)$/i);
  884. if (match) {
  885. text = match[1];
  886. }
  887. text = text.replace(/^(https?:)\/+(\/web\/upload\/.*)$/i, "$2");
  888. text = text.replace(/(\/web\/upload\/modules)\/+/i, "$1/");
  889. return text;
  890. },
  891. toAbsoluteEditorMediaUrl: function (url) {
  892. var text = String(url || "").trim();
  893. if (/^(https?:)?\/\//i.test(text) || /^(data|blob):/i.test(text)) {
  894. return text;
  895. }
  896. if (text.charAt(0) == "/") {
  897. return window.location.origin + text;
  898. }
  899. return text;
  900. },
  901. openVideoPicker: function () {
  902. if (this.$refs.videoInput) {
  903. this.$refs.videoInput.click();
  904. }
  905. },
  906. openPdfPicker: function () {
  907. if (this.$refs.pdfInput) {
  908. this.$refs.pdfInput.click();
  909. }
  910. },
  911. handleVideoSelect: function (event) {
  912. var files = Array.prototype.slice.call(event.target.files || []);
  913. event.target.value = "";
  914. if (files.length > 0) {
  915. this.uploadVideoFile(files[0]);
  916. }
  917. },
  918. handlePdfSelect: function (event) {
  919. var files = Array.prototype.slice.call(event.target.files || []);
  920. event.target.value = "";
  921. if (files.length > 0) {
  922. this.uploadPdfFile(files[0]);
  923. }
  924. },
  925. isPdfFile: function (file) {
  926. return !!file && ((file.type && file.type.toLowerCase() == "application/pdf") || /\.pdf$/i.test(file.name || ""));
  927. },
  928. uploadVideoFile: function (file) {
  929. if (!file || !file.type || file.type.indexOf("video/") !== 0) {
  930. space.message.warning("请选择视频文件");
  931. return;
  932. }
  933. var _this = this;
  934. _this.uploading = true;
  935. _this.uploadStatus = "正在上传视频";
  936. _this.uploadPlainFileByAdapter(file, {
  937. imageQuality: 'origin'
  938. }, function (loaded, total) {
  939. if (total > 0) {
  940. _this.uploadStatus = "正在上传视频 " + Math.round(loaded * 100 / total) + "%";
  941. }
  942. }).then(function (res) {
  943. var videoUrl = _this.normalizeLocalUploadUrl(res.default);
  944. _this.insertHtml('<p><video controls playsinline preload="metadata" src="' + _this.escapeHtmlAttr(videoUrl) + '" style="max-width:100%;"></video></p>');
  945. _this.$emit("input", _this.editor.getData());
  946. space.message.success("视频已插入正文");
  947. }).catch(function (msg) {
  948. space.message.error(msg || "视频上传失败");
  949. }).finally(function () {
  950. _this.uploading = false;
  951. _this.uploadStatus = "";
  952. });
  953. },
  954. uploadPdfFile: function (file) {
  955. if (!this.isPdfFile(file)) {
  956. space.message.warning("请选择PDF文件");
  957. return;
  958. }
  959. var _this = this;
  960. _this.uploading = true;
  961. _this.uploadStatus = "正在上传PDF";
  962. _this.uploadPlainFileByAdapter(file, {
  963. imageQuality: 'origin',
  964. pdfMaxSize: 100 * 1024 * 1024
  965. }, function (loaded, total) {
  966. if (total > 0) {
  967. _this.uploadStatus = "正在上传PDF " + Math.round(loaded * 100 / total) + "%";
  968. }
  969. }).then(function (res) {
  970. var pdfUrl = _this.normalizeLocalUploadUrl(res.default);
  971. _this.insertPdfViewer(pdfUrl, file.name);
  972. _this.$emit("input", _this.editor.getData());
  973. space.message.success("PDF已插入正文");
  974. }).catch(function (msg) {
  975. space.message.error(msg || "PDF上传失败");
  976. }).finally(function () {
  977. _this.uploading = false;
  978. _this.uploadStatus = "";
  979. });
  980. },
  981. buildPdfViewerHtml: function (url, fileName) {
  982. var safeUrl = this.escapeHtmlAttr(url);
  983. var safeName = this.escapeHtmlAttr(fileName || "PDF文件");
  984. return '<figure class="space-editor-pdf-viewer media" data-space-pdf="true">'
  985. + '<iframe class="space-editor-pdf-frame" src="' + safeUrl + '" title="' + safeName + '" loading="lazy"></iframe>'
  986. + '<figcaption><a href="' + safeUrl + '" target="_blank" rel="noopener">打开 PDF:' + safeName + '</a></figcaption>'
  987. + '</figure><p></p>';
  988. },
  989. insertPdfViewer: function (url, fileName) {
  990. if (!this.editor || space.utils.isNull(url)) {
  991. return;
  992. }
  993. var title = fileName || "PDF文件";
  994. try {
  995. this.editor.model.change(function (writer) {
  996. var pdfViewer = writer.createElement("spacePdfViewer", {
  997. src: url,
  998. title: title
  999. });
  1000. this.editor.model.insertContent(pdfViewer);
  1001. }.bind(this));
  1002. this.$emit("input", this.editor.getData());
  1003. this.deferSyncEditorPlayableVideos();
  1004. return;
  1005. } catch (e) {
  1006. console.log(e);
  1007. }
  1008. if (this.editor.commands && this.editor.commands.get("mediaEmbed")) {
  1009. this.editor.execute("mediaEmbed", this.toAbsoluteEditorMediaUrl(url));
  1010. this.deferSyncEditorPlayableVideos();
  1011. return;
  1012. }
  1013. this.insertHtml(this.buildPdfViewerHtml(url, title));
  1014. },
  1015. buildPdfLinkHtml: function (url, fileName) {
  1016. var safeUrl = this.escapeHtmlAttr(url);
  1017. var safeName = this.escapeHtmlAttr(fileName || "PDF文件");
  1018. return '<p class="space-editor-pdf-link"><a href="' + safeUrl
  1019. + '" target="_blank" rel="noopener">打开 PDF:' + safeName + '</a></p>';
  1020. },
  1021. upgradePdfLinksToViewers: function () {
  1022. if (!this.editor) {
  1023. return;
  1024. }
  1025. var data = this.editor.getData();
  1026. if (data.indexOf(".pdf") == -1) {
  1027. return;
  1028. }
  1029. var wrapper = document.createElement("div");
  1030. wrapper.innerHTML = data;
  1031. var links = wrapper.querySelectorAll(".space-editor-pdf-link a[href], a[href]");
  1032. var changed = false;
  1033. for (var i = 0; i < links.length; i++) {
  1034. var link = links[i];
  1035. var href = link.getAttribute("href") || "";
  1036. var text = (link.textContent || "").replace(/\s+/g, " ").trim();
  1037. var isPdf = /\.pdf(\?.*)?$/i.test(href);
  1038. var isEditorPdfLink = link.closest(".space-editor-pdf-link") || /^打开\s*PDF[::]/i.test(text);
  1039. if (!isPdf || !isEditorPdfLink) {
  1040. continue;
  1041. }
  1042. var title = text.replace(/^打开\s*PDF[::]\s*/i, "") || href.split("/").pop() || "PDF文件";
  1043. var holder = document.createElement("div");
  1044. holder.innerHTML = this.buildPdfViewerHtml(href, title);
  1045. var viewer = holder.firstElementChild;
  1046. var replaceTarget = link.closest(".space-editor-pdf-link") || link.parentNode;
  1047. if (viewer && replaceTarget && replaceTarget.parentNode) {
  1048. replaceTarget.parentNode.replaceChild(viewer, replaceTarget);
  1049. changed = true;
  1050. }
  1051. }
  1052. if (changed) {
  1053. this.editor.setData(wrapper.innerHTML);
  1054. this.$emit("input", this.editor.getData());
  1055. this.deferSyncEditorPlayableVideos();
  1056. }
  1057. },
  1058. bindVideoPasteUpload: function () {
  1059. if (!this.editor || !this.editor.ui || !this.editor.ui.getEditableElement) {
  1060. return;
  1061. }
  1062. var editable = this.editor.ui.getEditableElement();
  1063. if (!editable || editable.getAttribute("data-space-video-upload") == "true") {
  1064. return;
  1065. }
  1066. editable.setAttribute("data-space-video-upload", "true");
  1067. var _this = this;
  1068. var handler = function (event) {
  1069. var dataTransfer = event.clipboardData || event.dataTransfer;
  1070. if (!dataTransfer || !dataTransfer.files || dataTransfer.files.length == 0) {
  1071. return;
  1072. }
  1073. var files = Array.prototype.slice.call(dataTransfer.files);
  1074. var videoFile = files.find(function (file) {
  1075. return file && file.type && file.type.indexOf("video/") === 0;
  1076. });
  1077. var pdfFile = files.find(function (file) {
  1078. return _this.isPdfFile(file);
  1079. });
  1080. if (videoFile) {
  1081. event.preventDefault();
  1082. _this.uploadVideoFile(videoFile);
  1083. } else if (pdfFile) {
  1084. event.preventDefault();
  1085. _this.uploadPdfFile(pdfFile);
  1086. }
  1087. };
  1088. editable.addEventListener("paste", handler);
  1089. editable.addEventListener("drop", handler);
  1090. },
  1091. findClosestEditorMedia: function (target) {
  1092. if (!target) {
  1093. return null;
  1094. }
  1095. if (target.nodeType == 3) {
  1096. target = target.parentNode;
  1097. }
  1098. if (!target || target.nodeType != 1) {
  1099. return null;
  1100. }
  1101. if (/^(video|iframe)$/i.test(target.tagName)) {
  1102. return target;
  1103. }
  1104. if (target.closest) {
  1105. return target.closest("video, iframe");
  1106. }
  1107. return null;
  1108. },
  1109. prepareEditorPlayableMedia: function (media) {
  1110. if (!media || media.nodeType != 1) {
  1111. return;
  1112. }
  1113. var tagName = (media.tagName || "").toLowerCase();
  1114. if (tagName == "video") {
  1115. media.setAttribute("controls", "controls");
  1116. media.setAttribute("playsinline", "playsinline");
  1117. media.setAttribute("webkit-playsinline", "webkit-playsinline");
  1118. if (!media.getAttribute("preload")) {
  1119. media.setAttribute("preload", "metadata");
  1120. }
  1121. }
  1122. media.classList.add("space-editor-playable-media");
  1123. var mediaWidget = media.closest ? media.closest("figure.media") : null;
  1124. if (mediaWidget) {
  1125. mediaWidget.classList.add("space-editor-playable-media-widget");
  1126. }
  1127. },
  1128. bindPlayableMediaNativeEvents: function (media) {
  1129. if (!media || media.getAttribute("data-space-playable-media-bound") == "true") {
  1130. return;
  1131. }
  1132. this.prepareEditorPlayableMedia(media);
  1133. media.setAttribute("data-space-playable-media-bound", "true");
  1134. var _this = this;
  1135. var stopWidgetSelection = function (event) {
  1136. _this.prepareEditorPlayableMedia(media);
  1137. event.stopPropagation();
  1138. };
  1139. var eventNames = ["pointerdown", "pointerup", "mousedown", "mouseup", "click", "dblclick", "touchstart", "touchend"];
  1140. for (var i = 0; i < eventNames.length; i++) {
  1141. media.addEventListener(eventNames[i], stopWidgetSelection);
  1142. }
  1143. },
  1144. syncEditorPlayableVideos: function () {
  1145. if (!this.editor || !this.editor.ui || !this.editor.ui.getEditableElement) {
  1146. return;
  1147. }
  1148. var editable = this.editor.ui.getEditableElement();
  1149. if (!editable) {
  1150. return;
  1151. }
  1152. var medias = editable.querySelectorAll("video, iframe");
  1153. for (var i = 0; i < medias.length; i++) {
  1154. this.bindPlayableMediaNativeEvents(medias[i]);
  1155. }
  1156. },
  1157. deferSyncEditorPlayableVideos: function () {
  1158. if (this.videoPlaybackSyncTimer) {
  1159. window.clearTimeout(this.videoPlaybackSyncTimer);
  1160. }
  1161. var _this = this;
  1162. this.videoPlaybackSyncTimer = window.setTimeout(function () {
  1163. _this.syncEditorPlayableVideos();
  1164. _this.videoPlaybackSyncTimer = undefined;
  1165. }, 0);
  1166. },
  1167. bindEditorVideoPlayback: function () {
  1168. if (!this.editor || !this.editor.ui || !this.editor.ui.getEditableElement) {
  1169. return;
  1170. }
  1171. var editable = this.editor.ui.getEditableElement();
  1172. if (!editable || editable.getAttribute("data-space-playable-media-root") == "true") {
  1173. this.syncEditorPlayableVideos();
  1174. return;
  1175. }
  1176. editable.setAttribute("data-space-playable-media-root", "true");
  1177. var _this = this;
  1178. var stopNativeWidgetSelection = function (event) {
  1179. var media = _this.findClosestEditorMedia(event.target);
  1180. if (!media) {
  1181. return;
  1182. }
  1183. _this.prepareEditorPlayableMedia(media);
  1184. event.stopPropagation();
  1185. };
  1186. ["pointerdown", "pointerup", "mousedown", "mouseup", "click", "dblclick", "touchstart", "touchend"].forEach(function (eventName) {
  1187. editable.addEventListener(eventName, stopNativeWidgetSelection);
  1188. });
  1189. if (this.editor.editing && this.editor.editing.view && this.editor.editing.view.document) {
  1190. ["mousedown", "mouseup", "click", "dblclick", "touchstart", "touchend"].forEach(function (eventName) {
  1191. _this.editor.editing.view.document.on(eventName, function (evt, data) {
  1192. var domTarget = data ? (data.domTarget || (data.domEvent ? data.domEvent.target : null)) : null;
  1193. var media = _this.findClosestEditorMedia(domTarget);
  1194. if (!media) {
  1195. return;
  1196. }
  1197. _this.prepareEditorPlayableMedia(media);
  1198. evt.stop();
  1199. }, { priority: "highest" });
  1200. });
  1201. }
  1202. this.syncEditorPlayableVideos();
  1203. },
  1204. getPreviewPostData: function () {
  1205. var data = this.previewdata || {};
  1206. return {
  1207. title: data.title || '',
  1208. source: data.source || '',
  1209. navigationid: data.navigationid || '',
  1210. desc: data.desc || '',
  1211. body: this.editor ? this.editor.getData() : (this.htmlContent || '')
  1212. };
  1213. },
  1214. openArticlePreview: function () {
  1215. if (!this.editor) {
  1216. return;
  1217. }
  1218. var postData = this.getPreviewPostData();
  1219. if (space.utils.isNull(postData.navigationid)) {
  1220. space.message.warning("请先选择发布栏目");
  1221. return;
  1222. }
  1223. if (space.utils.isNull(postData.body)) {
  1224. space.message.warning("正文内容为空");
  1225. return;
  1226. }
  1227. var _this = this;
  1228. _this.previewLoading = true;
  1229. $.ajax({
  1230. url: _this.previewurl || "/apps?ark=website.article_preview",
  1231. type: "POST",
  1232. data: postData,
  1233. dataType: "json",
  1234. success: function (res) {
  1235. if (res.code != "success") {
  1236. space.message.error(res.msg || "生成预览失败");
  1237. return;
  1238. }
  1239. var data = res.data || {};
  1240. var baseUrl = data.url || data.previewUrl || "";
  1241. if (space.utils.isNull(baseUrl)) {
  1242. space.message.error("生成预览失败");
  1243. return;
  1244. }
  1245. _this.previewUrl = baseUrl + (baseUrl.indexOf("?") == -1 ? "?" : "&") + "_t=" + new Date().getTime();
  1246. _this.previewVisible = true;
  1247. },
  1248. error: function () {
  1249. space.message.error("生成预览失败");
  1250. },
  1251. complete: function () {
  1252. _this.previewLoading = false;
  1253. }
  1254. });
  1255. },
  1256. setPreviewMode: function (mode) {
  1257. this.previewMode = mode || 'pc';
  1258. },
  1259. handlePreviewFrameLoad: function (event) {
  1260. var frame = event ? event.target : null;
  1261. var _this = this;
  1262. this.scrollPreviewFrameToArticle(frame);
  1263. window.setTimeout(function () {
  1264. _this.scrollPreviewFrameToArticle(frame);
  1265. }, 220);
  1266. },
  1267. scrollPreviewFrameToArticle: function (frame) {
  1268. if (!frame || !frame.contentWindow || !frame.contentWindow.document) {
  1269. return;
  1270. }
  1271. try {
  1272. var doc = frame.contentWindow.document;
  1273. var target = doc.querySelector('.article-header')
  1274. || doc.querySelector('article[class*="article-panel"]')
  1275. || doc.querySelector('.article-body')
  1276. || doc.querySelector('main');
  1277. if (!target) {
  1278. return;
  1279. }
  1280. var top = target.getBoundingClientRect().top + frame.contentWindow.pageYOffset - 24;
  1281. frame.contentWindow.scrollTo(0, top > 0 ? top : 0);
  1282. } catch (e) {
  1283. console.log(e);
  1284. }
  1285. },
  1286. /**
  1287. * 初始化编辑器
  1288. */
  1289. initEditor: function () {
  1290. var toolbar = this.getToolbar();
  1291. var _this = this;
  1292. ClassicEditor.create(document.querySelector("#" + _this.id), {
  1293. extraPlugins: [function (editor) {
  1294. _this.installUploadAdapter(editor);
  1295. _this.installPdfViewerPlugin(editor);
  1296. }], // 添加上传适配器
  1297. toolbar: toolbar || {},
  1298. language: 'zh-cn',
  1299. placeholder: _this.placeholder || '',
  1300. mediaEmbed: {
  1301. previewsInData: true,
  1302. providers: [{
  1303. name: 'spaceVideo',
  1304. url: [
  1305. /\.(mp4|webm|ogg)(\?.*)?$/i
  1306. ],
  1307. html: match => {
  1308. const input = match['input'];
  1309. return (
  1310. `<video controls playsinline preload="metadata" src="${input}" style="max-width:100%;"></video>`
  1311. );
  1312. }
  1313. }, {
  1314. name: 'spacePdf',
  1315. url: [
  1316. /\.pdf(\?.*)?$/i
  1317. ],
  1318. html: match => {
  1319. const input = match['input'];
  1320. return (
  1321. `<iframe class="space-editor-pdf-frame" src="${input}" title="PDF文件" loading="lazy" frameborder="0"></iframe>`
  1322. );
  1323. }
  1324. }, {
  1325. name: 'myprovider',
  1326. url: [
  1327. /^lizzy.*\.com.*\/media\/(\w+)/,
  1328. /^www\.lizzy.*/,
  1329. /^.*/
  1330. ],
  1331. html: match => {
  1332. //获取媒体url
  1333. const input = match['input'];
  1334. return (
  1335. `<iframe src="${input}" allowFullScreen="true" frameborder="0"></iframe>`
  1336. );
  1337. }
  1338. }]
  1339. }
  1340. }).then(editor => {
  1341. space.vars.editorReady = true;
  1342. _this.editor = editor;
  1343. _this.alignFullScreenToolbarButton();
  1344. _this.mountVideoUploadToolbarButton();
  1345. _this.mountPdfUploadToolbarButton();
  1346. _this.bindVideoPasteUpload();
  1347. _this.bindEditorVideoPlayback();
  1348. if (_this.htmlContent != undefined) {
  1349. _this.editor.setData(_this.htmlContent);
  1350. _this.upgradePdfLinksToViewers();
  1351. _this.deferSyncEditorPlayableVideos();
  1352. }
  1353. window.setTimeout(function () {
  1354. _this.upgradePdfLinksToViewers();
  1355. }, 300);
  1356. /**
  1357. * 禁止在编辑器内回车事件
  1358. */
  1359. editor.ui.view.element.onkeydown = function (event) {
  1360. // 回车键
  1361. if (event.keyCode == 13) {
  1362. event.stopPropagation();
  1363. return false;
  1364. }
  1365. // tab 转空格
  1366. if (event.keyCode == 9) {
  1367. const model = editor.model;
  1368. model.change(writer => {
  1369. const ele = writer.createElement('span');
  1370. writer.insertText(" ", ele);
  1371. // 一定要 Element
  1372. model.insertContent(ele);
  1373. });
  1374. event.stopPropagation();
  1375. return false;
  1376. }
  1377. _this.$emit("input", editor.getData());
  1378. };
  1379. // 设置编辑器宽度
  1380. if (_this.width) {
  1381. $(editor.ui.view.element).css({
  1382. 'width': _this.width
  1383. })
  1384. }
  1385. // 监听编辑器的内容变动事件
  1386. editor.model.document.on('change:data', function () {
  1387. _this.$emit("input", editor.getData());
  1388. _this.deferSyncEditorPlayableVideos();
  1389. });
  1390. }).catch(error => {
  1391. console.log(error);
  1392. });
  1393. }
  1394. },
  1395. template: `
  1396. <div class="space-editor-wrap">
  1397. <div class="space-editor-toolstrip" v-if="showEditorTools()">
  1398. <el-button class="space-editor-tool-btn" size="small" plain icon="el-icon-magic-stick" @click="formatContent">一键排版</el-button>
  1399. <span class="space-editor-tool-divider"></span>
  1400. <el-select class="space-editor-quality" v-model="imageQuality" size="small" placeholder="图片质量压缩" title="图片质量压缩">
  1401. <el-option label="原图" value="origin"></el-option>
  1402. <el-option label="高清 85%" value="high"></el-option>
  1403. <el-option label="标准 70%" value="standard"></el-option>
  1404. <el-option label="低流量 50%" value="low"></el-option>
  1405. </el-select>
  1406. <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>
  1407. <span class="space-editor-upload-status" v-if="uploadStatus">{{uploadStatus}}</span>
  1408. </div>
  1409. <input ref="videoInput" type="file" accept="video/*" style="display:none;" @change="handleVideoSelect">
  1410. <input ref="pdfInput" type="file" accept="application/pdf,.pdf" style="display:none;" @change="handlePdfSelect">
  1411. <div :id="id">
  1412. <script type="text/plain"></script>
  1413. </div>
  1414. <el-dialog title="正文预览" :visible.sync="previewVisible" width="calc(100vw - 32px)" top="4vh" append-to-body custom-class="space-editor-preview-dialog">
  1415. <div class="space-editor-preview-toolbar">
  1416. <el-button-group class="space-editor-preview-modes">
  1417. <el-button size="small" :type="previewMode == 'pc' ? 'primary' : 'default'" @click="setPreviewMode('pc')">PC端</el-button>
  1418. <el-button size="small" :type="previewMode == 'pad' ? 'primary' : 'default'" @click="setPreviewMode('pad')">平板端</el-button>
  1419. <el-button size="small" :type="previewMode == 'mobile' ? 'primary' : 'default'" @click="setPreviewMode('mobile')">手机端</el-button>
  1420. </el-button-group>
  1421. <el-button size="small" icon="el-icon-refresh" @click="openArticlePreview" :loading="previewLoading">刷新预览</el-button>
  1422. </div>
  1423. <div class="space-editor-preview-stage">
  1424. <iframe v-if="previewUrl" :key="previewMode + previewUrl" :src="previewUrl" :style="previewFrameStyle" frameborder="0" @load="handlePreviewFrameLoad"></iframe>
  1425. </div>
  1426. </el-dialog>
  1427. </div>
  1428. `
  1429. })
  1430. /**
  1431. * CMS
  1432. */
  1433. Vue.component('spaceCms', {
  1434. data: function () {
  1435. return {
  1436. fileCount: 0, // 文件总数
  1437. }
  1438. },
  1439. name: "spaceCms",
  1440. model: {
  1441. prop: 'cmsid',
  1442. evnet: 'click'
  1443. },
  1444. props: {
  1445. // v-model
  1446. cmsid: {
  1447. type: [String],
  1448. required: true
  1449. },
  1450. // 文件类型
  1451. accept: {
  1452. type: String
  1453. },
  1454. // 最大上传数量,默认199
  1455. maxsize: {
  1456. type: [String, Number],
  1457. default: 199
  1458. },
  1459. },
  1460. created: function () {
  1461. this.getCmsFileCount(this.cmsid);
  1462. },
  1463. watch: {
  1464. cmsid: function (newVal, oldVal) {
  1465. this.getCmsFileCount(newVal);
  1466. }
  1467. },
  1468. methods: {
  1469. open: function () {
  1470. var that = this;
  1471. space.plugins.cms.openCMS({
  1472. cmsid: that.cmsid,
  1473. accept: that.accept,
  1474. maxsize: that.maxsize
  1475. }, that.callback);
  1476. },
  1477. callback: function (data, callback2) {
  1478. this.$emit("input", data.cmsid);
  1479. this.cmsid = data.cmsid;
  1480. this.fileCount = data.fileCount;
  1481. callback2();
  1482. },
  1483. getCmsFileCount: function (cmsid) {
  1484. if (space.utils.isNull(cmsid)) {
  1485. return;
  1486. }
  1487. var that = this;
  1488. space.utils.ajax({
  1489. url: '/apps?ark=sys.cms_getCmsInfo',
  1490. data: {
  1491. "idcard": cmsid
  1492. },
  1493. dataType: 'json',
  1494. loadLayer: false,
  1495. autoTip: false,
  1496. async: false, // 同步请求
  1497. callback: function (data) {
  1498. if (data.code == "success") {
  1499. that.fileCount = data.data.filecount;
  1500. }
  1501. }
  1502. });
  1503. }
  1504. },
  1505. template: `<div class="cms-upload">
  1506. <el-badge :value="fileCount" type="primary" v-show="fileCount > 0">
  1507. <el-button icon="el-icon-upload2" @click="open" >上传文件</el-button>
  1508. </el-badge>
  1509. <el-button icon="el-icon-upload2" v-show="fileCount == 0" @click="open" >上传文件</el-button>
  1510. </div>`
  1511. })
  1512. /**
  1513. * CMS预览
  1514. */
  1515. Vue.component('spaceCmsView', {
  1516. data: function () {
  1517. return {
  1518. thumbnail: "",
  1519. filename: "",
  1520. filecount: 0,
  1521. isShowEl: true, //是否显示整个组件元素
  1522. }
  1523. },
  1524. name: "spaceCmsView",
  1525. /**
  1526. * cmsid: 绑定的值,非空
  1527. * thumbnail: 缩略图
  1528. * filename: 文件名
  1529. * filecount: 文件数
  1530. */
  1531. props: {
  1532. cmsid: {
  1533. type: [String, Number],
  1534. required: true
  1535. }
  1536. },
  1537. created: function () {
  1538. var that = this;
  1539. if (space.utils.isNull(that.cmsid)) {
  1540. // 不显示整个组件元素
  1541. that.isShowEl = false;
  1542. return;
  1543. }
  1544. space.utils.ajax({
  1545. url: '/apps?ark=sys.cms_getCmsInfo',
  1546. data: {
  1547. "idcard": that.cmsid
  1548. },
  1549. dataType: 'json',
  1550. loadLayer: false,
  1551. autoTip: false,
  1552. async: false, // 同步请求
  1553. callback: function (data) {
  1554. if (data.code == "success") {
  1555. that.thumbnail = data.data.thumbnail;
  1556. that.filename = data.data.filename;
  1557. that.filecount = data.data.filecount;
  1558. } else if (data.code == "warning") {
  1559. // 不显示整个组件元素
  1560. that.isShowEl = false;
  1561. }
  1562. }
  1563. });
  1564. },
  1565. methods: {
  1566. open: function () {
  1567. var that = this;
  1568. space.plugins.cms.openCMSView({
  1569. cmsid: that.cmsid
  1570. });
  1571. },
  1572. },
  1573. template: `
  1574. <div class="space-cms-view" v-if="isShowEl">
  1575. <div class="space-cms-view-wrap" @click="open">
  1576. <div class="space-cms-view-main">
  1577. <el-image :src="thumbnail">
  1578. <div slot="error" class="image-slot">
  1579. <i class="el-icon-picture-outline"></i>
  1580. </div>
  1581. </el-image>
  1582. <span>{{filename}}</span>
  1583. </div>
  1584. <div v-show="filecount >=2 " class="more-file"></div>
  1585. <div v-show="filecount >=2 " class="more-file more-file-bottom"></div>
  1586. </div>
  1587. </div>
  1588. `
  1589. })
  1590. /**
  1591. * 业务流转
  1592. */
  1593. Vue.component('spacePluginFlow', {
  1594. data: function () {
  1595. return {
  1596. isOpenFlowDrawer: false, // 是否打开抽屉
  1597. isLoadShow: true, // 是否显示加载动画
  1598. frameID: space.utils.randomStringAndNumber(6) + 'Frm', // frame 窗口ID
  1599. closeBtnID: space.utils.randomStringAndNumber(6) + 'Close', // 关闭按钮的ID
  1600. url: '/app/flow/time/', // url
  1601. title: '业务流转', // 标题
  1602. closeTitle: '收起', // 关闭按钮文本内容
  1603. }
  1604. },
  1605. name: "spacePluginFlow",
  1606. /**
  1607. * cmsid: 绑定的值,非空
  1608. * size: 抽屉大小,可传px、%,默认30%
  1609. */
  1610. props: {
  1611. audit: {
  1612. type: String,
  1613. required: true
  1614. },
  1615. apply: {
  1616. type: String,
  1617. required: true
  1618. },
  1619. open: {
  1620. type: Boolean,
  1621. },
  1622. size: {
  1623. type: String
  1624. },
  1625. },
  1626. created: function () {
  1627. this.url = this.url + "?auditid=" + this.audit + "&applyid=" + this.apply;
  1628. this.size = space.utils.isNull(this.size) ? '360px' : this.size;
  1629. this.isOpenFlowDrawer = space.utils.isNull(this.open) ? false : this.open;
  1630. this.showLoad();
  1631. },
  1632. mounted: function () {
  1633. var that = this;
  1634. // 监听窗口变化,计算关闭按钮的位置
  1635. window.onresize = function () {
  1636. // 抽屉打开状态
  1637. if (that.isOpenFlowDrawer) {
  1638. that.initCloseBtnLocation();
  1639. }
  1640. }
  1641. },
  1642. methods: {
  1643. /**
  1644. * 计算关闭按钮的坐标
  1645. */
  1646. initCloseBtnLocation: function () {
  1647. var offset = space.utils.getDomPosition(document.getElementById(this.frameID));
  1648. $("#" + this.closeBtnID).css("left", (offset.left - $("#" + this.closeBtnID).width()) + "px");
  1649. },
  1650. /**
  1651. * 显示抽屉
  1652. */
  1653. showDrawer: function () {
  1654. var that = this;
  1655. that.initCloseBtnLocation();
  1656. $("#" + that.closeBtnID).fadeIn(200);
  1657. // 为了更好的动画体验
  1658. setTimeout(() => {
  1659. $("#" + that.closeBtnID).css("z-index", "999999999999999");
  1660. }, 200);
  1661. },
  1662. /**
  1663. * 控制加载动画
  1664. */
  1665. showLoad: function () {
  1666. var that = this;
  1667. var loadInterval = setInterval(() => {
  1668. var frameEle = document.getElementById(that.frameID);
  1669. console.log("loadInterval");
  1670. frameEle.onload = function () {
  1671. window.clearInterval(loadInterval);
  1672. that.isLoadShow = false;
  1673. }
  1674. }, 100);
  1675. }
  1676. },
  1677. watch: {
  1678. isOpenFlowDrawer: function (newVal, oldVal) {
  1679. var that = this;
  1680. // 打开
  1681. if (newVal) {
  1682. var getFrameInterval = setInterval(() => {
  1683. var frameEle = document.getElementById(that.frameID);
  1684. if (!frameEle) {
  1685. return;
  1686. }
  1687. window.clearInterval(getFrameInterval);
  1688. that.showDrawer();
  1689. }, 200);
  1690. }
  1691. // 关闭
  1692. else {
  1693. $("#" + that.closeBtnID).hide();
  1694. $("#" + that.closeBtnID).css("z-index", "");
  1695. }
  1696. },
  1697. open: function (newVal, oldVal) {
  1698. this.isOpenFlowDrawer = space.utils.isNull(newVal) ? false : newVal;
  1699. }
  1700. },
  1701. template: `
  1702. <div class="space-app-plugin">
  1703. <div class="space-app-plugin-open disable-select animate__animated animate__slideInRight" v-show="!isOpenFlowDrawer" @click="isOpenFlowDrawer = true">{{title}}</div>
  1704. <div :id="closeBtnID" class="space-app-plugin-close disable-select animate__animated animate__slideInRight" @click="isOpenFlowDrawer = false">{{closeTitle}}</div>
  1705. <el-drawer :modal="false" :visible.sync="isOpenFlowDrawer" :size="size" :with-header="false" :wrapper-closable="false" >
  1706. <iframe :id="frameID" :src="url"></iframe>
  1707. <div class="space-app-plugin-load" v-show="isLoadShow" ><i class="el-icon-loading" ></i>加载中</div>
  1708. </el-drawer>
  1709. </div>
  1710. `
  1711. })