<commit_before>import * as objectHash from "object-hash/dist/object_hash"

/* Functions for ProseMirror integration.*/
import {ProseMirror} from "prosemirror/dist/edit/main"
import {fromDOM} from "prosemirror/dist/format"
import {serializeTo} from "prosemirror/dist/format"
import "prosemirror/dist/collab"
import {scheduleDOMUpdate} from "prosemirror/dist/ui/update"
//import "prosemirror/dist/menu/menubar"

import {fidusSchema} from "./schema"
import {ModComments} from "./comments/mod"
import {ModFootnotes} from "./footnotes/mod"
import {ModCitations} from "./citations/mod"
import {ModCollab} from "./collab/mod"
import {ModTools} from "./tools/mod"
import {ModSettings} from "./settings/mod"
import {ModMenus} from "./menus/mod"
import {ModServerCommunications} from "./server-communications"
import {ModNodeConvert} from "./node-convert"
import {node2Obj, obj2Node} from "../exporter/json"
import {BibliographyDB} from "../bibliography/database"
import {ImageDB} from "../images/database"

export class Editor {
    // A class that contains everything that happens on the editor page.
    // It is currently not possible to initialize more thna one editor class, as it
    // contains bindings to menu items, etc. that are uniquely defined.
    constructor() {
        this.mod = {}
        // Whether the editor is currently waiting for a document update. Set to true
        // initially so that diffs that arrive before document has been loaded are not
        // dealt with.
        this.waitingForDocument = true

        this.docInfo = {
            'sentHash': false,
            'rights': '',
            // In collaborative mode, only the first client to connect will have
            // this.editor.docInfo.control set to true.
            'control': false,
            'last_diffs': [],
            'is_owner': false,
            'is_new': false,
            'titleChanged': false,
            'changed': false
        }
        this.schema = fidusSchema
        this.doc = {}
        this.user = false
        new ModSettings(this)
        new ModNodeConvert(this)
        new ModServerCommunications(this)
        this.init()
    }

    init() {
        let that = this

        jQuery(document).ready(function() {
            that.startEditor()
        })
    }

    startEditor() {
        let that = this
        this.pm = this.makeEditor(document.getElementById('document-editable'))
        this.currentPm = this.pm // The editor that is currently being edited in -- main or footnote editor
        new ModFootnotes(this)
        new ModCitations(this)
        new ModMenus(this)
        new ModCollab(this)
        new ModTools(this)
        new ModComments(this)
        this.pm.on("change", function(){that.docInfo.changed = true})
        this.pm.on("filterTransform", (transform) => {return that.onFilterTransform(transform)})
        this.pm.on("transform", (transform, options) => {that.onTransform(transform, true)})
        this.pm.mod.collab.on("collabTransform", (transform, options) => {that.onTransform(transform, false)})
        this.setSaveTimers()
    }

    setSaveTimers() {
        let that = this
        // Set Auto-save to send the document every two minutes, if it has changed.
        this.sendDocumentTimer = setInterval(function() {
            if (that.docInfo && that.docInfo.changed) {
                that.getUpdates(function() {
                    that.sendDocumentUpdate()
                })
            }
        }, 120000)

        // Set Auto-save to send the title every 5 seconds, if it has changed.
        this.sendDocumentTitleTimer = setInterval(function() {
            if (that.docInfo && that.docInfo.titleChanged) {
                that.docInfo.titleChanged = false
                if (that.docInfo.control) {
                    that.mod.serverCommunications.send({
                        type: 'update_title',
                        title: that.doc.title
                    });
                }
            }
        }, 10000)
    }

    makeEditor(where) {
        let pm = new ProseMirror({<commit_msg>update title even if not main user<commit_after>import * as objectHash from "object-hash/dist/object_hash"

/* Functions for ProseMirror integration.*/
import {ProseMirror} from "prosemirror/dist/edit/main"
import {fromDOM} from "prosemirror/dist/format"
import {serializeTo} from "prosemirror/dist/format"
import "prosemirror/dist/collab"
import {scheduleDOMUpdate} from "prosemirror/dist/ui/update"
//import "prosemirror/dist/menu/menubar"

import {fidusSchema} from "./schema"
import {ModComments} from "./comments/mod"
import {ModFootnotes} from "./footnotes/mod"
import {ModCitations} from "./citations/mod"
import {ModCollab} from "./collab/mod"
import {ModTools} from "./tools/mod"
import {ModSettings} from "./settings/mod"
import {ModMenus} from "./menus/mod"
import {ModServerCommunications} from "./server-communications"
import {ModNodeConvert} from "./node-convert"
import {node2Obj, obj2Node} from "../exporter/json"
import {BibliographyDB} from "../bibliography/database"
import {ImageDB} from "../images/database"

export class Editor {
    // A class that contains everything that happens on the editor page.
    // It is currently not possible to initialize more thna one editor class, as it
    // contains bindings to menu items, etc. that are uniquely defined.
    constructor() {
        this.mod = {}
        // Whether the editor is currently waiting for a document update. Set to true
        // initially so that diffs that arrive before document has been loaded are not
        // dealt with.
        this.waitingForDocument = true

        this.docInfo = {
            'sentHash': false,
            'rights': '',
            // In collaborative mode, only the first client to connect will have
            // this.editor.docInfo.control set to true.
            'control': false,
            'last_diffs': [],
            'is_owner': false,
            'is_new': false,
            'titleChanged': false,
            'changed': false
        }
        this.schema = fidusSchema
        this.doc = {}
        this.user = false
        new ModSettings(this)
        new ModNodeConvert(this)
        new ModServerCommunications(this)
        this.init()
    }

    init() {
        let that = this

        jQuery(document).ready(function() {
            that.startEditor()
        })
    }

    startEditor() {
        let that = this
        this.pm = this.makeEditor(document.getElementById('document-editable'))
        this.currentPm = this.pm // The editor that is currently being edited in -- main or footnote editor
        new ModFootnotes(this)
        new ModCitations(this)
        new ModMenus(this)
        new ModCollab(this)
        new ModTools(this)
        new ModComments(this)
        this.pm.on("change", function(){that.docInfo.changed = true})
        this.pm.on("filterTransform", (transform) => {return that.onFilterTransform(transform)})
        this.pm.on("transform", (transform, options) => {that.onTransform(transform, true)})
        this.pm.mod.collab.on("collabTransform", (transform, options) => {that.onTransform(transform, false)})
        this.setSaveTimers()
    }

    setSaveTimers() {
        let that = this
        // Set Auto-save to send the document every two minutes, if it has changed.
        this.sendDocumentTimer = setInterval(function() {
            if (that.docInfo && that.docInfo.changed) {
                that.getUpdates(function() {
                    that.sendDocumentUpdate()
                })
            }
        }, 120000)

        // Set Auto-save to send the title every 5 seconds, if it has changed.
        this.sendDocumentTitleTimer = setInterval(function() {
            if (that.docInfo && that.docInfo.titleChanged) {
                that.docInfo.titleChanged = false
                that.mod.serverCommunications.send({
                    type: 'update_title',
                    title: that.doc.title
                })
            }
        }, 10000)
    }

    makeEditor(where) {
        let pm = new ProseMirror({
{'fraction_non_alphanumeric': 0.1111248149975333, 'fraction_numerical': 0.003453379378391712, 'mean_word_length': 2.2737182075090834, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '6638927', 'n_tokens_mistral': 2274, 'n_tokens_neox': 2175, 'n_words': 619}