@alitons/ckeditor5 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@alitons/ckeditor5",
3
3
  "author": "Aliton Silva",
4
4
  "description": "Ckeditor 5 Personalizado adicionados campos para atender as demandas da SEAD/AC",
5
- "version": "0.0.5",
5
+ "version": "0.0.6",
6
6
  "license": "SEE LICENSE IN LICENSE.md",
7
7
  "private": false,
8
8
  "main": "./build/ckeditor.js",
@@ -95,7 +95,6 @@ export default class NumberedDivList extends Plugin {
95
95
  classes: 'num-list',
96
96
  },
97
97
  model: (viewElement, { writer }) => {
98
- console.log('upcast num-list', viewElement);
99
98
  const attrs: any = {};
100
99
 
101
100
  const ds = viewElement.getAttribute('data-style');
@@ -217,75 +216,75 @@ export default class NumberedDivList extends Plugin {
217
216
 
218
217
 
219
218
  // não permitir que saia da lista caso exista o atributo data-block
220
- viewDoc.on('keydown', (evt, data) => {
221
- if (data.keyCode !== keyCodes.enter || data.shiftKey) return;
222
- if (!editor.model.document.selection.isCollapsed) return;
223
-
224
- if(config?.disableEnter === true) {
225
- return;
226
- }
227
-
228
- const { model } = editor;
229
- const pos = model.document.selection.getFirstPosition();
230
- if (!pos) return;
231
-
232
- const sealed = getNearestSealedListFromPos(pos);
233
- if (!sealed) return; // só intercepta se estiver dentro de um sealed
234
-
235
- data.preventDefault(); evt.stop();
236
-
237
- model.change(writer => {
238
- // deixa o enter nativo dividir o bloco
239
- editor.execute('enter');
240
-
241
- const posAfter = model.document.selection.getFirstPosition();
242
- if (!posAfter) return;
243
- const newBlock = posAfter.parent as any;
244
- if (!newBlock) return;
245
-
246
- // queremos transformar o novo bloco em um novo numItem,
247
- // mantendo-o dentro de ALGUM numList que esteja dentro do `sealed` (o mais próximo)
248
- // pega o numItem atual (mais próximo)
249
- let currentItem = posAfter.findAncestor('numItem');
250
-
251
- // se o bloco recém criado ficou fora de um numItem,
252
- // cria um numItem irmão do atual (se existir) dentro do mesmo numList
253
- if (!currentItem) {
254
- const listForNew = getNearestSealedListFromPos(posAfter) || sealed;
255
- const newItem = writer.createElement('numItem');
256
- writer.insert(newItem, writer.createPositionAt(listForNew, 'end'));
257
- writer.move(writer.createRangeOn(newBlock), writer.createPositionAt(newItem, 0));
258
- writer.setSelection(ensureTypablePosInItem(writer, newItem, false));
259
- return;
260
- }
261
-
262
- // garantir que este numItem pertence a um numList que está dentro do sealed mais próximo
263
- let itsList = currentItem.parent; // deve ser um numList
264
- if (!isInside(itsList, sealed)) {
265
- // se por alguma razão o split empurrou pra fora, anexa de volta ao sealed
266
- const fallback = firstItem(sealed) || null;
267
- if (fallback) {
268
- const newItem = writer.createElement('numItem');
269
- writer.insert(newItem, writer.createPositionAfter(fallback));
270
- writer.move(writer.createRangeOn(newBlock), writer.createPositionAt(newItem, 0));
271
- writer.setSelection(ensureTypablePosInItem(writer, newItem, false));
272
- } else {
273
- // sealed vazio (raro): crie o primeiro item
274
- const newItem = writer.createElement('numItem');
275
- writer.insert(newItem, writer.createPositionAt(sealed, 0));
276
- writer.move(writer.createRangeOn(newBlock), writer.createPositionAt(newItem, 0));
277
- writer.setSelection(ensureTypablePosInItem(writer, newItem, false));
278
- }
279
- return;
280
- }
281
-
282
- // caso normal: cria irmão dentro do mesmo numList
283
- const newItem = writer.createElement('numItem');
284
- writer.insert(newItem, writer.createPositionAfter(currentItem));
285
- writer.move(writer.createRangeOn(newBlock), writer.createPositionAt(newItem, 0));
286
- writer.setSelection(ensureTypablePosInItem(writer, newItem, false));
287
- });
288
- }, { priority: 'high' });
219
+ // viewDoc.on('keydown', (evt, data) => {
220
+ // if (data.keyCode !== keyCodes.enter || data.shiftKey) return;
221
+ // if (!editor.model.document.selection.isCollapsed) return;
222
+
223
+ // if(config?.disableEnter === true) {
224
+ // return;
225
+ // }
226
+
227
+ // const { model } = editor;
228
+ // const pos = model.document.selection.getFirstPosition();
229
+ // if (!pos) return;
230
+
231
+ // const sealed = getNearestSealedListFromPos(pos);
232
+ // if (!sealed) return; // só intercepta se estiver dentro de um sealed
233
+
234
+ // data.preventDefault(); evt.stop();
235
+
236
+ // model.change(writer => {
237
+ // // deixa o enter nativo dividir o bloco
238
+ // editor.execute('enter');
239
+
240
+ // const posAfter = model.document.selection.getFirstPosition();
241
+ // if (!posAfter) return;
242
+ // const newBlock = posAfter.parent as any;
243
+ // if (!newBlock) return;
244
+
245
+ // // queremos transformar o novo bloco em um novo numItem,
246
+ // // mantendo-o dentro de ALGUM numList que esteja dentro do `sealed` (o mais próximo)
247
+ // // pega o numItem atual (mais próximo)
248
+ // let currentItem = posAfter.findAncestor('numItem');
249
+
250
+ // // se o bloco recém criado ficou fora de um numItem,
251
+ // // cria um numItem irmão do atual (se existir) dentro do mesmo numList
252
+ // if (!currentItem) {
253
+ // const listForNew = getNearestSealedListFromPos(posAfter) || sealed;
254
+ // const newItem = writer.createElement('numItem');
255
+ // writer.insert(newItem, writer.createPositionAt(listForNew, 'end'));
256
+ // writer.move(writer.createRangeOn(newBlock), writer.createPositionAt(newItem, 0));
257
+ // writer.setSelection(ensureTypablePosInItem(writer, newItem, false));
258
+ // return;
259
+ // }
260
+
261
+ // // garantir que este numItem pertence a um numList que está dentro do sealed mais próximo
262
+ // let itsList = currentItem.parent; // deve ser um numList
263
+ // if (!isInside(itsList, sealed)) {
264
+ // // se por alguma razão o split empurrou pra fora, anexa de volta ao sealed
265
+ // const fallback = firstItem(sealed) || null;
266
+ // if (fallback) {
267
+ // const newItem = writer.createElement('numItem');
268
+ // writer.insert(newItem, writer.createPositionAfter(fallback));
269
+ // writer.move(writer.createRangeOn(newBlock), writer.createPositionAt(newItem, 0));
270
+ // writer.setSelection(ensureTypablePosInItem(writer, newItem, false));
271
+ // } else {
272
+ // // sealed vazio (raro): crie o primeiro item
273
+ // const newItem = writer.createElement('numItem');
274
+ // writer.insert(newItem, writer.createPositionAt(sealed, 0));
275
+ // writer.move(writer.createRangeOn(newBlock), writer.createPositionAt(newItem, 0));
276
+ // writer.setSelection(ensureTypablePosInItem(writer, newItem, false));
277
+ // }
278
+ // return;
279
+ // }
280
+
281
+ // // caso normal: cria irmão dentro do mesmo numList
282
+ // const newItem = writer.createElement('numItem');
283
+ // writer.insert(newItem, writer.createPositionAfter(currentItem));
284
+ // writer.move(writer.createRangeOn(newBlock), writer.createPositionAt(newItem, 0));
285
+ // writer.setSelection(ensureTypablePosInItem(writer, newItem, false));
286
+ // });
287
+ // }, { priority: 'high' });
289
288
 
290
289
  // não permitir que backspace/delete saia da lista caso exista o atributo data-block
291
290
  viewDoc.on(
@@ -299,110 +298,42 @@ export default class NumberedDivList extends Plugin {
299
298
  const model = editor.model;
300
299
  const sel = model.document.selection;
301
300
 
302
- if (!sel.isCollapsed && (selectionSpansBlocks(model) || selectionTouchesElements(model))) {
303
- data.preventDefault();
304
- evt.stop();
305
-
306
- model.change( writer => {
307
- const anchor = sel.anchor!;
308
- writer.setSelection( anchor );
309
- // apaga 1 caractere para trás/à frente (se existir)
310
- if (isBackspace) {
311
- // @ts-ignore
312
- editor.execute('delete', { unit: 'character', direction: 'backward' });
313
- } else {
314
- // @ts-ignore
315
- editor.execute('delete', { unit: 'character', direction: 'forward' });
316
- }
317
- });
318
- return;
319
- }
320
-
321
- model.change(writer => {
322
- const pos = model.document.selection.getFirstPosition();
323
- if (!pos) return;
324
-
325
- const sealed = getNearestSealedListFromPos(pos);
326
- if (!sealed) return;
327
-
328
- const numItem = pos.findAncestor('numItem');
329
- const numList = pos.findAncestor('numList') as any;
330
- const block = pos.parent as any;
331
-
332
- if (!numItem || !block?.is?.('element') || !numList || numList.getAttribute('data-block')) return;
301
+ data.preventDefault();
302
+ evt.stop();
333
303
 
304
+ model.change( async (writer: any) => {
305
+ // @ts-ignore
306
+ const getPos = sel.getFirstPosition() as any;
307
+ const blocoPos = getPos?.findAncestor('paragraph') ?? getPos?.findAncestor('numItem') ?? getPos?.findAncestor('numList') ?? null;
308
+ const itemPos = blocoPos.findAncestor('numItem');
309
+ const listPos = itemPos.findAncestor('numList');
334
310
 
335
- // é o primeiro bloco dentro do item?
336
- const isFirstItem = sealed.getChild(0) === block;
337
- const isLastItem = sealed.getChild(sealed.childCount - 1) === block;
338
- const atStartOfBlock = pos.isAtStart && numItem.getChild(0) === block;
339
- const atEndOfBlock = pos.isAtEnd && numItem.getChild(numItem.childCount - 1) === block;
311
+ // @ts-ignore
312
+ editor.execute(isBackspace ? 'delete' : 'deleteForward', { unit: 'character', direction: isBackspace ? 'backward' : 'forward' });
340
313
 
341
- if (isBackspace && isFirstItem && atStartOfBlock) {
342
- data.preventDefault(); evt.stop();
343
- writer.setSelection(ensureTypablePosInItem(writer, numItem, false)); // não deixa "sair"
344
- return;
314
+ if(blocoPos.is('element', 'paragraph') && !blocoPos?.getChild(0)) {
315
+ writer.remove(blocoPos);
345
316
  }
346
-
347
- if (isDelete && isLastItem && atEndOfBlock) {
348
- data.preventDefault(); evt.stop();
349
- writer.setSelection(ensureTypablePosInItem(writer, numItem, true)); // idem
350
- return;
351
- }
352
-
353
- if (isFirstItem) {
354
- // Evita que o Delete padrão remova o container todo
355
- data.preventDefault();
356
- evt.stop();
357
-
358
- // Estratégia: cria (se necessário) um parágrafo antes do numList e move o caret pra lá.
359
- // Se já existir algo antes do numList e for bloco digitável, só posiciona o caret.
360
- const before = writer.createPositionBefore(numItem);
361
- const parent = before.parent;
362
-
363
- // Se o pai aceita parágrafos diretamente e não há bloco imediatamente anterior,
364
- // crie um parágrafo novo; do contrário, apenas posicione o caret.
365
- let setToPos = before;
366
-
367
- const prevSibling = numItem.previousSibling;
368
- if (!prevSibling) {
369
- const canParagraph =
370
- // @ts-ignore
371
- editor.model.schema.checkChild(parent, 'paragraph');
372
-
373
- const hasText = Array.from(parent.getChildren()).some(child => {
374
- // @ts-ignore
375
- return child.is('element', 'paragraph') && child.getChild(0)?.is('text');
376
- });
377
-
378
- if (canParagraph) {
379
- if (hasText) {
380
- const paragraph = writer.createElement('paragraph');
381
- writer.insert(paragraph, before);
382
- setToPos = writer.createPositionAt(paragraph, 0);
383
- } else {
384
- writer.remove(numItem);
385
- setToPos = before;
386
- }
387
- }
388
- } else {
389
- writer.remove(numItem);
390
- setToPos = writer.createPositionAfter(prevSibling);
391
- }
392
-
393
- writer.setSelection(setToPos);
394
-
395
- if (numList.childCount === 0 && !numList.getAttribute('data-block')) {
396
- const aboveList = numList.findAncestor('numList');
397
- const lastChild = aboveList ? Array.from(aboveList.getChildren()).pop() : null as any;
398
- if (aboveList && lastChild) {
399
- writer.setSelection(writer.createPositionAfter(lastChild));
400
- }
401
-
402
- writer.remove(numList);
403
- }
317
+ if(itemPos && itemPos.childCount === 0) {
318
+ writer.remove(itemPos);
404
319
  }
320
+ if(listPos && listPos.childCount === 0) {
321
+ writer.remove(listPos);
322
+ }
323
+
324
+
405
325
  });
326
+
327
+ if(isBackspace) {
328
+ model.change( async (writer: any) => {
329
+ // coloca o cursor no final do bloco atual
330
+ const posAfter = model.document.selection.getFirstPosition() as any;
331
+ writer.setSelection(writer.createPositionAt(posAfter.parent, 'end'));
332
+ });
333
+ }
334
+
335
+ return;
336
+
406
337
  },
407
338
  { priority: 'high' }
408
339
  );
@@ -492,9 +423,13 @@ class ToggleNumberedDivListCommand {
492
423
  const model = editor.model;
493
424
 
494
425
  const selection = model.document.selection;
495
- const firstPos = selection.getFirstPosition();
426
+ const firstPos = selection.getFirstPosition('paragraph') ?? selection.getFirstPosition('numItem');
496
427
  const existingItem = firstPos?.findAncestor('numItem');
428
+ const paragraphAbove = firstPos?.findAncestor('paragraph');
429
+ const firstParagraphItem = existingItem ? existingItem?.getChild(0) : null;
497
430
  let existingList = firstPos?.findAncestor('numList');
431
+ let firstItemInList = existingList ? existingList.getChild(0) : null;
432
+ const selectedItemIndex = existingList ? existingList.getChildIndex(existingItem) : null;
498
433
 
499
434
  if(value === 'recuar') {
500
435
  if (!existingItem || !existingList) return;
@@ -503,6 +438,12 @@ class ToggleNumberedDivListCommand {
503
438
 
504
439
  if(value !== undefined) {
505
440
  const block = firstPos.parent as any;
441
+
442
+ if(existingItem && firstItemInList === existingItem) {
443
+ if(value === 'decimal') {
444
+ return;
445
+ }
446
+ }
506
447
 
507
448
  if(existingItem?.getChild(0) === block) {
508
449
  return editor.execute('toggleNumberedDivList', { value: undefined, start: start });
@@ -510,54 +451,33 @@ class ToggleNumberedDivListCommand {
510
451
  }
511
452
 
512
453
  model.change((writer: any) => {
513
- const paragraphAbove = firstPos?.findAncestor('paragraph');
514
- const firstParagraphItem = existingItem ? existingItem?.getChild(0) : null;
515
-
516
- console.log(firstParagraphItem, paragraphAbove);
517
-
518
- if (value == undefined && firstParagraphItem == paragraphAbove) {
519
- const prevItem = existingItem?.previousSibling ?? null;
520
- if (prevItem) {
521
- writer.setSelection(writer.createPositionAt(prevItem, 'end'));
522
- const items: any[] = [];
523
- for (const child of existingItem.getChildren()) {
524
- items.push(child);
525
- }
526
- for (const item of items) {
527
- while (item.childCount > 0) {
528
- const child = item?.getChild(0);
529
- const rangeOnChild = writer.createRangeOn(child);
530
- const paragraph = writer.createElement('paragraph');
531
- writer.insert(paragraph, writer.createPositionAt(prevItem, 'end'));
532
- writer.move(rangeOnChild, writer.createPositionAt(paragraph, 'end'));
533
- }
534
- }
535
-
536
- writer.remove(existingItem);
454
+ // se não for o primeiro do item
455
+ if ((value == undefined || value == 'decimal') && paragraphAbove === firstParagraphItem && selectedItemIndex >= 0) {
456
+
457
+ if(!selectedItemIndex) return;
537
458
 
538
- const lastParagraph = prevItem?.getChild(prevItem.childCount - 1);
459
+ // procura o numItem acima da seleção
460
+ let prevItem = existingList.getChild(selectedItemIndex - 1);
461
+ if (!prevItem) return;
539
462
 
540
- // coloca o cursor no final do novo item
541
- // @ts-ignore
542
- writer.setSelection(writer.createPositionAt(lastParagraph, 'end'));
463
+ if(prevItem.is('element', 'numItem')) {
464
+ // criar uma nova lista abaixo da prevItem
465
+ const newBlock = writer.createElement('numList', {
466
+ ...(value && value !== 'decimal' ? { 'data-style': value } : {})
467
+ });
468
+ const insertPos = writer.createPositionAfter(prevItem);
469
+ writer.insert(newBlock, insertPos);
543
470
 
544
- return editor.execute('toggleNumberedDivList', { value: value ?? 'decimal', start: start });
471
+ writer.move(writer.createRangeOn(existingItem), writer.createPositionAt(newBlock, 0));
472
+ } else {
473
+ // move para o final do prevItem
474
+ writer.move(writer.createRangeOn(existingItem), writer.createPositionAt(prevItem, 'end'));
545
475
  }
476
+
546
477
  return;
547
- }
548
-
549
- // VERIFICA SE EXISTE UMA NUMLIST ACIMA DESSA SELEÇÃO
550
- let parent = firstPos.parent;
551
-
552
- while (parent) {
553
- if (parent.is('element', 'numList')) {
554
- existingList = parent;
555
- break;
556
- }
557
- parent = parent.parent;
558
- }
478
+ }
559
479
 
560
- const blocks = Array.from(selection.getSelectedBlocks());
480
+ const blocks = Array.from(selection.getSelectedBlocks()) as any[];
561
481
 
562
482
  if (!blocks.length) {
563
483
  const insertRange = findOptimalInsertionRange(selection, model);
@@ -572,9 +492,21 @@ class ToggleNumberedDivListCommand {
572
492
  }
573
493
 
574
494
  if (existingList && value === undefined) {
575
- const insertPos = writer.createPositionBefore(paragraphAbove);
495
+ // verifica se está dentro de um numItem
496
+ const isInsideNumItem = blocks[0].findAncestor('numItem') === existingItem;
497
+ let insertPos = writer.createPositionBefore(paragraphAbove);
498
+ let numItem;
499
+
500
+ if (isInsideNumItem && existingItem) {
501
+ // cria um num item abaixo do existente
502
+ numItem = writer.createElement('numItem');
503
+ insertPos = writer.createPositionAfter(existingItem);
504
+ writer.insert(numItem, insertPos);
505
+ } else {
506
+ numItem = writer.createElement('numItem');
507
+ }
508
+
576
509
  for (const block of blocks) {
577
- const numItem = writer.createElement('numItem');
578
510
  writer.insert(numItem, insertPos);
579
511
  writer.move(writer.createRangeOn(block), writer.createPositionAt(numItem, 0));
580
512
  }
@@ -614,100 +546,28 @@ class ToggleNumberedDivListCommand {
614
546
  function shiftTab(editor: any) {
615
547
  const model = editor.model;
616
548
 
617
- const posBefore = model.document.selection.getFirstPosition();
618
- const inItem = posBefore?.findAncestor('numItem');
619
- const inList = posBefore?.findAncestor('numList');
620
- const parentItem = inList.findAncestor('numItem') ?? null;
621
- const db = inList.getAttribute('dataBlock');
549
+ const selection = model.document.selection;
550
+ const firstBlock = selection.getFirstPosition('paragraph') ?? selection.getFirstPosition('numItem');
551
+ const firstPos = firstBlock.is('element', 'numItem') ? firstBlock : firstBlock.findAncestor('numItem');
552
+ const existingList = firstPos?.findAncestor('numList');
553
+ const selectedItemIndex = existingList ? existingList.getChildIndex(firstPos) : null;
554
+ const numListPai = existingList ? existingList?.findAncestor('numList') : null;
555
+ const selectedListPaiIndex = numListPai ? numListPai.getChildIndex(existingList) : null;
622
556
 
623
557
  model.change((writer: any) => {
624
-
625
- if (parentItem) {
626
- const insertPos = writer.createPositionAfter(parentItem);
627
- const newItem = writer.createElement('numItem');
628
- writer.insert(newItem, insertPos);
629
- const items: any[] = [];
630
- for (const child of inItem.getChildren()) {
631
- items.push(child);
632
- }
633
- for (const item of items) {
634
- while (item.childCount > 0) {
635
- const child = item.getChild(0);
636
- const rangeOnChild = writer.createRangeOn(child);
637
- const paragraph = writer.createElement('paragraph');
638
- writer.insert(paragraph, writer.createPositionAt(newItem, 'end'));
639
- writer.move(rangeOnChild, writer.createPositionAt(paragraph, 'end'));
640
- }
641
- }
642
-
643
- const lastChild = newItem.getChild(newItem.childCount - 1);
644
- if (lastChild) {
645
- // @ts-ignore
646
- writer.setSelection(writer.createPositionAt(lastChild, 'end'));
647
- } else {
648
- writer.setSelection(writer.createPositionAt(newItem, 'end'));
649
- }
650
- } else {
651
- const prevList = db ? inList : inItem.previousSibling;
652
-
653
- if ((prevList && prevList.is('element', 'numItem')) || db) {
654
- const items: any[] = [];
655
- for (const child of inItem.getChildren()) {
656
- items.push(child);
657
- }
658
- for (const item of items) {
659
- while (item.childCount > 0) {
660
- const child = item.getChild(0);
661
- const rangeOnChild = writer.createRangeOn(child);
662
- const paragraph = writer.createElement('paragraph');
663
- writer.insert(paragraph, writer.createPositionAt(prevList, 'end'));
664
- writer.move(rangeOnChild, writer.createPositionAt(paragraph, 'end'));
665
- }
666
- }
667
-
668
- const lastChild = prevList.getChild(prevList.childCount - 1);
669
- if (lastChild) {
670
- // @ts-ignore
671
- writer.setSelection(writer.createPositionAt(lastChild, 'end'));
672
- } else {
673
- writer.setSelection(writer.createPositionAt(prevList, 'end'));
674
- }
675
-
676
- } else {
677
- const insertPos = db ? writer.createPositionAt(inList, 'end') : writer.createPositionAfter(inList);
678
-
679
- const items: any[] = [];
680
- for (const child of inItem.getChildren()) {
681
- items.push(child);
682
- }
683
- for (const item of items) {
684
- while (item.childCount > 0) {
685
- const child = item.getChild(0);
686
- const rangeOnChild = writer.createRangeOn(child);
687
- const paragraph = writer.createElement('paragraph');
688
- writer.insert(paragraph, insertPos);
689
- writer.move(rangeOnChild, writer.createPositionAt(paragraph, 'end'));
690
- }
691
- }
692
-
693
- }
694
-
695
- const lastChild = prevList ? prevList.getChild(prevList.childCount - 1) : null;
696
- if (lastChild) {
697
- // @ts-ignore
698
- writer.setSelection(writer.createPositionAt(lastChild, 'end'));
699
- } else {
700
- writer.setSelection(writer.createPositionAt(inList, 'end'));
701
- }
558
+ // se for o primeiro item da lista
559
+ if(numListPai && selectedListPaiIndex >= 0 && selectedItemIndex === 0) {
560
+ writer.move(writer.createRangeOn(firstPos), writer.createPositionAt(numListPai, selectedListPaiIndex));
561
+ return;
702
562
  }
703
-
704
- writer.remove(inItem);
705
-
706
-
707
- if (inList.childCount === 0) {
708
- if (!db) writer.remove(inList);
563
+ // se for o último item da lista
564
+ if(numListPai && selectedListPaiIndex >= 0 && selectedItemIndex === existingList.childCount -1) {
565
+ writer.move(writer.createRangeOn(firstPos), writer.createPositionAt(numListPai, selectedListPaiIndex + 1));
566
+ return;
709
567
  }
710
568
 
569
+ // criar novas regras de acordo com a necessidade
570
+
711
571
  });
712
572
  }
713
573