vue-editify 0.0.16 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
package/src/Editify.vue CHANGED
@@ -119,10 +119,10 @@ export default {
119
119
  }
120
120
  //如果是外部修改,需要重新渲染编辑器
121
121
  this.editor.stack = this.editor.parseHtml(newVal)
122
+ this.editor.range = null
122
123
  this.editor.formatElementStack()
123
- this.editor.range.anchor.moveToStart(this.editor.stack[0])
124
- this.editor.range.focus.moveToStart(this.editor.stack[0])
125
124
  this.editor.domRender()
125
+ this.editor.rangeRender()
126
126
  },
127
127
  //代码视图切换
128
128
  isSourceView(newValue) {
@@ -430,6 +430,9 @@ export default {
430
430
  else {
431
431
  element.marks['data-editify-task'] = 'checked'
432
432
  }
433
+ if (!this.editor.range) {
434
+ this.editor.initRange()
435
+ }
433
436
  this.editor.range.anchor.moveToEnd(element)
434
437
  this.editor.range.focus.moveToEnd(element)
435
438
  this.editor.formatElementStack()
@@ -533,6 +536,9 @@ export default {
533
536
  const key = node.getAttribute('data-editify-element')
534
537
  if (key) {
535
538
  const element = this.editor.getElementByKey(key)
539
+ if (!this.editor.range) {
540
+ this.editor.initRange()
541
+ }
536
542
  this.editor.range.anchor.moveToStart(element)
537
543
  this.editor.range.focus.moveToEnd(element)
538
544
  this.editor.rangeRender()
@@ -687,6 +693,9 @@ export default {
687
693
  if (this.disabled) {
688
694
  return null
689
695
  }
696
+ if (!this.editor.range) {
697
+ return null
698
+ }
690
699
  if (this.editor.range.anchor.element.isEqual(this.editor.range.focus.element)) {
691
700
  return this.getParsedomElementByElement(this.editor.range.anchor.element, parsedom)
692
701
  }
@@ -723,6 +732,9 @@ export default {
723
732
  if (this.disabled) {
724
733
  return
725
734
  }
735
+ if (!this.editor.range) {
736
+ return
737
+ }
726
738
  const element = this.getCurrentParsedomElement(parsedom)
727
739
  if (element) {
728
740
  element.toEmpty()
@@ -736,6 +748,9 @@ export default {
736
748
  if (this.disabled) {
737
749
  return
738
750
  }
751
+ if (!this.editor.range) {
752
+ return
753
+ }
739
754
  const link = this.getCurrentParsedomElement('a')
740
755
  if (link) {
741
756
  link.parsedom = AlexElement.TEXT_NODE
@@ -751,6 +766,9 @@ export default {
751
766
  if (this.disabled) {
752
767
  return
753
768
  }
769
+ if (!this.editor.range) {
770
+ return
771
+ }
754
772
  const values = getButtonOptionsConfig(this.$editTrans, this.$editLocale).heading.map(item => {
755
773
  return item.value
756
774
  })
@@ -785,6 +803,9 @@ export default {
785
803
  if (this.disabled) {
786
804
  return
787
805
  }
806
+ if (!this.editor.range) {
807
+ return
808
+ }
788
809
  //起点和终点在一起
789
810
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
790
811
  const block = this.editor.range.anchor.element.getBlock()
@@ -824,6 +845,9 @@ export default {
824
845
  if (this.disabled) {
825
846
  return
826
847
  }
848
+ if (!this.editor.range) {
849
+ return
850
+ }
827
851
  //起点和终点在一起
828
852
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
829
853
  const block = this.editor.range.anchor.element.getBlock()
@@ -863,6 +887,9 @@ export default {
863
887
  if (this.disabled) {
864
888
  return
865
889
  }
890
+ if (!this.editor.range) {
891
+ return
892
+ }
866
893
  const active = this.queryTextStyle(name, value)
867
894
  if (active) {
868
895
  this.editor.removeTextStyle([name])
@@ -884,6 +911,9 @@ export default {
884
911
  if (this.disabled) {
885
912
  return
886
913
  }
914
+ if (!this.editor.range) {
915
+ return
916
+ }
887
917
  const active = this.queryTextMark(name, value)
888
918
  if (active) {
889
919
  this.editor.removeTextMark([name])
@@ -905,6 +935,9 @@ export default {
905
935
  if (this.disabled) {
906
936
  return
907
937
  }
938
+ if (!this.editor.range) {
939
+ return
940
+ }
908
941
  this.editor.removeTextStyle()
909
942
  this.editor.removeTextMark()
910
943
  this.editor.formatElementStack()
@@ -916,6 +949,9 @@ export default {
916
949
  if (this.disabled) {
917
950
  return
918
951
  }
952
+ if (!this.editor.range) {
953
+ return
954
+ }
919
955
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
920
956
  const block = this.editor.range.anchor.element.getBlock()
921
957
  const inblock = this.editor.range.anchor.element.getInblock()
@@ -1009,6 +1045,9 @@ export default {
1009
1045
  if (this.disabled) {
1010
1046
  return
1011
1047
  }
1048
+ if (!this.editor.range) {
1049
+ return
1050
+ }
1012
1051
  //起点和终点在一起
1013
1052
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
1014
1053
  const block = this.editor.range.anchor.element.getBlock()
@@ -1046,6 +1085,9 @@ export default {
1046
1085
  if (this.disabled) {
1047
1086
  return
1048
1087
  }
1088
+ if (!this.editor.range) {
1089
+ return
1090
+ }
1049
1091
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
1050
1092
  const block = this.editor.range.anchor.element.getBlock()
1051
1093
  const inblock = this.editor.range.anchor.element.getInblock()
@@ -1109,6 +1151,9 @@ export default {
1109
1151
  if (this.disabled) {
1110
1152
  return
1111
1153
  }
1154
+ if (!this.editor.range) {
1155
+ return
1156
+ }
1112
1157
  const fn = element => {
1113
1158
  if (element.hasStyles()) {
1114
1159
  if (element.styles.hasOwnProperty('text-indent')) {
@@ -1157,6 +1202,9 @@ export default {
1157
1202
  if (this.disabled) {
1158
1203
  return
1159
1204
  }
1205
+ if (!this.editor.range) {
1206
+ return
1207
+ }
1160
1208
  const fn = element => {
1161
1209
  if (element.hasStyles() && element.styles.hasOwnProperty('text-indent')) {
1162
1210
  let val = element.styles['text-indent']
@@ -1177,7 +1225,7 @@ export default {
1177
1225
  fn(block)
1178
1226
  }
1179
1227
  } else {
1180
- const result = this.getElementsByRange(true, false)
1228
+ const result = this.editor.getElementsByRange(true, false)
1181
1229
  result.forEach(item => {
1182
1230
  const block = item.element.getBlock()
1183
1231
  const inblock = item.element.getInblock()
@@ -1197,6 +1245,9 @@ export default {
1197
1245
  if (this.disabled) {
1198
1246
  return
1199
1247
  }
1248
+ if (!this.editor.range) {
1249
+ return
1250
+ }
1200
1251
  if (!url || typeof url != 'string') {
1201
1252
  throw new Error('An image address must be given')
1202
1253
  }
@@ -1219,6 +1270,9 @@ export default {
1219
1270
  if (this.disabled) {
1220
1271
  return
1221
1272
  }
1273
+ if (!this.editor.range) {
1274
+ return
1275
+ }
1222
1276
  if (!url || typeof url != 'string') {
1223
1277
  throw new Error('A video address must be given')
1224
1278
  }
@@ -1244,171 +1298,199 @@ export default {
1244
1298
  },
1245
1299
  //api:选区是否含有代码块样式
1246
1300
  hasPreStyle() {
1301
+ if (!this.editor.range) {
1302
+ return false
1303
+ }
1247
1304
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
1248
1305
  return this.editor.range.anchor.element.isPreStyle()
1249
- } else {
1250
- const result = this.editor.getElementsByRange(true, false)
1251
- return result.some(item => {
1252
- return item.element.isPreStyle()
1253
- })
1254
1306
  }
1307
+ const result = this.editor.getElementsByRange(true, false)
1308
+ return result.some(item => {
1309
+ return item.element.isPreStyle()
1310
+ })
1255
1311
  },
1256
1312
  //api:选区是否含有引用
1257
1313
  hasQuote() {
1314
+ if (!this.editor.range) {
1315
+ return false
1316
+ }
1258
1317
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
1259
1318
  const block = this.editor.range.anchor.element.getBlock()
1260
1319
  return block.parsedom == 'blockquote'
1261
- } else {
1262
- const result = this.editor.getElementsByRange(true, false)
1263
- return result.some(item => {
1264
- if (item.element.isBlock()) {
1265
- return item.element.parsedom == 'blockquote'
1266
- } else {
1267
- const block = item.element.getBlock()
1268
- return block.parsedom == 'blockquote'
1269
- }
1270
- })
1271
1320
  }
1321
+ const result = this.editor.getElementsByRange(true, false)
1322
+ return result.some(item => {
1323
+ if (item.element.isBlock()) {
1324
+ return item.element.parsedom == 'blockquote'
1325
+ } else {
1326
+ const block = item.element.getBlock()
1327
+ return block.parsedom == 'blockquote'
1328
+ }
1329
+ })
1272
1330
  },
1273
1331
  //api:选区是否含有列表
1274
1332
  hasList(ordered = false) {
1333
+ if (!this.editor.range) {
1334
+ return false
1335
+ }
1275
1336
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
1276
1337
  const block = this.editor.range.anchor.element.getBlock()
1277
1338
  return blockIsList(block, ordered)
1278
- } else {
1279
- const result = this.editor.getElementsByRange(true, false)
1280
- return result.some(item => {
1281
- if (item.element.isBlock()) {
1282
- return blockIsList(item.element, ordered)
1283
- } else {
1284
- const block = item.element.getBlock()
1285
- return blockIsList(block, ordered)
1286
- }
1287
- })
1288
1339
  }
1340
+ const result = this.editor.getElementsByRange(true, false)
1341
+ return result.some(item => {
1342
+ if (item.element.isBlock()) {
1343
+ return blockIsList(item.element, ordered)
1344
+ } else {
1345
+ const block = item.element.getBlock()
1346
+ return blockIsList(block, ordered)
1347
+ }
1348
+ })
1289
1349
  },
1290
1350
  //api:选区是否含有链接
1291
1351
  hasLink() {
1352
+ if (!this.editor.range) {
1353
+ return false
1354
+ }
1292
1355
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
1293
1356
  return !!this.getParsedomElementByElement(this.editor.range.anchor.element, 'a')
1294
- } else {
1295
- const result = this.editor.getElementsByRange(true, true).filter(item => {
1296
- return item.element.isText()
1297
- })
1298
- return result.some(item => {
1299
- return !!this.getParsedomElementByElement(item.element, 'a')
1300
- })
1301
1357
  }
1358
+ const result = this.editor.getElementsByRange(true, true).filter(item => {
1359
+ return item.element.isText()
1360
+ })
1361
+ return result.some(item => {
1362
+ return !!this.getParsedomElementByElement(item.element, 'a')
1363
+ })
1302
1364
  },
1303
1365
  //api:选区是否含有表格
1304
1366
  hasTable() {
1367
+ if (!this.editor.range) {
1368
+ return false
1369
+ }
1305
1370
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
1306
1371
  const block = this.editor.range.anchor.element.getBlock()
1307
1372
  return block.parsedom == 'table'
1308
- } else {
1309
- const result = this.editor.getElementsByRange(true, false)
1310
- return result.some(item => {
1311
- if (item.element.isBlock()) {
1312
- return item.element.parsedom == 'table'
1313
- } else {
1314
- const block = item.element.getBlock()
1315
- return block.parsedom == 'table'
1316
- }
1317
- })
1318
1373
  }
1374
+ const result = this.editor.getElementsByRange(true, false)
1375
+ return result.some(item => {
1376
+ if (item.element.isBlock()) {
1377
+ return item.element.parsedom == 'table'
1378
+ } else {
1379
+ const block = item.element.getBlock()
1380
+ return block.parsedom == 'table'
1381
+ }
1382
+ })
1319
1383
  },
1320
1384
  //api:选区是否含有任务列表
1321
1385
  hasTask() {
1386
+ if (!this.editor.range) {
1387
+ return false
1388
+ }
1322
1389
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
1323
1390
  const block = this.editor.range.anchor.element.getBlock()
1324
1391
  return blockIsTask(block)
1325
- } else {
1326
- const result = this.editor.getElementsByRange(true, false)
1327
- return result.some(item => {
1328
- if (item.element.isBlock()) {
1329
- return blockIsTask(item.element)
1330
- } else {
1331
- const block = item.element.getBlock()
1332
- return blockIsTask(block)
1333
- }
1334
- })
1335
1392
  }
1393
+ const result = this.editor.getElementsByRange(true, false)
1394
+ return result.some(item => {
1395
+ if (item.element.isBlock()) {
1396
+ return blockIsTask(item.element)
1397
+ } else {
1398
+ const block = item.element.getBlock()
1399
+ return blockIsTask(block)
1400
+ }
1401
+ })
1336
1402
  },
1337
1403
  //选区是否含有图片
1338
1404
  hasImage() {
1405
+ if (!this.editor.range) {
1406
+ return false
1407
+ }
1339
1408
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
1340
1409
  return this.editor.range.anchor.element.isClosed() && this.editor.range.anchor.element.parsedom == 'img'
1341
- } else {
1342
- const result = this.editor.getElementsByRange(true, true)
1343
- return result.some(item => {
1344
- return item.element.isClosed() && item.element.parsedom == 'img'
1345
- })
1346
1410
  }
1411
+ const result = this.editor.getElementsByRange(true, true)
1412
+ return result.some(item => {
1413
+ return item.element.isClosed() && item.element.parsedom == 'img'
1414
+ })
1347
1415
  },
1348
1416
  //选区是否含有视频
1349
1417
  hasVideo() {
1418
+ if (!this.editor.range) {
1419
+ return false
1420
+ }
1350
1421
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
1351
1422
  return this.editor.range.anchor.element.isClosed() && this.editor.range.anchor.element.parsedom == 'video'
1352
- } else {
1353
- const result = this.editor.getElementsByRange(true, true)
1354
- return result.some(item => {
1355
- return item.element.isClosed() && item.element.parsedom == 'video'
1356
- })
1357
1423
  }
1424
+ const result = this.editor.getElementsByRange(true, true)
1425
+ return result.some(item => {
1426
+ return item.element.isClosed() && item.element.parsedom == 'video'
1427
+ })
1358
1428
  },
1359
1429
  //api:选区是否全部在引用内
1360
1430
  inQuote() {
1431
+ if (!this.editor.range) {
1432
+ return false
1433
+ }
1361
1434
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
1362
1435
  const block = this.editor.range.anchor.element.getBlock()
1363
1436
  return block.parsedom == 'blockquote'
1364
- } else {
1365
- const result = this.editor.getElementsByRange(true, false)
1366
- return result.every(item => {
1367
- if (item.element.isBlock()) {
1368
- return item.element.parsedom == 'blockquote'
1369
- } else {
1370
- const block = item.element.getBlock()
1371
- return block.parsedom == 'blockquote'
1372
- }
1373
- })
1374
1437
  }
1438
+ const result = this.editor.getElementsByRange(true, false)
1439
+ return result.every(item => {
1440
+ if (item.element.isBlock()) {
1441
+ return item.element.parsedom == 'blockquote'
1442
+ } else {
1443
+ const block = item.element.getBlock()
1444
+ return block.parsedom == 'blockquote'
1445
+ }
1446
+ })
1375
1447
  },
1376
1448
  //api:选区是否全部在列表内
1377
1449
  inList(ordered = false) {
1450
+ if (!this.editor.range) {
1451
+ return false
1452
+ }
1378
1453
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
1379
1454
  const block = this.editor.range.anchor.element.getBlock()
1380
1455
  return blockIsList(block, ordered)
1381
- } else {
1382
- const result = this.editor.getElementsByRange(true, false)
1383
- return result.every(item => {
1384
- if (item.element.isBlock()) {
1385
- return blockIsList(item.element, ordered)
1386
- } else {
1387
- const block = item.element.getBlock()
1388
- return blockIsList(block, ordered)
1389
- }
1390
- })
1391
1456
  }
1457
+ const result = this.editor.getElementsByRange(true, false)
1458
+ return result.every(item => {
1459
+ if (item.element.isBlock()) {
1460
+ return blockIsList(item.element, ordered)
1461
+ } else {
1462
+ const block = item.element.getBlock()
1463
+ return blockIsList(block, ordered)
1464
+ }
1465
+ })
1392
1466
  },
1393
1467
  //api:选区是否全部在任务列表里
1394
1468
  inTask() {
1469
+ if (!this.editor.range) {
1470
+ return false
1471
+ }
1395
1472
  if (this.editor.range.anchor.isEqual(this.editor.range.focus)) {
1396
1473
  const block = this.editor.range.anchor.element.getBlock()
1397
1474
  return blockIsTask(block)
1398
- } else {
1399
- const result = this.editor.getElementsByRange(true, false)
1400
- return result.every(item => {
1401
- if (item.element.isBlock()) {
1402
- return blockIsTask(item.element)
1403
- } else {
1404
- const block = item.element.getBlock()
1405
- return blockIsTask(block)
1406
- }
1407
- })
1408
1475
  }
1476
+ const result = this.editor.getElementsByRange(true, false)
1477
+ return result.every(item => {
1478
+ if (item.element.isBlock()) {
1479
+ return blockIsTask(item.element)
1480
+ } else {
1481
+ const block = item.element.getBlock()
1482
+ return blockIsTask(block)
1483
+ }
1484
+ })
1409
1485
  },
1410
1486
  //api:创建一个空的表格
1411
1487
  insertTable(rowLength, colLength) {
1488
+ if (this.disabled) {
1489
+ return
1490
+ }
1491
+ if (!this.editor.range) {
1492
+ return
1493
+ }
1412
1494
  const table = new AlexElement('block', 'table', null, null, null)
1413
1495
  const tbody = new AlexElement('inblock', 'tbody', null, null, null)
1414
1496
  this.editor.addElementTo(tbody, table)
@@ -1439,6 +1521,9 @@ export default {
1439
1521
  if (this.disabled) {
1440
1522
  return
1441
1523
  }
1524
+ if (!this.editor.range) {
1525
+ return
1526
+ }
1442
1527
  const pre = this.getCurrentParsedomElement('pre')
1443
1528
  if (pre) {
1444
1529
  let content = ''
@@ -1517,6 +1602,9 @@ export default {
1517
1602
  if (this.disabled) {
1518
1603
  return
1519
1604
  }
1605
+ if (!this.editor.range) {
1606
+ return
1607
+ }
1520
1608
  this.editor.insertText(text)
1521
1609
  this.editor.formatElementStack()
1522
1610
  this.editor.domRender()
@@ -1527,6 +1615,9 @@ export default {
1527
1615
  if (this.disabled) {
1528
1616
  return
1529
1617
  }
1618
+ if (!this.editor.range) {
1619
+ return
1620
+ }
1530
1621
  const elements = this.editor.parseHtml(html)
1531
1622
  for (let i = 0; i < elements.length; i++) {
1532
1623
  this.editor.insertElement(elements[i], false)
@@ -1861,6 +1952,12 @@ export default {
1861
1952
  :deep(a) {
1862
1953
  cursor: pointer;
1863
1954
  }
1955
+
1956
+ :deep(table) {
1957
+ td:not(:last-child)::after {
1958
+ cursor: auto;
1959
+ }
1960
+ }
1864
1961
  }
1865
1962
  }
1866
1963
 
package/src/core/index.js CHANGED
@@ -353,6 +353,9 @@ export const tableHandle = function (element) {
353
353
 
354
354
  //更新代码块内的光标位置
355
355
  const updateRangeInPre = function (element, originalTextElements, newElements) {
356
+ if (!this.range) {
357
+ return
358
+ }
356
359
  //如果虚拟光标的起点在代码块内对虚拟光标的起点进行重新定位
357
360
  if (this.range.anchor.element.getBlock().isEqual(element)) {
358
361
  //获取起点所在文本元素的在所有文本元素中的序列
package/src/index.js CHANGED
@@ -7,7 +7,7 @@ import './icon/iconfont.css'
7
7
  //引入国际化
8
8
  import i18n from './locale'
9
9
  //版本号
10
- const version = '0.0.16'
10
+ const version = '0.0.18'
11
11
  //安装函数
12
12
  const install = (app, props) => {
13
13
  const locale = (props ? props.locale : 'zh_CN') || 'zh_CN'