Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/ve.ui.SequenceRegistry.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface SequenceRegistry class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Sequence registry.
9 *
10 * @class
11 * @extends OO.Registry
12 * @constructor
13 */
14 ve.ui.SequenceRegistry = function VeUiSequenceRegistry() {
15 // Parent constructor
16 ve.ui.SequenceRegistry.super.call( this );
17 };
18
19 /* Inheritance */
20
21 OO.inheritClass( ve.ui.SequenceRegistry, OO.Registry );
22
23 /**
24 * Register a sequence with the factory.
25 *
26 * @param {ve.ui.Sequence} sequence
27 * @throws {Error} If sequence is not an instance of ve.ui.Sequence
28 */
29 ve.ui.SequenceRegistry.prototype.register = function ( sequence ) {
30 // Validate arguments
31 if ( !( sequence instanceof ve.ui.Sequence ) ) {
32 throw new Error(
33 'sequence must be an instance of ve.ui.Sequence, cannot be a ' + typeof sequence
34 );
35 }
36
37 ve.ui.SequenceRegistry.super.prototype.register.call( this, sequence.getName(), sequence );
38 };
39
40 /**
41 * Matching sequence and corresponding range
42 *
43 * @typedef {Object} Match
44 * @memberof ve.ui.SequenceRegistry
45 * @property {ve.ui.Sequence} sequence
46 * @property {ve.Range} range
47 */
48
49 /**
50 * Find sequence matches a given offset in the data
51 *
52 * @param {ve.dm.ElementLinearData} data
53 * @param {number} offset
54 * @param {boolean} [isPaste] Whether this in the context of a paste
55 * @param {boolean} [isDelete] Whether this is after content being deleted
56 * @return {ve.ui.SequenceRegistry.Match[]} Array of matching sequences, and the corresponding range of the match for each.
57 */
58 ve.ui.SequenceRegistry.prototype.findMatching = function ( data, offset, isPaste, isDelete ) {
59 // To avoid blowup when matching RegExp sequences, we're going to grab
60 // all the plaintext to the left (until the nearest node) *once* and pass
61 // it to each sequence matcher. We're also going to hard-limit that
62 // plaintext to 256 characters to ensure we don't run into O(N^2)
63 // slowdown when inserting N characters of plain text.
64
65 // First skip over open elements, then close elements, to ensure that
66 // pressing enter after a (possibly nested) list item or inside a
67 // paragraph works properly. Typing "foo\n" inside a paragraph creates
68 // "foo</p><p>" in the content model, and typing "foo\n" inside a list
69 // creates "foo</p></li><li><p>" -- we want to give the matcher a
70 // chance to match "foo\n+" in these cases.
71 let textStart;
72 let state = 0;
73 for ( textStart = offset - 1; textStart >= 0 && ( offset - textStart ) <= 256; textStart-- ) {
74 if ( state === 0 && !data.isOpenElementData( textStart ) ) {
75 state++;
76 }
77 if ( state === 1 && !data.isCloseElementData( textStart ) ) {
78 state++;
79 }
80 if ( state === 2 && data.isElementData( textStart ) ) {
81 break;
82 }
83 }
84 const sequences = [];
85 const plaintext = data.getText( true, new ve.Range( textStart + 1, offset ) );
86 // Now search through the registry.
87 for ( const name in this.registry ) {
88 const sequence = this.registry[ name ];
89 if ( isPaste && !sequence.checkOnPaste ) {
90 continue;
91 }
92 if ( isDelete && !sequence.checkOnDelete ) {
93 continue;
94 }
95 const range = sequence.match( data, offset, plaintext );
96 if ( range !== null ) {
97 sequences.push( {
98 sequence: sequence,
99 range: range
100 } );
101 }
102 }
103 return sequences;
104 };
105
106 /* Initialization */
107
108 ve.ui.sequenceRegistry = new ve.ui.SequenceRegistry();
109
110 /* Registrations */
111
112 ve.ui.sequenceRegistry.register(
113 new ve.ui.Sequence( 'bulletStar', 'bulletWrapOnce', [ { type: 'paragraph' }, '*', ' ' ], 2 )
114 );
115 ve.ui.sequenceRegistry.register(
116 new ve.ui.Sequence( 'numberDot', 'numberWrapOnce', [ { type: 'paragraph' }, '1', '.', ' ' ], 3 )
117 );
118 ve.ui.sequenceRegistry.register(
119 new ve.ui.Sequence( 'horizontalRule', 'insertHorizontalRule', [ { type: 'paragraph' }, '-', '-', '-', '-' ], 4 )
120 );