Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // fg and bg are HTML colors, bg can (and usually will) be null
- // fg can be 'reset', which resets the colors back to default
- function linechunk_color(fg, bg) {
- var chunk = linechunk_base();
- chunk._fg = fg;
- chunk._bg = bg;
- chunk.type = function() { return 'color'; }
- chunk.formatted = function(chunks) {
- if (this._fg === 'reset') {
- var res = chunks.suffix;
- chunks.suffix = '';
- return res;
- }
- var style = '';
- if (this._fg != null) style += 'color: ' + this._fg + '; ';
- if (this._bg != null) style += 'background-color: ' + this._bg + ' ';
- if (style == '') return '';
- var res = '';
- // close previous color tags
- // IMPORTANT: if we use span suffix tags for anything else, this will fail!
- // Disabling this, it's causing weird problems
- // while (chunks.suffix.substr(0, 7) === '</span>') {
- // res += '</span>';
- // chunks.suffix = chunks.suffix.substr(7);
- // }
- chunks.suffix += '</span>';
- return res + '<span style="' + style + '">';
- }
- chunk.update_state = function(state) {
- state.fg = this._fg;
- if (this._fg == 'reset')
- delete state.bg;
- else
- state.bg = this._bg;
- return state;
- }
- return chunk;
- }
- function linechunk_mxp_send(color, commands, text) {
- if (client.reverted) {
- if (color == '#FFFFFF') color = '#000000';
- if (color == '#C0C0C0') color = '#333333';
- }
- var chunk = linechunk_text(text, false);
- chunk._color = color;
- chunk._commands = commands;
- chunk.type = function() { return 'link'; }
- chunk.formatted = function(chunks) {
- var res = '<a class="mxp_send" href="' + this._commands + '"';
- if (this._color) res += ' style="color: ' + this._color + '"';
- res += '>' + this._txt + '</a>';
- return res;
- }
- return chunk;
- }
- function linechunks_create(chunks) {
- var res = {};
- res.chunks = chunks;
- // helper function for colorize / replace. Ensures that there exists no chunk that spans this position.
- // Chunks starting -at- this position are fine. Chunks -ending- at this position are not (last letter will be split off).
- res.split_chunk_at_pos = function(pos)
- {
- if (pos <= 0) return; // nothing to do
- var at = 0;
- for (var idx = 0; idx < this.chunks.length; ++idx) {
- // if we got exactly to the desired position, there is no need to split anything; bail out
- if (at == pos) break;
- var len = this.chunks[idx].length();
- if (at + len <= pos) {
- at += len;
- continue;
- }
- // If we got here, at < pos and at + len > pos, meaning we need to split
- var splitlen = pos - at;
- var chunk2 = $.extend(true, {}, this.chunks[idx]); // create a deep copy of the chunk
- this.chunks[idx].resize(0, splitlen);
- chunk2.resize(splitlen, len);
- this.chunks.splice(idx + 1, 0, chunk2); // insert chunk2 after the current chunk
- break;
- }
- }
- res.colorize = function(start, end, color, bgcolor) {
- if ((this.chunks == null) || (this.chunks.length == 0))
- return;
- if (end < start) end = start;
- // ensure that no chunk spans the thresholds (simplifies the logic)
- this.split_chunk_at_pos(start);
- if (end != start) this.split_chunk_at_pos(end);
- // grab all the chunks between start and end, except colorization ones
- var chunks = [];
- chunks.push(linechunk_color(color, bgcolor));
- var pos = 0;
- var copying = false;
- for (var idx = 0; idx < this.chunks.length; ++idx) {
- if (pos >= end) break;
- if (pos >= start) copying = true;
- if (copying && this.chunks[idx].type() != 'color') chunks.push(this.chunks[idx]);
- var len = this.chunks[idx].length();
- pos += len;
- }
- return this.replace_with_linechunks(start, end, chunks);
- }
- res.replace = function(start, end, replacement, color, bgcolor) {
- var chunks = [];
- if ((replacement != null) && replacement.length) {
- if ((color != undefined) || (bgcolor != undefined))
- chunks.push(linechunk_color(color, bgcolor));
- chunks.push(linechunk_text(replacement, true));
- }
- return this.replace_with_linechunks(start, end, chunks);
- }
- res.linkify = function(start, end, color, link_command, link_text) {
- if (!link_command) return;
- if (!link_text)
- link_text = this.text().substr(start, end - start + 1);
- var chunks = [];
- chunks.push(linechunk_mxp_send(color, link_command, link_text));
- return this.replace_with_linechunks(start, end, chunks);
- }
- // start = first index to replace, end = first index to -not- replace (i.e. end-start = length to replace)
- res.replace_with_linechunks = function(start, end, linechunks)
- {
- if ((this.chunks == null) || (this.chunks.length == 0)) {
- this.chunks = linechunks;
- return;
- }
- if (end < start) end = start;
- // ensure that no chunk spans the thresholds (simplifies the logic)
- this.split_chunk_at_pos(start);
- if (end != start) this.split_chunk_at_pos(end);
- // remove old chunks, remember state at the end (so that colors match)
- var pos = 0;
- var removeidx = -1;
- var removecount = 0;
- var state = {};
- var removing = false;
- for (var idx = 0; idx < this.chunks.length; ++idx) {
- if (pos >= end) break;
- if ((pos >= start) && (removeidx < 0)) removeidx = idx;
- if (removeidx >= 0) removecount += 1;
- var len = this.chunks[idx].length();
- pos += len;
- state = this.chunks[idx].update_state(state);
- }
- if (removeidx < 0) removeidx = 0;
- linechunks = linechunks || []; // linechunks must exist, as we'll be adding the new state
- var state_chunks = client.apply_line_state(state);
- linechunks.push(linechunk_color('reset', null)); // reset the color -- fixes background color leakage
- for (var s = 0; s < state_chunks.length; ++s)
- linechunks.push(state_chunks[s]);
- // this calls splice with (removeidx, removecount, (contents of linechunks)) as params
- Array.prototype.splice.apply(this.chunks, [removeidx, removecount].concat(linechunks));
- }
- res.remove = function(start, end) {
- return this.replace_with_linechunks(startd, end, null);
- }
- res.formatted = function() {
- var res = '';
- this.prefix = '';
- this.suffix = '';
- for (var i = 0; i < this.chunks.length; ++i)
- res += this.chunks[i].formatted(this);
- return this.prefix + res + this.suffix;
- }
- res.text = function() {
- var res = '';
- for (var i = 0; i < this.chunks.length; ++i)
- res += this.chunks[i].text(this);
- return res;
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement