Skip to content

Commit

Permalink
Add Simple Plugin System
Browse files Browse the repository at this point in the history
  • Loading branch information
taufik-nurrohman committed Dec 26, 2023
1 parent 45f8a6f commit 9dba972
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 40 deletions.
6 changes: 5 additions & 1 deletion .github/factory/index.html.pug
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ block content
pre: code let editor = new TE(#[var self], #[var tab] = '\t');
pre: code
| let editor = new TE(#[var self], #[var state] = {
| tab: '\t'
| tab: '\t',
| with: []
| });
table(border='1')
thead
Expand All @@ -169,6 +170,9 @@ block content
tr
td: code state.tab
td The default indent character for #[code editor.pull()] and #[code editor.push()] method.
tr
td: code state.with
td List of callable functions to be invoked each time the application is initialized. A very simple “plugin” system.
h2 Methods and Properties
h3 TE.esc(string)
p Escape regular expression’s special characters.
Expand Down
9 changes: 8 additions & 1 deletion .github/factory/index.js.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ function TE(source, state = {}) {
// Return the text editor state
$.state = state;

if (isArray(state.with)) {
for (let i = 0, j = toCount(state.with); i < j; ++i) {
isFunction(state.with[i]) && state.with[i].call($, source, state);
}
}

return $;

}
Expand All @@ -254,7 +260,8 @@ TE.esc = esc;
TE.instances = {};

TE.state = {
'tab': '\t'
'tab': '\t',
'with': []
};

