-
Notifications
You must be signed in to change notification settings - Fork 4
/
text-limiter.js
146 lines (119 loc) · 3.81 KB
/
text-limiter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
jQuery( function ( $ ) {
'use strict';
let Limiter = function ( $el ) {
this.$el = $el;
};
Limiter.prototype = {
// Initialize everything.
init: function () {
this.initElements();
this.addListeners();
this.$input.trigger( 'input' );
},
// Initialize elements.
initElements: function () {
this.$input = this.$el.siblings( '.rwmb-text' );
if ( !this.$input.length ) {
this.$input = this.$el.siblings( '.rwmb-textarea' );
}
this.isTinymce = false;
if ( !this.$input.length ) {
let tmce = this.$el.siblings( '.tmce-active' ).contents().filter( '.wp-editor-container' ).contents().filter( 'textarea' );
if ( tmce.length > 0 ) {
// wysiwyg in tmce mode
this.$tmceEditorId = tmce[ 0 ].id;
this.$input = $( '#' + this.$tmceEditorId );
this.isTinymce = true;
} else {
// wysiwyg in html mode
this.$input = this.$el.siblings( '.html-active' ).contents().filter( '.wp-editor-container' ).contents().filter( 'textarea' );
}
this.switchBtn = this.$el.siblings( '.wp-editor-wrap' ).contents().filter( '.wp-editor-tools' ).contents().filter( '.wp-editor-tabs' ).contents().filter( '.wp-switch-editor' );
}
this.$counter = this.$el.find( '.counter' );
this.type = this.$el.data( 'limit-type' );
this.max = parseInt( this.$el.find( '.maximum' ).text() );
},
// Add event listeners for 'input'.
addListeners: function () {
const that = this;
if ( !that.isTinymce ) {
this.$input.on( 'input', function () {
let value = this.value,
length = that.count( value, that.type );
if ( length > that.max && !that.isTinymce ) {
value = that.cut( that, value );
length = that.max;
this.value = value;
}
that.$counter.html( length );
} );
} else {
this.$input.on( 'input change', function () {
let tmceEditor = tinyMCE.get( that.$tmceEditorId );
let value = tmceEditor ? tmceEditor.getContent() : '';
let length = that.count( value, that.type );
if ( length > that.max ) {
value = that.cut( that, value );
length = that.max;
this.value = value;
tinyMCE.get( that.$tmceEditorId ).setContent( value, { format: 'html' } );
tinyMCE.activeEditor.selection.select( tinyMCE.activeEditor.getBody(), true );
// set cursor to end of value
tinyMCE.activeEditor.selection.collapse( false );
}
that.$counter.html( length );
} );
}
if ( that.switchBtn ) {
that.switchBtn.on( 'mouseup', function () {
setTimeout( () => {
that.initElements();
that.addListeners();
that.$input.trigger( 'input' );
}, 200 );
} );
}
},
// Cut the content to the max length.
cut: ( obj, content ) => {
let length = obj.count( content, obj.type );
while ( length > obj.max ) {
content = content.substring( 0, content.length - 1 );
length = obj.count( content, obj.type );
}
return content;
},
// Count the length of the content, without HTML.
count: function ( content, type ) {
// Remove HTML.
content = content.replace( /<[^>]+>/ig, '' );
if ( $.trim( content ) == '' ) {
return 0;
}
return 'word' === type ? content.match( /\S+/g ).length : content.length;
},
// Get subString for text by word or characters.
subStr: function ( val, start, len, type ) {
if ( 'word' !== type ) {
return val.substr( start, len );
}
var lastIndexSpace = val.lastIndexOf( ' ' );
return val.substr( start, lastIndexSpace );
}
};
function update() {
$( '.text-limiter' ).each( function () {
var $this = $( this ),
controller = $this.data( 'limiterController' );
if ( controller ) {
return;
}
controller = new Limiter( $this );
controller.init();
$this.data( 'limiterController', controller );
} );
}
update();
$( '.rwmb-input' ).on( 'clone', update );
} );