<commit_before>"use strict"

/* global location, ace, io */

// Assumes ace is imported from script tag with noconflict version

// Assumes io is imported from socket.io

// Mithril only needs to be imported once in the application as it sets a global "m"
import "./vendor/mithril.js"

// sha256 only needs to be imported once in the application as it sets a global sha256
import "./vendor/sha256.js"

import { NotebookView } from "./NotebookView.js"
import { Stream } from "./Stream.js"
import { StreamBackendUsingLocalStorage } from "./StreamBackendUsingLocalStorage.js"
import { StreamBackendUsingServer } from "./StreamBackendUsingServer.js"
import { FileUtils } from "./FileUtils.js"
import { CanonicalJSON } from "./CanonicalJSON.js"

import { menu, popup } from "./popup.js"

let initialKeyToGoTo = null

let notebookId = "common"

const NotebookUsingMemory = Stream()
const NotebookUsingLocalStorage = Stream(StreamBackendUsingLocalStorage())
const NotebookUsingServer = Stream(StreamBackendUsingServer(m.redraw, notebookId))

const modelistWrapper = {
    modelist: null
}

let notebookView = NotebookView(NotebookUsingLocalStorage, ace, modelistWrapper)

// TODO: improve import for ace somehow via ES6 probably by getting a new version of ace
ace.require(["ace/ext/modelist"], function(modelist) {
    modelistWrapper.modelist = modelist
    m.redraw()
})

function getItemForJSON(itemJSON) {
    if (itemJSON === null) return null
    if (itemJSON.startsWith("{")) {
        try {
            return JSON.parse(itemJSON)
        } catch(e) {
            // fall through
        }
    }
    const newItem = notebookView.newItem()
    newItem.value = itemJSON
    return newItem
}

function setupTwirlip7Global(callback) {
    // setup Twirlip7 global for use by evaluated code
    if (window.Twirlip7) {
        alert("Unexpected: Twirlip7 global already exists!")
        callback()
        return
    }
    window.Twirlip7 = {
        show: notebookView.show,
        icon: notebookView.icon,
        popup,
        menu,

        notebookView,

        // TODO: Remove legacy support for two forms of "workspace view"
        workspaceView: notebookView,
        WorkspaceView: notebookView,

        FileUtils,
        CanonicalJSON,
        NotebookUsingLocalStorage,
        NotebookUsingMemory,
        NotebookUsingServer,

        // TODO: Remove legacy support for previous development notes
        getCurrentJournal: () => {
            return notebookView.getCurrentNotebook()
        },

        getCurrentNotebook: () => {
            return notebookView.getCurrentNotebook()
        },

        getItemForJSON: getItemForJSON,
        newItem: notebookView.newItem,

        saveItem: (item) => {<commit_msg>notebook: remove some legacy support<commit_after>"use strict"

/* global location, ace, io */

// Assumes ace is imported from script tag with noconflict version

// Assumes io is imported from socket.io

// Mithril only needs to be imported once in the application as it sets a global "m"
import "./vendor/mithril.js"

// sha256 only needs to be imported once in the application as it sets a global sha256
import "./vendor/sha256.js"

import { NotebookView } from "./NotebookView.js"
import { Stream } from "./Stream.js"
import { StreamBackendUsingLocalStorage } from "./StreamBackendUsingLocalStorage.js"
import { StreamBackendUsingServer } from "./StreamBackendUsingServer.js"
import { FileUtils } from "./FileUtils.js"
import { CanonicalJSON } from "./CanonicalJSON.js"

import { menu, popup } from "./popup.js"

let initialKeyToGoTo = null

let notebookId = "common"

const NotebookUsingMemory = Stream()
const NotebookUsingLocalStorage = Stream(StreamBackendUsingLocalStorage())
const NotebookUsingServer = Stream(StreamBackendUsingServer(m.redraw, notebookId))

const modelistWrapper = {
    modelist: null
}

let notebookView = NotebookView(NotebookUsingLocalStorage, ace, modelistWrapper)

// TODO: improve import for ace somehow via ES6 probably by getting a new version of ace
ace.require(["ace/ext/modelist"], function(modelist) {
    modelistWrapper.modelist = modelist
    m.redraw()
})

function getItemForJSON(itemJSON) {
    if (itemJSON === null) return null
    if (itemJSON.startsWith("{")) {
        try {
            return JSON.parse(itemJSON)
        } catch(e) {
            // fall through
        }
    }
    const newItem = notebookView.newItem()
    newItem.value = itemJSON
    return newItem
}

function setupTwirlip7Global(callback) {
    // setup Twirlip7 global for use by evaluated code
    if (window.Twirlip7) {
        alert("Unexpected: Twirlip7 global already exists!")
        callback()
        return
    }
    window.Twirlip7 = {
        show: notebookView.show,
        icon: notebookView.icon,
        popup,
        menu,

        notebookView,

        FileUtils,
        CanonicalJSON,
        NotebookUsingLocalStorage,
        NotebookUsingMemory,
        NotebookUsingServer,

        getCurrentNotebook: () => {
            return notebookView.getCurrentNotebook()
        },

        getItemForJSON: getItemForJSON,
        newItem: notebookView.newItem,

        saveItem: (item) => {
{'fraction_non_alphanumeric': 0.0923671497584541, 'fraction_numerical': 0.005797101449275362, 'mean_word_length': 3.034294621979735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '3665421', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1444, 'n_words': 421}