1
1
import { Range } from 'monaco-editor' ;
2
+ import { __ } from '~/locale' ;
2
3
import {
3
4
EDITOR_TYPE_CODE ,
4
5
EXTENSION_BASE_LINE_LINK_ANCHOR_CLASS ,
5
6
EXTENSION_BASE_LINE_NUMBERS_CLASS ,
7
+ EDITOR_TOOLBAR_RIGHT_GROUP ,
8
+ EXTENSION_SOFTWRAP_ID ,
6
9
} from '../constants' ;
7
10
8
11
const hashRegexp = / # ? L / g;
@@ -24,6 +27,13 @@ export class SourceEditorExtension {
24
27
return 'BaseExtension' ;
25
28
}
26
29
30
+ onSetup ( instance ) {
31
+ this . toolbarButtons = [ ] ;
32
+ if ( instance . toolbar ) {
33
+ this . setupToolbar ( instance ) ;
34
+ }
35
+ }
36
+
27
37
// eslint-disable-next-line class-methods-use-this
28
38
onUse ( instance ) {
29
39
SourceEditorExtension . highlightLines ( instance ) ;
@@ -32,6 +42,31 @@ export class SourceEditorExtension {
32
42
}
33
43
}
34
44
45
+ onBeforeUnuse ( instance ) {
46
+ const ids = this . toolbarButtons . map ( ( item ) => item . id ) ;
47
+ if ( instance . toolbar ) {
48
+ instance . toolbar . removeItems ( ids ) ;
49
+ }
50
+ }
51
+
52
+ setupToolbar ( instance ) {
53
+ this . toolbarButtons = [
54
+ {
55
+ id : EXTENSION_SOFTWRAP_ID ,
56
+ label : __ ( 'Soft wrap' ) ,
57
+ icon : 'soft-wrap' ,
58
+ selected : instance . getOption ( 116 ) === 'on' ,
59
+ group : EDITOR_TOOLBAR_RIGHT_GROUP ,
60
+ category : 'primary' ,
61
+ selectedLabel : __ ( 'No wrap' ) ,
62
+ selectedIcon : 'soft-unwrap' ,
63
+ class : 'soft-wrap-toggle' ,
64
+ onClick : ( ) => instance . toggleSoftwrap ( ) ,
65
+ } ,
66
+ ] ;
67
+ instance . toolbar . addItems ( this . toolbarButtons ) ;
68
+ }
69
+
35
70
static onMouseMoveHandler ( e ) {
36
71
const target = e . target . element ;
37
72
if ( target . classList . contains ( EXTENSION_BASE_LINE_NUMBERS_CLASS ) ) {
@@ -108,6 +143,16 @@ export class SourceEditorExtension {
108
143
highlightLines ( instance , bounds = null ) {
109
144
SourceEditorExtension . highlightLines ( instance , bounds ) ;
110
145
} ,
146
+
147
+ toggleSoftwrap ( instance ) {
148
+ const isSoftWrapped = instance . getOption ( 116 ) === 'on' ;
149
+ instance . updateOptions ( { wordWrap : isSoftWrapped ? 'off' : 'on' } ) ;
150
+ if ( instance . toolbar ) {
151
+ instance . toolbar . updateItem ( EXTENSION_SOFTWRAP_ID , {
152
+ selected : ! isSoftWrapped ,
153
+ } ) ;
154
+ }
155
+ } ,
111
156
} ;
112
157
}
113
158
}
0 commit comments