<commit_before>/**
 * A custom Quill highlighting module for markup modes/styles.  It manages
 * multiple mode instances that are used to apply text formatting to the
 * contents of the control.
 *
 * The module is added to the component per the API instructions:
 * https://quilljs.com/guides/building-a-custom-module/
 *
 * Once the module is initialized the highlighting mode can be changed with
 * the `set` functions.  This just changes what the reference internally to
 * another class that handles the formatting.
 *
 * See the README.md file for an example.
 *
 * Note that initial *content* cannot be set in the contructor to the module.
 * it is overwritten with the contents of the `#editor` div as Quill is
 * instantiated.  It can be set after creation of the Quill instance using
 * the `.setContent('')` function.
 *
 * @module Markup
 */

'use strict';

import {union} from 'lodash';
import {getFontList} from 'util.fontlist';
import {line as getLine, Section} from 'util.section';
import {Quill} from './helpers';
import {cssHighlights} from './highlights';
import {
	Asciidoc,
	BaseMarkupMode,
	Markdown,
	Text
} from './modes';

export type EventCallback = (val: any) => void;

const nilEvent: EventCallback = (val: any): void => {
	val = null;
};

export enum MarkupMode {
	asciidoc,
	markdown,
	text
}

export interface MarkupOptions {
	content?: string;
	custom?: any;
	dirtyLimit?: number;
	fontName?: string;
	fontSize?: number;
	followLinks?: boolean;
	highlight?: string;
	idleDelay?: number;
	mode?: MarkupMode;
	onChange?: EventCallback;
	onClick?: EventCallback;
	onClickLink?: EventCallback;
}

const defaultOptions: MarkupOptions = {
	content: '',
	custom: {},
	dirtyLimit: 20,
	fontName: 'Fira Code',
	fontSize: 12,
	followLinks: false,
	highlight: 'solarized-light',
	idleDelay: 2000,
	mode: MarkupMode.text,
	onChange: nilEvent,
	onClick: nilEvent,
	onClickLink: nilEvent
};

require('./styles.css');

const debug = require('debug')('markup');

export class Markup {

	// As the document is modified the number of characters that are changed
	// is counted.  When a "dirty" limit is reached and the user is idle then
	// a full rescan of the document block elements will occur.  The smaller
	// the limit, the sooner this will occur when idle.
	private _dirtyIdle: boolean = false;
	private _dirtyInline: boolean = false;
	private _dirtyCount: number = 10;
	private _dirtyLimit: number;

	// The time between each incremental section scan
	private _delay: number = 250;
	private _changing: boolean = false;

	// The time before idle detection
	private _idle: boolean = true;
	private _idleDelay: number = 2000;
	private _idleTimer: any;

	// A reference to the DOM editor node
	private _editor: HTMLElement;

	private _fonts: string[] = [
		'Fira Code',
		'Inconsolata',
		'Source Code Pro'
	];
	private _line: Section = {
		start: 0,
		end: 0,
		text: '',
		multiLine: false
	};
	private _modes: any = {};
	private _opts: MarkupOptions;
	private _paste: boolean = false;
	private _processor: BaseMarkupMode;
	private _quill: any;

