space.vue.subassembly.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  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 = "left";
  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.width = "100%";
  836. images[imgIndex].style.maxWidth = "100%";
  837. images[imgIndex].style.height = "auto";
  838. images[imgIndex].style.display = "block";
  839. images[imgIndex].style.margin = "18px 0";
  840. }
  841. var figures = wrapper.querySelectorAll("figure.image, figure");
  842. for (var figureIndex = 0; figureIndex < figures.length; figureIndex++) {
  843. var figure = figures[figureIndex];
  844. if (!figure.querySelector("img")) {
  845. continue;
  846. }
  847. figure.removeAttribute("width");
  848. figure.removeAttribute("height");
  849. figure.style.display = "block";
  850. figure.style.width = "100%";
  851. figure.style.maxWidth = "100%";
  852. figure.style.margin = "18px 0";
  853. figure.style.textAlign = "left";
  854. }
  855. var videos = wrapper.querySelectorAll("video, iframe, object, embed");
  856. for (var v = 0; v < videos.length; v++) {
  857. videos[v].style.width = "100%";
  858. videos[v].style.maxWidth = "100%";
  859. if (videos[v].tagName.toLowerCase() == "video") {
  860. videos[v].setAttribute("controls", "controls");
  861. videos[v].setAttribute("playsinline", "playsinline");
  862. videos[v].style.display = "block";
  863. videos[v].style.margin = "18px 0";
  864. }
  865. }
  866. var tables = wrapper.querySelectorAll("table");
  867. for (var t = 0; t < tables.length; t++) {
  868. tables[t].style.width = "100%";
  869. tables[t].style.borderCollapse = "collapse";
  870. }
  871. this.editor.setData(wrapper.innerHTML);
  872. this.$emit("input", this.editor.getData());
  873. this.deferSyncEditorPlayableVideos();
  874. space.message.success("正文已完成排版整理");
  875. },
  876. insertHtml: function (html) {
  877. if (!this.editor || space.utils.isNull(html)) {
  878. return;
  879. }
  880. var viewFragment = this.editor.data.processor.toView(html);
  881. var modelFragment = this.editor.data.toModel(viewFragment);
  882. this.editor.model.insertContent(modelFragment);
  883. this.$emit("input", this.editor.getData());
  884. this.deferSyncEditorPlayableVideos();
  885. },
  886. escapeHtmlAttr: function (value) {
  887. return String(value || "").replace(/[&<>"']/g, function (text) {
  888. return {
  889. "&": "&amp;",
  890. "<": "&lt;",
  891. ">": "&gt;",
  892. "\"": "&quot;",
  893. "'": "&#39;"
  894. }[text];
  895. });
  896. },
  897. normalizeLocalUploadUrl: function (url) {
  898. var text = String(url || "").trim().replace(/\\/g, "/");
  899. var match = text.match(/^(?:https?:)?\/\/[^/]*(\/web\/upload\/.*)$/i);
  900. if (match) {
  901. text = match[1];
  902. }
  903. text = text.replace(/^(https?:)\/+(\/web\/upload\/.*)$/i, "$2");
  904. text = text.replace(/(\/web\/upload\/modules)\/+/i, "$1/");
  905. return text;
  906. },
  907. toAbsoluteEditorMediaUrl: function (url) {
  908. var text = String(url || "").trim();
  909. if (/^(https?:)?\/\//i.test(text) || /^(data|blob):/i.test(text)) {
  910. return text;
  911. }
  912. if (text.charAt(0) == "/") {
  913. return window.location.origin + text;
  914. }
  915. return text;
  916. },
  917. openVideoPicker: function () {
  918. if (this.$refs.videoInput) {
  919. this.$refs.videoInput.click();
  920. }
  921. },
  922. openPdfPicker: function () {
  923. if (this.$refs.pdfInput) {
  924. this.$refs.pdfInput.click();
  925. }
  926. },
  927. handleVideoSelect: function (event) {
  928. var files = Array.prototype.slice.call(event.target.files || []);
  929. event.target.value = "";
  930. if (files.length > 0) {
  931. this.uploadVideoFile(files[0]);
  932. }
  933. },
  934. handlePdfSelect: function (event) {
  935. var files = Array.prototype.slice.call(event.target.files || []);
  936. event.target.value = "";
  937. if (files.length > 0) {
  938. this.uploadPdfFile(files[0]);
  939. }
  940. },
  941. isPdfFile: function (file) {
  942. return !!file && ((file.type && file.type.toLowerCase() == "application/pdf") || /\.pdf$/i.test(file.name || ""));
  943. },
  944. uploadVideoFile: function (file) {
  945. if (!file || !file.type || file.type.indexOf("video/") !== 0) {
  946. space.message.warning("请选择视频文件");
  947. return;
  948. }
  949. var _this = this;
  950. _this.uploading = true;
  951. _this.uploadStatus = "正在上传视频";
  952. _this.uploadPlainFileByAdapter(file, {
  953. imageQuality: 'origin'
  954. }, function (loaded, total) {
  955. if (total > 0) {
  956. _this.uploadStatus = "正在上传视频 " + Math.round(loaded * 100 / total) + "%";
  957. }
  958. }).then(function (res) {
  959. var videoUrl = _this.normalizeLocalUploadUrl(res.default);
  960. _this.insertHtml('<p><video controls playsinline preload="metadata" src="' + _this.escapeHtmlAttr(videoUrl) + '" style="max-width:100%;"></video></p>');
  961. _this.$emit("input", _this.editor.getData());
  962. space.message.success("视频已插入正文");
  963. }).catch(function (msg) {
  964. space.message.error(msg || "视频上传失败");
  965. }).finally(function () {
  966. _this.uploading = false;
  967. _this.uploadStatus = "";
  968. });
  969. },
  970. uploadPdfFile: function (file) {
  971. if (!this.isPdfFile(file)) {
  972. space.message.warning("请选择PDF文件");
  973. return;
  974. }
  975. var _this = this;
  976. _this.uploading = true;
  977. _this.uploadStatus = "正在上传PDF";
  978. _this.uploadPlainFileByAdapter(file, {
  979. imageQuality: 'origin',
  980. pdfMaxSize: 100 * 1024 * 1024
  981. }, function (loaded, total) {
  982. if (total > 0) {
  983. _this.uploadStatus = "正在上传PDF " + Math.round(loaded * 100 / total) + "%";
  984. }
  985. }).then(function (res) {
  986. var pdfUrl = _this.normalizeLocalUploadUrl(res.default);
  987. _this.insertPdfViewer(pdfUrl, file.name);
  988. _this.$emit("input", _this.editor.getData());
  989. space.message.success("PDF已插入正文");
  990. }).catch(function (msg) {
  991. space.message.error(msg || "PDF上传失败");
  992. }).finally(function () {
  993. _this.uploading = false;
  994. _this.uploadStatus = "";
  995. });
  996. },
  997. buildPdfViewerHtml: function (url, fileName) {
  998. var safeUrl = this.escapeHtmlAttr(url);
  999. var safeName = this.escapeHtmlAttr(fileName || "PDF文件");
  1000. return '<figure class="space-editor-pdf-viewer media" data-space-pdf="true">'
  1001. + '<iframe class="space-editor-pdf-frame" src="' + safeUrl + '" title="' + safeName + '" loading="lazy"></iframe>'
  1002. + '<figcaption><a href="' + safeUrl + '" target="_blank" rel="noopener">打开 PDF:' + safeName + '</a></figcaption>'
  1003. + '</figure><p></p>';
  1004. },
  1005. insertPdfViewer: function (url, fileName) {
  1006. if (!this.editor || space.utils.isNull(url)) {
  1007. return;
  1008. }
  1009. var title = fileName || "PDF文件";
  1010. try {
  1011. this.editor.model.change(function (writer) {
  1012. var pdfViewer = writer.createElement("spacePdfViewer", {
  1013. src: url,
  1014. title: title
  1015. });
  1016. this.editor.model.insertContent(pdfViewer);
  1017. }.bind(this));
  1018. this.$emit("input", this.editor.getData());
  1019. this.deferSyncEditorPlayableVideos();
  1020. return;
  1021. } catch (e) {
  1022. console.log(e);
  1023. }
  1024. if (this.editor.commands && this.editor.commands.get("mediaEmbed")) {
  1025. this.editor.execute("mediaEmbed", this.toAbsoluteEditorMediaUrl(url));
  1026. this.deferSyncEditorPlayableVideos();
  1027. return;
  1028. }
  1029. this.insertHtml(this.buildPdfViewerHtml(url, title));
  1030. },
  1031. buildPdfLinkHtml: function (url, fileName) {
  1032. var safeUrl = this.escapeHtmlAttr(url);
  1033. var safeName = this.escapeHtmlAttr(fileName || "PDF文件");
  1034. return '<p class="space-editor-pdf-link"><a href="' + safeUrl
  1035. + '" target="_blank" rel="noopener">打开 PDF:' + safeName + '</a></p>';
  1036. },
  1037. upgradePdfLinksToViewers: function () {
  1038. if (!this.editor) {
  1039. return;
  1040. }
  1041. var data = this.editor.getData();
  1042. if (data.indexOf(".pdf") == -1) {
  1043. return;
  1044. }
  1045. var wrapper = document.createElement("div");
  1046. wrapper.innerHTML = data;
  1047. var links = wrapper.querySelectorAll(".space-editor-pdf-link a[href], a[href]");
  1048. var changed = false;
  1049. for (var i = 0; i < links.length; i++) {
  1050. var link = links[i];
  1051. var href = link.getAttribute("href") || "";
  1052. var text = (link.textContent || "").replace(/\s+/g, " ").trim();
  1053. var isPdf = /\.pdf(\?.*)?$/i.test(href);
  1054. var isEditorPdfLink = link.closest(".space-editor-pdf-link") || /^打开\s*PDF[::]/i.test(text);
  1055. if (!isPdf || !isEditorPdfLink) {
  1056. continue;
  1057. }
  1058. var title = text.replace(/^打开\s*PDF[::]\s*/i, "") || href.split("/").pop() || "PDF文件";
  1059. var holder = document.createElement("div");
  1060. holder.innerHTML = this.buildPdfViewerHtml(href, title);
  1061. var viewer = holder.firstElementChild;
  1062. var replaceTarget = link.closest(".space-editor-pdf-link") || link.parentNode;
  1063. if (viewer && replaceTarget && replaceTarget.parentNode) {
  1064. replaceTarget.parentNode.replaceChild(viewer, replaceTarget);
  1065. changed = true;
  1066. }
  1067. }
  1068. if (changed) {
  1069. this.editor.setData(wrapper.innerHTML);
  1070. this.$emit("input", this.editor.getData());
  1071. this.deferSyncEditorPlayableVideos();
  1072. }
  1073. },
  1074. bindVideoPasteUpload: function () {
  1075. if (!this.editor || !this.editor.ui || !this.editor.ui.getEditableElement) {
  1076. return;
  1077. }
  1078. var editable = this.editor.ui.getEditableElement();
  1079. if (!editable || editable.getAttribute("data-space-video-upload") == "true") {
  1080. return;
  1081. }
  1082. editable.setAttribute("data-space-video-upload", "true");
  1083. var _this = this;
  1084. var handler = function (event) {
  1085. var dataTransfer = event.clipboardData || event.dataTransfer;
  1086. if (!dataTransfer || !dataTransfer.files || dataTransfer.files.length == 0) {
  1087. return;
  1088. }
  1089. var files = Array.prototype.slice.call(dataTransfer.files);
  1090. var videoFile = files.find(function (file) {
  1091. return file && file.type && file.type.indexOf("video/") === 0;
  1092. });
  1093. var pdfFile = files.find(function (file) {
  1094. return _this.isPdfFile(file);
  1095. });
  1096. if (videoFile) {
  1097. event.preventDefault();
  1098. _this.uploadVideoFile(videoFile);
  1099. } else if (pdfFile) {
  1100. event.preventDefault();
  1101. _this.uploadPdfFile(pdfFile);
  1102. }
  1103. };
  1104. editable.addEventListener("paste", handler);
  1105. editable.addEventListener("drop", handler);
  1106. },
  1107. findClosestEditorMedia: function (target) {
  1108. if (!target) {
  1109. return null;
  1110. }
  1111. if (target.nodeType == 3) {
  1112. target = target.parentNode;
  1113. }
  1114. if (!target || target.nodeType != 1) {
  1115. return null;
  1116. }
  1117. if (/^(video|iframe)$/i.test(target.tagName)) {
  1118. return target;
  1119. }
  1120. if (target.closest) {
  1121. return target.closest("video, iframe");
  1122. }
  1123. return null;
  1124. },
  1125. prepareEditorPlayableMedia: function (media) {
  1126. if (!media || media.nodeType != 1) {
  1127. return;
  1128. }
  1129. var tagName = (media.tagName || "").toLowerCase();
  1130. if (tagName == "video") {
  1131. media.setAttribute("controls", "controls");
  1132. media.setAttribute("playsinline", "playsinline");
  1133. media.setAttribute("webkit-playsinline", "webkit-playsinline");
  1134. if (!media.getAttribute("preload")) {
  1135. media.setAttribute("preload", "metadata");
  1136. }
  1137. }
  1138. media.classList.add("space-editor-playable-media");
  1139. var mediaWidget = media.closest ? media.closest("figure.media") : null;
  1140. if (mediaWidget) {
  1141. mediaWidget.classList.add("space-editor-playable-media-widget");
  1142. }
  1143. },
  1144. bindPlayableMediaNativeEvents: function (media) {
  1145. if (!media || media.getAttribute("data-space-playable-media-bound") == "true") {
  1146. return;
  1147. }
  1148. this.prepareEditorPlayableMedia(media);
  1149. media.setAttribute("data-space-playable-media-bound", "true");
  1150. var _this = this;
  1151. var stopWidgetSelection = function (event) {
  1152. _this.prepareEditorPlayableMedia(media);
  1153. event.stopPropagation();
  1154. };
  1155. var eventNames = ["pointerdown", "pointerup", "mousedown", "mouseup", "click", "dblclick", "touchstart", "touchend"];
  1156. for (var i = 0; i < eventNames.length; i++) {
  1157. media.addEventListener(eventNames[i], stopWidgetSelection);
  1158. }
  1159. },
  1160. syncEditorPlayableVideos: function () {
  1161. if (!this.editor || !this.editor.ui || !this.editor.ui.getEditableElement) {
  1162. return;
  1163. }
  1164. var editable = this.editor.ui.getEditableElement();
  1165. if (!editable) {
  1166. return;
  1167. }
  1168. var medias = editable.querySelectorAll("video, iframe");
  1169. for (var i = 0; i < medias.length; i++) {
  1170. this.bindPlayableMediaNativeEvents(medias[i]);
  1171. }
  1172. },
  1173. deferSyncEditorPlayableVideos: function () {
  1174. if (this.videoPlaybackSyncTimer) {
  1175. window.clearTimeout(this.videoPlaybackSyncTimer);
  1176. }
  1177. var _this = this;
  1178. this.videoPlaybackSyncTimer = window.setTimeout(function () {
  1179. _this.syncEditorPlayableVideos();
  1180. _this.videoPlaybackSyncTimer = undefined;
  1181. }, 0);
  1182. },
  1183. bindEditorVideoPlayback: function () {
  1184. if (!this.editor || !this.editor.ui || !this.editor.ui.getEditableElement) {
  1185. return;
  1186. }
  1187. var editable = this.editor.ui.getEditableElement();
  1188. if (!editable || editable.getAttribute("data-space-playable-media-root") == "true") {
  1189. this.syncEditorPlayableVideos();
  1190. return;
  1191. }
  1192. editable.setAttribute("data-space-playable-media-root", "true");
  1193. var _this = this;
  1194. var stopNativeWidgetSelection = function (event) {
  1195. var media = _this.findClosestEditorMedia(event.target);
  1196. if (!media) {
  1197. return;
  1198. }
  1199. _this.prepareEditorPlayableMedia(media);
  1200. event.stopPropagation();
  1201. };
  1202. ["pointerdown", "pointerup", "mousedown", "mouseup", "click", "dblclick", "touchstart", "touchend"].forEach(function (eventName) {
  1203. editable.addEventListener(eventName, stopNativeWidgetSelection);
  1204. });
  1205. if (this.editor.editing && this.editor.editing.view && this.editor.editing.view.document) {
  1206. ["mousedown", "mouseup", "click", "dblclick", "touchstart", "touchend"].forEach(function (eventName) {
  1207. _this.editor.editing.view.document.on(eventName, function (evt, data) {
  1208. var domTarget = data ? (data.domTarget || (data.domEvent ? data.domEvent.target : null)) : null;
  1209. var media = _this.findClosestEditorMedia(domTarget);
  1210. if (!media) {
  1211. return;
  1212. }
  1213. _this.prepareEditorPlayableMedia(media);
  1214. evt.stop();
  1215. }, { priority: "highest" });
  1216. });
  1217. }
  1218. this.syncEditorPlayableVideos();
  1219. },
  1220. getPreviewPostData: function () {
  1221. var data = this.previewdata || {};
  1222. return {
  1223. title: data.title || '',
  1224. source: data.source || '',
  1225. navigationid: data.navigationid || '',
  1226. desc: data.desc || '',
  1227. body: this.editor ? this.editor.getData() : (this.htmlContent || '')
  1228. };
  1229. },
  1230. openArticlePreview: function () {
  1231. if (!this.editor) {
  1232. return;
  1233. }
  1234. var postData = this.getPreviewPostData();
  1235. if (space.utils.isNull(postData.navigationid)) {
  1236. space.message.warning("请先选择发布栏目");
  1237. return;
  1238. }
  1239. if (space.utils.isNull(postData.body)) {
  1240. space.message.warning("正文内容为空");
  1241. return;
  1242. }
  1243. var _this = this;
  1244. _this.previewLoading = true;
  1245. $.ajax({
  1246. url: _this.previewurl || "/apps?ark=website.article_preview",
  1247. type: "POST",
  1248. data: postData,
  1249. dataType: "json",
  1250. success: function (res) {
  1251. if (res.code != "success") {
  1252. space.message.error(res.msg || "生成预览失败");
  1253. return;
  1254. }
  1255. var data = res.data || {};
  1256. var baseUrl = data.url || data.previewUrl || "";
  1257. if (space.utils.isNull(baseUrl)) {
  1258. space.message.error("生成预览失败");
  1259. return;
  1260. }
  1261. _this.previewUrl = baseUrl + (baseUrl.indexOf("?") == -1 ? "?" : "&") + "_t=" + new Date().getTime();
  1262. _this.previewVisible = true;
  1263. },
  1264. error: function () {
  1265. space.message.error("生成预览失败");
  1266. },
  1267. complete: function () {
  1268. _this.previewLoading = false;
  1269. }
  1270. });
  1271. },
  1272. setPreviewMode: function (mode) {
  1273. this.previewMode = mode || 'pc';
  1274. },
  1275. handlePreviewFrameLoad: function (event) {
  1276. var frame = event ? event.target : null;
  1277. var _this = this;
  1278. this.scrollPreviewFrameToArticle(frame);
  1279. window.setTimeout(function () {
  1280. _this.scrollPreviewFrameToArticle(frame);
  1281. }, 220);
  1282. },
  1283. scrollPreviewFrameToArticle: function (frame) {
  1284. if (!frame || !frame.contentWindow || !frame.contentWindow.document) {
  1285. return;
  1286. }
  1287. try {
  1288. var doc = frame.contentWindow.document;
  1289. var target = doc.querySelector('.article-header')
  1290. || doc.querySelector('article[class*="article-panel"]')
  1291. || doc.querySelector('.article-body')
  1292. || doc.querySelector('main');
  1293. if (!target) {
  1294. return;
  1295. }
  1296. var top = target.getBoundingClientRect().top + frame.contentWindow.pageYOffset - 24;
  1297. frame.contentWindow.scrollTo(0, top > 0 ? top : 0);
  1298. } catch (e) {
  1299. console.log(e);
  1300. }
  1301. },
  1302. /**
  1303. * 初始化编辑器
  1304. */
  1305. initEditor: function () {
  1306. var toolbar = this.getToolbar();
  1307. var _this = this;
  1308. ClassicEditor.create(document.querySelector("#" + _this.id), {
  1309. extraPlugins: [function (editor) {
  1310. _this.installUploadAdapter(editor);
  1311. _this.installPdfViewerPlugin(editor);
  1312. }], // 添加上传适配器
  1313. toolbar: toolbar || {},
  1314. language: 'zh-cn',
  1315. placeholder: _this.placeholder || '',
  1316. mediaEmbed: {
  1317. previewsInData: true,
  1318. providers: [{
  1319. name: 'spaceVideo',
  1320. url: [
  1321. /\.(mp4|webm|ogg)(\?.*)?$/i
  1322. ],
  1323. html: match => {
  1324. const input = match['input'];
  1325. return (
  1326. `<video controls playsinline preload="metadata" src="${input}" style="max-width:100%;"></video>`
  1327. );
  1328. }
  1329. }, {
  1330. name: 'spacePdf',
  1331. url: [
  1332. /\.pdf(\?.*)?$/i
  1333. ],
  1334. html: match => {
  1335. const input = match['input'];
  1336. return (
  1337. `<iframe class="space-editor-pdf-frame" src="${input}" title="PDF文件" loading="lazy" frameborder="0"></iframe>`
  1338. );
  1339. }
  1340. }, {
  1341. name: 'myprovider',
  1342. url: [
  1343. /^lizzy.*\.com.*\/media\/(\w+)/,
  1344. /^www\.lizzy.*/,
  1345. /^.*/
  1346. ],
  1347. html: match => {
  1348. //获取媒体url
  1349. const input = match['input'];
  1350. return (
  1351. `<iframe src="${input}" allowFullScreen="true" frameborder="0"></iframe>`
  1352. );
  1353. }
  1354. }]
  1355. }
  1356. }).then(editor => {
  1357. space.vars.editorReady = true;
  1358. _this.editor = editor;
  1359. _this.alignFullScreenToolbarButton();
  1360. _this.mountVideoUploadToolbarButton();
  1361. _this.mountPdfUploadToolbarButton();
  1362. _this.bindVideoPasteUpload();
  1363. _this.bindEditorVideoPlayback();
  1364. if (_this.htmlContent != undefined) {
  1365. _this.editor.setData(_this.htmlContent);
  1366. _this.upgradePdfLinksToViewers();
  1367. _this.deferSyncEditorPlayableVideos();
  1368. }
  1369. window.setTimeout(function () {
  1370. _this.upgradePdfLinksToViewers();
  1371. }, 300);
  1372. /**
  1373. * 禁止在编辑器内回车事件
  1374. */
  1375. editor.ui.view.element.onkeydown = function (event) {
  1376. // 回车键
  1377. if (event.keyCode == 13) {
  1378. event.stopPropagation();
  1379. return false;
  1380. }
  1381. // tab 转空格
  1382. if (event.keyCode == 9) {
  1383. const model = editor.model;
  1384. model.change(writer => {
  1385. const ele = writer.createElement('span');
  1386. writer.insertText(" ", ele);
  1387. // 一定要 Element
  1388. model.insertContent(ele);
  1389. });
  1390. event.stopPropagation();
  1391. return false;
  1392. }
  1393. _this.$emit("input", editor.getData());
  1394. };
  1395. // 设置编辑器宽度
  1396. if (_this.width) {
  1397. $(editor.ui.view.element).css({
  1398. 'width': _this.width
  1399. })
  1400. }
  1401. // 监听编辑器的内容变动事件
  1402. editor.model.document.on('change:data', function () {
  1403. _this.$emit("input", editor.getData());
  1404. _this.deferSyncEditorPlayableVideos();
  1405. });
  1406. }).catch(error => {
  1407. console.log(error);
  1408. });
  1409. }
  1410. },
  1411. template: `
  1412. <div class="space-editor-wrap">
  1413. <div class="space-editor-toolstrip" v-if="showEditorTools()">
  1414. <el-button class="space-editor-tool-btn" size="small" plain icon="el-icon-magic-stick" @click="formatContent">一键排版</el-button>
  1415. <span class="space-editor-tool-divider"></span>
  1416. <el-select class="space-editor-quality" v-model="imageQuality" size="small" placeholder="图片质量压缩" title="图片质量压缩">
  1417. <el-option label="原图" value="origin"></el-option>
  1418. <el-option label="高清 85%" value="high"></el-option>
  1419. <el-option label="标准 70%" value="standard"></el-option>
  1420. <el-option label="低流量 50%" value="low"></el-option>
  1421. </el-select>
  1422. <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>
  1423. <span class="space-editor-upload-status" v-if="uploadStatus">{{uploadStatus}}</span>
  1424. </div>
  1425. <input ref="videoInput" type="file" accept="video/*" style="display:none;" @change="handleVideoSelect">
  1426. <input ref="pdfInput" type="file" accept="application/pdf,.pdf" style="display:none;" @change="handlePdfSelect">
  1427. <div :id="id">
  1428. <script type="text/plain"></script>
  1429. </div>
  1430. <el-dialog title="正文预览" :visible.sync="previewVisible" width="calc(100vw - 32px)" top="4vh" append-to-body custom-class="space-editor-preview-dialog">
  1431. <div class="space-editor-preview-toolbar">
  1432. <el-button-group class="space-editor-preview-modes">
  1433. <el-button size="small" :type="previewMode == 'pc' ? 'primary' : 'default'" @click="setPreviewMode('pc')">PC端</el-button>
  1434. <el-button size="small" :type="previewMode == 'pad' ? 'primary' : 'default'" @click="setPreviewMode('pad')">平板端</el-button>
  1435. <el-button size="small" :type="previewMode == 'mobile' ? 'primary' : 'default'" @click="setPreviewMode('mobile')">手机端</el-button>
  1436. </el-button-group>
  1437. <el-button size="small" icon="el-icon-refresh" @click="openArticlePreview" :loading="previewLoading">刷新预览</el-button>
  1438. </div>
  1439. <div class="space-editor-preview-stage">
  1440. <iframe v-if="previewUrl" :key="previewMode + previewUrl" :src="previewUrl" :style="previewFrameStyle" frameborder="0" @load="handlePreviewFrameLoad"></iframe>
  1441. </div>
  1442. </el-dialog>
  1443. </div>
  1444. `
  1445. })
  1446. /**
  1447. * CMS
  1448. */
  1449. Vue.component('spaceCms', {
  1450. data: function () {
  1451. return {
  1452. fileCount: 0, // 文件总数
  1453. }
  1454. },
  1455. name: "spaceCms",
  1456. model: {
  1457. prop: 'cmsid',
  1458. evnet: 'click'
  1459. },
  1460. props: {
  1461. // v-model
  1462. cmsid: {
  1463. type: [String],
  1464. required: true
  1465. },
  1466. // 文件类型
  1467. accept: {
  1468. type: String
  1469. },
  1470. // 最大上传数量,默认199
  1471. maxsize: {
  1472. type: [String, Number],
  1473. default: 199
  1474. },
  1475. },
  1476. created: function () {
  1477. this.getCmsFileCount(this.cmsid);
  1478. },
  1479. watch: {
  1480. cmsid: function (newVal, oldVal) {
  1481. this.getCmsFileCount(newVal);
  1482. }
  1483. },
  1484. methods: {
  1485. open: function () {
  1486. var that = this;
  1487. space.plugins.cms.openCMS({
  1488. cmsid: that.cmsid,
  1489. accept: that.accept,
  1490. maxsize: that.maxsize
  1491. }, that.callback);
  1492. },
  1493. callback: function (data, callback2) {
  1494. this.$emit("input", data.cmsid);
  1495. this.cmsid = data.cmsid;
  1496. this.fileCount = data.fileCount;
  1497. callback2();
  1498. },
  1499. getCmsFileCount: function (cmsid) {
  1500. if (space.utils.isNull(cmsid)) {
  1501. return;
  1502. }
  1503. var that = this;
  1504. space.utils.ajax({
  1505. url: '/apps?ark=sys.cms_getCmsInfo',
  1506. data: {
  1507. "idcard": cmsid
  1508. },
  1509. dataType: 'json',
  1510. loadLayer: false,
  1511. autoTip: false,
  1512. async: false, // 同步请求
  1513. callback: function (data) {
  1514. if (data.code == "success") {
  1515. that.fileCount = data.data.filecount;
  1516. }
  1517. }
  1518. });
  1519. }
  1520. },
  1521. template: `<div class="cms-upload">
  1522. <el-badge :value="fileCount" type="primary" v-show="fileCount > 0">
  1523. <el-button icon="el-icon-upload2" @click="open" >上传文件</el-button>
  1524. </el-badge>
  1525. <el-button icon="el-icon-upload2" v-show="fileCount == 0" @click="open" >上传文件</el-button>
  1526. </div>`
  1527. })
  1528. /**
  1529. * CMS预览
  1530. */
  1531. Vue.component('spaceCmsView', {
  1532. data: function () {
  1533. return {
  1534. thumbnail: "",
  1535. filename: "",
  1536. filecount: 0,
  1537. isShowEl: true, //是否显示整个组件元素
  1538. }
  1539. },
  1540. name: "spaceCmsView",
  1541. /**
  1542. * cmsid: 绑定的值,非空
  1543. * thumbnail: 缩略图
  1544. * filename: 文件名
  1545. * filecount: 文件数
  1546. */
  1547. props: {
  1548. cmsid: {
  1549. type: [String, Number],
  1550. required: true
  1551. }
  1552. },
  1553. created: function () {
  1554. var that = this;
  1555. if (space.utils.isNull(that.cmsid)) {
  1556. // 不显示整个组件元素
  1557. that.isShowEl = false;
  1558. return;
  1559. }
  1560. space.utils.ajax({
  1561. url: '/apps?ark=sys.cms_getCmsInfo',
  1562. data: {
  1563. "idcard": that.cmsid
  1564. },
  1565. dataType: 'json',
  1566. loadLayer: false,
  1567. autoTip: false,
  1568. async: false, // 同步请求
  1569. callback: function (data) {
  1570. if (data.code == "success") {
  1571. that.thumbnail = data.data.thumbnail;
  1572. that.filename = data.data.filename;
  1573. that.filecount = data.data.filecount;
  1574. } else if (data.code == "warning") {
  1575. // 不显示整个组件元素
  1576. that.isShowEl = false;
  1577. }
  1578. }
  1579. });
  1580. },
  1581. methods: {
  1582. open: function () {
  1583. var that = this;
  1584. space.plugins.cms.openCMSView({
  1585. cmsid: that.cmsid
  1586. });
  1587. },
  1588. },
  1589. template: `
  1590. <div class="space-cms-view" v-if="isShowEl">
  1591. <div class="space-cms-view-wrap" @click="open">
  1592. <div class="space-cms-view-main">
  1593. <el-image :src="thumbnail">
  1594. <div slot="error" class="image-slot">
  1595. <i class="el-icon-picture-outline"></i>
  1596. </div>
  1597. </el-image>
  1598. <span>{{filename}}</span>
  1599. </div>
  1600. <div v-show="filecount >=2 " class="more-file"></div>
  1601. <div v-show="filecount >=2 " class="more-file more-file-bottom"></div>
  1602. </div>
  1603. </div>
  1604. `
  1605. })
  1606. /**
  1607. * 业务流转
  1608. */
  1609. Vue.component('spacePluginFlow', {
  1610. data: function () {
  1611. return {
  1612. isOpenFlowDrawer: false, // 是否打开抽屉
  1613. isLoadShow: true, // 是否显示加载动画
  1614. frameID: space.utils.randomStringAndNumber(6) + 'Frm', // frame 窗口ID
  1615. closeBtnID: space.utils.randomStringAndNumber(6) + 'Close', // 关闭按钮的ID
  1616. url: '/app/flow/time/', // url
  1617. title: '业务流转', // 标题
  1618. closeTitle: '收起', // 关闭按钮文本内容
  1619. }
  1620. },
  1621. name: "spacePluginFlow",
  1622. /**
  1623. * cmsid: 绑定的值,非空
  1624. * size: 抽屉大小,可传px、%,默认30%
  1625. */
  1626. props: {
  1627. audit: {
  1628. type: String,
  1629. required: true
  1630. },
  1631. apply: {
  1632. type: String,
  1633. required: true
  1634. },
  1635. open: {
  1636. type: Boolean,
  1637. },
  1638. size: {
  1639. type: String
  1640. },
  1641. },
  1642. created: function () {
  1643. this.url = this.url + "?auditid=" + this.audit + "&applyid=" + this.apply;
  1644. this.size = space.utils.isNull(this.size) ? '360px' : this.size;
  1645. this.isOpenFlowDrawer = space.utils.isNull(this.open) ? false : this.open;
  1646. this.showLoad();
  1647. },
  1648. mounted: function () {
  1649. var that = this;
  1650. // 监听窗口变化,计算关闭按钮的位置
  1651. window.onresize = function () {
  1652. // 抽屉打开状态
  1653. if (that.isOpenFlowDrawer) {
  1654. that.initCloseBtnLocation();
  1655. }
  1656. }
  1657. },
  1658. methods: {
  1659. /**
  1660. * 计算关闭按钮的坐标
  1661. */
  1662. initCloseBtnLocation: function () {
  1663. var offset = space.utils.getDomPosition(document.getElementById(this.frameID));
  1664. $("#" + this.closeBtnID).css("left", (offset.left - $("#" + this.closeBtnID).width()) + "px");
  1665. },
  1666. /**
  1667. * 显示抽屉
  1668. */
  1669. showDrawer: function () {
  1670. var that = this;
  1671. that.initCloseBtnLocation();
  1672. $("#" + that.closeBtnID).fadeIn(200);
  1673. // 为了更好的动画体验
  1674. setTimeout(() => {
  1675. $("#" + that.closeBtnID).css("z-index", "999999999999999");
  1676. }, 200);
  1677. },
  1678. /**
  1679. * 控制加载动画
  1680. */
  1681. showLoad: function () {
  1682. var that = this;
  1683. var loadInterval = setInterval(() => {
  1684. var frameEle = document.getElementById(that.frameID);
  1685. console.log("loadInterval");
  1686. frameEle.onload = function () {
  1687. window.clearInterval(loadInterval);
  1688. that.isLoadShow = false;
  1689. }
  1690. }, 100);
  1691. }
  1692. },
  1693. watch: {
  1694. isOpenFlowDrawer: function (newVal, oldVal) {
  1695. var that = this;
  1696. // 打开
  1697. if (newVal) {
  1698. var getFrameInterval = setInterval(() => {
  1699. var frameEle = document.getElementById(that.frameID);
  1700. if (!frameEle) {
  1701. return;
  1702. }
  1703. window.clearInterval(getFrameInterval);
  1704. that.showDrawer();
  1705. }, 200);
  1706. }
  1707. // 关闭
  1708. else {
  1709. $("#" + that.closeBtnID).hide();
  1710. $("#" + that.closeBtnID).css("z-index", "");
  1711. }
  1712. },
  1713. open: function (newVal, oldVal) {
  1714. this.isOpenFlowDrawer = space.utils.isNull(newVal) ? false : newVal;
  1715. }
  1716. },
  1717. template: `
  1718. <div class="space-app-plugin">
  1719. <div class="space-app-plugin-open disable-select animate__animated animate__slideInRight" v-show="!isOpenFlowDrawer" @click="isOpenFlowDrawer = true">{{title}}</div>
  1720. <div :id="closeBtnID" class="space-app-plugin-close disable-select animate__animated animate__slideInRight" @click="isOpenFlowDrawer = false">{{closeTitle}}</div>
  1721. <el-drawer :modal="false" :visible.sync="isOpenFlowDrawer" :size="size" :with-header="false" :wrapper-closable="false" >
  1722. <iframe :id="frameID" :src="url"></iframe>
  1723. <div class="space-app-plugin-load" v-show="isLoadShow" ><i class="el-icon-loading" ></i>加载中</div>
  1724. </el-drawer>
  1725. </div>
  1726. `
  1727. })