@@ -7,7 +7,7 @@ import {printTree} from 'tree-dump/lib/printTree';
77import { createRegistry } from '../registry/registry' ;
88import { PersistedSlice } from '../slice/PersistedSlice' ;
99import { stringify } from '../../../json-text/stringify' ;
10- import { CommonSliceType , type SliceTypeSteps , type SliceType } from '../slice' ;
10+ import { CommonSliceType , type SliceTypeSteps , type SliceType , type SliceTypeStep } from '../slice' ;
1111import { isLetter , isPunctuation , isWhitespace , stepsEqual } from './util' ;
1212import { ValueSyncStore } from '../../../util/events/sync-store' ;
1313import { MarkerOverlayPoint } from '../overlay/MarkerOverlayPoint' ;
@@ -854,6 +854,85 @@ export class Editor<T = string> implements Printable {
854854 }
855855 }
856856
857+ public setStartMarker ( type : SliceType , data ?: unknown , slices : EditorSlices < T > = this . saved ) : MarkerSlice < T > {
858+ const after = this . txt . pointStart ( ) ?? this . txt . pointAbsStart ( ) ;
859+ after . refAfter ( ) ;
860+ if ( Array . isArray ( type ) && type . length === 1 ) type = type [ 0 ] ;
861+ return slices . slices . insMarkerAfter ( after . id , type , data ) ;
862+ }
863+
864+ public tglMarkerAt (
865+ point : Point < T > ,
866+ type : SliceType ,
867+ data ?: unknown ,
868+ slices : EditorSlices < T > = this . saved ,
869+ def : SliceTypeStep = SliceTypeCon . p ,
870+ ) : void {
871+ const overlay = this . txt . overlay ;
872+ const markerPoint = overlay . getOrNextLowerMarker ( point ) ;
873+ if ( markerPoint ) {
874+ const marker = markerPoint . marker ;
875+ const tag = marker . tag ( ) ;
876+ if ( ! Array . isArray ( type ) ) type = [ type ] ;
877+ const typeTag = type [ type . length - 1 ] ;
878+ if ( tag === typeTag ) type = [ ...type . slice ( 0 , - 1 ) , def ] ;
879+ if ( Array . isArray ( type ) && type . length === 1 ) type = type [ 0 ] ;
880+ marker . update ( { type} ) ;
881+ } else this . setStartMarker ( type , data , slices ) ;
882+ }
883+
884+ public updMarkerAt ( point : Point < T > , type : SliceType , data ?: unknown , slices : EditorSlices < T > = this . saved ) : void {
885+ const overlay = this . txt . overlay ;
886+ const markerPoint = overlay . getOrNextLowerMarker ( point ) ;
887+ if ( markerPoint ) {
888+ const marker = markerPoint . marker ;
889+ if ( Array . isArray ( type ) && type . length === 1 ) type = type [ 0 ] ;
890+ marker . update ( { type} ) ;
891+ } else this . setStartMarker ( type , data , slices ) ;
892+ }
893+
894+ /**
895+ * Toggle the type of a block split between the slice type and the default
896+ * (paragraph) block type.
897+ *
898+ * @param type Slice type to toggle.
899+ * @param data Custom data of the slice.
900+ */
901+ public tglMarker (
902+ type : SliceType ,
903+ data ?: unknown ,
904+ slices : EditorSlices < T > = this . saved ,
905+ def : SliceTypeStep = SliceTypeCon . p ,
906+ ) : void {
907+ for ( let i = this . cursors0 ( ) , cursor = i ( ) ; cursor ; cursor = i ( ) )
908+ this . tglMarkerAt ( cursor . start , type , data , slices , def ) ;
909+ }
910+
911+ /**
912+ * Update the type of a block split at all cursor positions.
913+ *
914+ * @param type Slice type to set.
915+ * @param data Custom data of the slice.
916+ * @param slices The slices set to use, if new marker is inserted at the start
917+ * of the document.
918+ */
919+ public updMarker ( type : SliceType , data ?: unknown , slices : EditorSlices < T > = this . saved ) : void {
920+ for ( let i = this . cursors0 ( ) , cursor = i ( ) ; cursor ; cursor = i ( ) )
921+ this . updMarkerAt ( cursor . start , type , data , slices ) ;
922+ }
923+
924+ public delMarker ( ) : void {
925+ const markerPoints = new Set < MarkerOverlayPoint < T > > ( ) ;
926+ for ( let i = this . cursors0 ( ) , cursor = i ( ) ; cursor ; cursor = i ( ) ) {
927+ const markerPoint = this . txt . overlay . getOrNextLowerMarker ( cursor . start ) ;
928+ if ( markerPoint ) markerPoints . add ( markerPoint ) ;
929+ }
930+ for ( const markerPoint of markerPoints ) {
931+ const boundary = markerPoint . marker . boundary ( ) ;
932+ this . delRange ( boundary ) ;
933+ }
934+ }
935+
857936 // ---------------------------------------------------------- export / import
858937
859938 public export ( range : Range < T > ) : ViewRange {
0 commit comments