TE.S = function (a, b, c) {
Expand Down
11 changes: 8 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta content="#000" name="theme-color">
<meta content="width=device-width" name="viewport">
<meta content="A text selection range API written in pure JavaScript, for modern browsers." name="description">
<title>Text Editor 3.3.14</title>
<title>Text Editor 3.4.0</title>
<style>
* {
box-sizing: border-box;
Expand Down Expand Up @@ -69,7 +69,7 @@
<body>
<p role="alert">Do you like this project? Please support my <a href="https://github.com/mecha-cms">Mecha CMS</a> project too. Thank you!</p>
<header>
<h1>Text Editor 3.3.14</h1>
<h1>Text Editor 3.4.0</h1>
<p>Text Editor is a simple JavaScript application that aims to provide accessibility improvements to the native HTML <code>&lt;textarea&gt;</code> elements so that users can control and manipulate their data in the text editor as they wish.</p>
<hr>
</header>
Expand Down Expand Up @@ -139,7 +139,8 @@ <h2>Examples</h2>
<h2>Settings</h2>
<pre><code>let editor = new TE(<var>self</var>, <var>tab</var> = '\t');</code></pre>
<pre><code>let editor = new TE(<var>self</var>, <var>state</var> = {
tab: '\t'
tab: '\t',
with: []
});</code></pre>
<table border="1">
<thead>
Expand All @@ -165,6 +166,10 @@ <h2>Settings</h2>
<td><code>state.tab</code></td>
<td>The default indent character for <code>editor.pull()</code> and <code>editor.push()</code> method.</td>
</tr>
<tr>
<td><code>state.with</code></td>
<td>List of callable functions to be invoked each time the application is initialized. A very simple “plugin” system.</td>
</tr>
</tbody>
</table>
<h2>Methods and Properties</h2>
Expand Down
87 changes: 57 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,20 @@
if (!isSet(out[k])) {
out[k] = lot[i][k];
continue;
} // Merge array
}
// Merge array
if (isArray(out[k]) && isArray(lot[i][k])) {
out[k] = [
/* Clone! */
].concat(out[k]);
out[k] = [ /* Clone! */ ].concat(out[k]);
for (var ii = 0, jj = toCount(lot[i][k]); ii < jj; ++ii) {
if (!hasValue(lot[i][k][ii], out[k])) {
out[k].push(lot[i][k][ii]);
}
} // Merge object recursive
}
// Merge object recursive
} else if (isObject(out[k]) && isObject(lot[i][k])) {
out[k] = fromStates({
/* Clone! */
}, out[k], lot[i][k]); // Replace value
/* Clone! */ }, out[k], lot[i][k]);
// Replace value
} else {
out[k] = lot[i][k];
}
Expand All @@ -117,7 +117,8 @@
var toPattern = function toPattern(pattern, opt) {
if (isPattern(pattern)) {
return pattern;
} // No need to escape `/` in the pattern string
}
// No need to escape `/` in the pattern string
pattern = pattern.replace(/\//g, '\\/');
return new RegExp(pattern, isSet(opt) ? opt : 'g');
};
Expand All @@ -135,18 +136,23 @@
var $ = this;
if (!source) {
return $;
} // Already instantiated, skip!
}
// Already instantiated, skip!
if (source[name]) {
return source[name];
} // Return new instance if `TE` was called without the `new` operator
}
// Return new instance if `TE` was called without the `new` operator
if (!isInstance($, TE)) {
return new TE(source, state);
}
$.state = state = fromStates({}, TE.state, isString(state) ? {
tab: state
} : state || {}); // The `<textarea>` element
$.self = $.source = source; // Store current instance to `TE.instances`
TE.instances[source.id || source.name || toObjectCount(TE.instances)] = $; // Mark current DOM as active text editor to prevent duplicate instance
} : state || {});
// The `<textarea>` element
$.self = $.source = source;
// Store current instance to `TE.instances`
TE.instances[source.id || source.name || toObjectCount(TE.instances)] = $;
// Mark current DOM as active text editor to prevent duplicate instance
source[name] = $;
var any = /^([\s\S]*?)$/,
// Any character(s)
Expand All @@ -158,20 +164,25 @@
},
sourceValue = function sourceValue() {
return source.value.replace(/\r/g, "");
}; // The initial value
$.value = sourceValue(); // Get value
};
// The initial value
$.value = sourceValue();
// Get value
$.get = function () {
return !sourceIsDisabled() && trim(sourceValue()) || null;
}; // Reset to the initial value
};
// Reset to the initial value
$.let = function () {
return source.value = $.value, $;
}; // Set value
};
// Set value
$.set = function (value) {
if (sourceIsDisabled() || sourceIsReadOnly()) {
return $;
}
return source.value = value, $;
}; // Get selection
};
// Get selection
$.$ = function () {
return new TE.S(source.selectionStart, source.selectionEnd, sourceValue());
};
Expand All @@ -188,10 +199,12 @@
source.scrollTop = y;
}
return source.focus(), $;
}; // Blur from the editor
};
// Blur from the editor
$.blur = function () {
return source.blur(), $;
}; // Select value
};
// Select value
$.select = function () {
if (sourceIsDisabled() || sourceIsReadOnly()) {
return source.focus(), $;
Expand Down Expand Up @@ -223,13 +236,15 @@
}
lot[1] = lot[0];
}
source.focus(); // Default `$.select(7, 100)`
source.focus();
// Default `$.select(7, 100)`
source.selectionStart = lot[0];
source.selectionEnd = lot[1];
source.scrollLeft = X;
source.scrollTop = Y;
return W.scroll(x, y), $;
}; // Match at selection
};
// Match at selection
$.match = function (pattern, then) {
var _$$$2 = $.$(),
after = _$$$2.after,
Expand All @@ -241,7 +256,8 @@
}
var m = value.match(pattern);
return isFunction(then) ? then.call($, m || []) : !!m;
}; // Replace at selection
};
// Replace at selection
$.replace = function (from, to, mode) {
var _$$$3 = $.$(),
after = _$$$3.after,
Expand All @@ -258,7 +274,8 @@
value = value.replace(from, to);
}
return $.set(before + value + after).select(before = toCount(before), before + toCount(value));
}; // Insert/replace at caret
};
// Insert/replace at caret
$.insert = function (value, mode, clear) {
var from = any;
if (clear) {
Expand All @@ -272,7 +289,8 @@
from = /^/;
}
return $.replace(from, value, mode);
}; // Wrap current selection
};
// Wrap current selection
$.wrap = function (open, close, wrap) {
var _$$$4 = $.$(),
after = _$$$4.after,
Expand All @@ -282,7 +300,8 @@
return $.replace(any, open + '$1' + close);
}
return $.set(before + open + value + close + after).select(before = toCount(before + open), before + toCount(value));
}; // Unwrap current selection
};
// Unwrap current selection
$.peel = function (open, close, wrap) {
var _$$$5 = $.$(),
after = _$$$5.after,
Expand Down Expand Up @@ -362,20 +381,28 @@
if (false !== start) value = trim(value, -1);
if (false !== end) value = trim(value, 1);
return $.set(before + value + after).select(before = toCount(before), before + toCount(value));
}; // Destructor
};
// Destructor
$.pop = function () {
if (!source[name]) {
return $; // Already ejected!
}
return delete source[name], $;
}; // Return the text editor state
};
// Return the text editor state
$.state = state;
if (isArray(state.with)) {
for (var i = 0, j = toCount(state.with); i < j; ++i) {
isFunction(state.with[i]) && state.with[i].call($, source, state);
}
}
return $;
}
TE.esc = esc;
TE.instances = {};
TE.state = {
'tab': '\t'
'tab': '\t',
'with': []
};
TE.S = function (a, b, c) {
var t = this,
Expand All @@ -390,7 +417,7 @@
return d;
};
};
TE.version = '3.3.14';
TE.version = '3.4.0';
TE.x = x;
return TE;
}));
2 changes: 1 addition & 1 deletion index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9dba972

Please sign in to comment.