	constructor(quill: any, opts: MarkupOptions = {}) {
		debug('Initializing markup module');

		this._quill = quill;
		this._opts = Object.assign({}, defaultOptions, opts);
		this._editor = quill.container;

		debug('quill (local): %o', quill);
		debug('Quill (global): %o', Quill);
		debug('Editor id: %s', this.editorKey);

		this._modes[MarkupMode.asciidoc] = new Asciidoc(quill);
		this._modes[MarkupMode.markdown] = new Markdown(quill);
		this._modes[MarkupMode.text] = new Text(quill);

		debug('modes: %O', this._modes);

		this._fonts = union(this._fonts, getFontList());
		debug('available fonts: %O', this.fonts);

		// bind all potential callbacks to the class.
		[
			'handleClick',
			'handleEditorChange',
			'handlePaste',
			'handleTextChange',
			'resetInactivityTimer',
			'markIdle',
			'redo',
			'set',
			'setBold',
			'setContent',
			'setItalic',
			'setFont',<commit_msg>Fixes a bug with the refresh()<commit_after>/**
 * A custom Quill highlighting module for markup modes/styles.  It manages
 * multiple mode instances that are used to apply text formatting to the
 * contents of the control.
 *
 * The module is added to the component per the API instructions:
 * https://quilljs.com/guides/building-a-custom-module/
 *
 * Once the module is initialized the highlighting mode can be changed with
 * the `set` functions.  This just changes what the reference internally to
 * another class that handles the formatting.
 *
 * See the README.md file for an example.
 *
 * Note that initial *content* cannot be set in the contructor to the module.
 * it is overwritten with the contents of the `#editor` div as Quill is
 * instantiated.  It can be set after creation of the Quill instance using
 * the `.setContent('')` function.
 *
 * @module Markup
 */

'use strict';

import {union} from 'lodash';
import {getFontList} from 'util.fontlist';
import {line as getLine, Section} from 'util.section';
import {Quill} from './helpers';
import {cssHighlights} from './highlights';
import {
	Asciidoc,
	BaseMarkupMode,
	Markdown,
	Text
} from './modes';

export type EventCallback = (val: any) => void;

const nilEvent: EventCallback = (val: any): void => {
	val = null;
};

export enum MarkupMode {
	asciidoc,
	markdown,
	text
}

export interface MarkupOptions {
	content?: string;
	custom?: any;
	dirtyLimit?: number;
	fontName?: string;
	fontSize?: number;
	followLinks?: boolean;
	highlight?: string;
	idleDelay?: number;
	mode?: MarkupMode;
	onChange?: EventCallback;
	onClick?: EventCallback;
	onClickLink?: EventCallback;
}

const defaultOptions: MarkupOptions = {
	content: '',
	custom: {},
	dirtyLimit: 20,
	fontName: 'Fira Code',
	fontSize: 12,
	followLinks: false,
	highlight: 'solarized-light',
	idleDelay: 2000,
	mode: MarkupMode.text,
	onChange: nilEvent,
	onClick: nilEvent,
	onClickLink: nilEvent
};

require('./styles.css');

const debug = require('debug')('markup');

export class Markup {

	// As the document is modified the number of characters that are changed
	// is counted.  When a "dirty" limit is reached and the user is idle then
	// a full rescan of the document block elements will occur.  The smaller
	// the limit, the sooner this will occur when idle.
	private _dirtyIdle: boolean = false;
	private _dirtyInline: boolean = false;
	private _dirtyCount: number = 10;
	private _dirtyLimit: number;

	// The time between each incremental section scan
	private _delay: number = 250;
	private _changing: boolean = false;

	// The time before idle detection
	private _idle: boolean = true;
	private _idleDelay: number = 2000;
	private _idleTimer: any;

	// A reference to the DOM editor node
	private _editor: HTMLElement;

	private _fonts: string[] = [
		'Fira Code',
		'Inconsolata',
		'Source Code Pro'
	];
	private _line: Section = {
		start: 0,
		end: 0,
		text: '',
		multiLine: false
	};
	private _modes: any = {};
	private _opts: MarkupOptions;
	private _paste: boolean = false;
	private _processor: BaseMarkupMode;
	private _quill: any;

	constructor(quill: any, opts: MarkupOptions = {}) {
		debug('Initializing markup module');

		this._quill = quill;
		this._opts = Object.assign({}, defaultOptions, opts);
		this._editor = quill.container;

		debug('quill (local): %o', quill);
		debug('Quill (global): %o', Quill);
		debug('Editor id: %s', this.editorKey);

		this._modes[MarkupMode.asciidoc] = new Asciidoc(quill);
		this._modes[MarkupMode.markdown] = new Markdown(quill);
		this._modes[MarkupMode.text] = new Text(quill);

		debug('modes: %O', this._modes);

		this._fonts = union(this._fonts, getFontList());
		debug('available fonts: %O', this.fonts);

		// bind all potential callbacks to the class.
		[
			'handleClick',
			'handleEditorChange',
			'handlePaste',
			'handleTextChange',
			'resetInactivityTimer',
			'markIdle',
			'redo',
			'refresh',
			'set',
			'setBold',
			'setContent',
			'setItalic',
			'setFont',
{'fraction_non_alphanumeric': 0.11832013076826355, 'fraction_numerical': 0.004778071168112662, 'mean_word_length': 4.164935064935065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4903952', 'n_tokens_mistral': 2713, 'n_tokens_neox': 2518, 'n_words': 873}