<commit_before>/**
 * contextmenu.
 * User: raytin
 * Date: 13-7-25
 */
var require = global.require,
    window = global.window,
    console = window.console,
    fs = require('fs'),
    iconv = require('iconv-lite');

var source = {};

// 初始化右键菜单
var contextMenuInit = function(){
    var markdown = global.markdown,
        cache = markdown.cache,
        editor = cache.editor;

    var gui = cache.gui,
        clipboard = gui.Clipboard.get();

    function menuMachine(name, option){
        return (source[name] = new gui.MenuItem(option));
    }

    function showToolOrConsole(that, type){
        var footer = $('#J-footer'),
            toolbar = $('#J-toolbar'),
            main = $('#J-tab, #codeMirror, #showdown'),
            fireOnloadLocalData = cache.tempFlag === 'localData';

        // 这一次是 加载本地数据时进来的 取反
        if(fireOnloadLocalData){
            that.checked = !that.checked;
        }

        if(type === 'toolbar'){
            if(that.checked){
                toolbar.removeClass('hide');
                main.css('top', '+=30px');
            }else{
                toolbar.addClass('hide');
                main.css('top', '-=30px');
            }
        }
        else if(type === 'console'){
            if(that.checked){
                footer.removeClass('hide');
                main.css('bottom', '25px');
            }else{
                footer.addClass('hide');
                main.css('bottom', '0px');
            }
        }

        markdown.adjustWorkspace();

        // 保存用户选择的工具栏和状态栏显隐值
        markdown.userFootprint.saveToMemory(type, that.checked);

        if(fireOnloadLocalData){
            delete cache.tempFlag;
        }
    }

    // 工具栏 menu
    var toolbarMenu = new gui.Menu();
    toolbarMenu.append(menuMachine('toolbar', {
        label: '显示工具栏', //提示通过‘查看’可再次打开
        type: 'checkbox',
        checked: true,
        click: function(){
            showToolOrConsole(this, 'toolbar');
        }
    }));

    // 状态栏 menu
    var consoleMenu = new gui.Menu();
    consoleMenu.append(menuMachine('console', {
        label: '显示状态栏', //提示通过‘查看’可再次打开
        type: 'checkbox',
        checked: true,
        click: function(){
            showToolOrConsole(this, 'console');
        }
    }));

    // 文件 menu
    var fileMenu = new gui.Menu();
    fileMenu.append(menuMachine('file-newDoc', {
        label: '新建文档 (Ctrl+N)',
        click: function(){
            $('#J-tool-new').trigger('click');
        }
    }));
    fileMenu.append(menuMachine('file-saveDoc', {
        label: '保存文档 (Ctrl+S)',
        click: function(){
            $('#J-tool-save').trigger('click');
        }
    }));
    fileMenu.append(menuMachine('file-openDoc', {
        label: '打开文件... (Ctrl+O)',
        click: function(){
            $('#J-tool-select').trigger('click');
        }
    }));
    fileMenu.append(menuMachine('file-saveAsDoc', {
        label: '另存为... (Ctrl+Shift+S)',
        click: function(){
            markdown.file.saveAs();
        }
    }));
    fileMenu.append(menuMachine('file-docDir', {
        label: '查看文件目录',
        click: function(){
            markdown.file.viewPath();
        }
    }));
    fileMenu.append(menuMachine('file-docDir', {
        label: '从磁盘删除',
        click: function(){
            markdown.file.delFileFromDisk();
        }
    }));
    fileMenu.append(menuMachine('separator', {
        type: 'separator'
    }));
    fileMenu.append(menuMachine('file-saveAsDoc', {
        label: '关闭 (Alt+C)',
        click: function(){
            markdown.tab.close(markdown.tab.getCurrentTab().id);
        }
    }));
    fileMenu.append(menuMachine('file-saveAsDoc', {
        label: '全部关闭',
        click: function(){
            markdown.tab.closeAll();
        }
    }));
    fileMenu.append(menuMachine('file-saveAsDoc', {
        label: '关闭当前以外所有标签',
        click: function(){
            markdown.tab.closeOthers();
        }
    }));
    fileMenu.append(source.separator);
    fileMenu.append(menuMachine('file-exit', {
        label: '打印 (Ctrl+P)',
        enabled: false,
        click: function(){
            window.print();
        }
    }));
    fileMenu.append(source.separator);
    fileMenu.append(menuMachine('file-exit', {
        label: '退出 (Exit)',
        click: function(){
            cache.guiWin.close();
        }
    }));

    menuMachine('file-clearHistory', {
        label: '清除所有历史记录',
        click: function(){
            markdown.userFootprint.clearHistoryInMenu();
            markdown.userFootprint.clearInMemory('historyList');
        }
    });
    source.file = fileMenu;


    // 编辑 menu
    var editMenu = new gui.Menu();
    editMenu.append(menuMachine('edit-undo', {
        label: '撤消 (Ctrl+Z)',
        click: function(){
            $('#J-tool-undo').trigger('click');
        }
    }));
    editMenu.append(menuMachine('edit-redo', {
        label: '重做 (Ctrl+Y)',
        click: function(){
            $('#J-tool-redo').trigger('click');
        }
    }));
    editMenu.append(source.separator);
    editMenu.append(menuMachine('copy', {
        label: '复制 (Ctrl+C)',
        click: function(){
            clipboard.set(editor.getSelection());
        }
    }));
    editMenu.append(menuMachine('cut', {
        label: '剪切 (Ctrl+X)',
        click: function(){
            clipboard.set(editor.getSelection());
            editor.replaceSelection('');<commit_msg>Improve context menu implementation for Windows<commit_after>/**
 * contextmenu.
 * User: raytin
 * Date: 13-7-25
 */
var require = global.require,
    window = global.window,
    console = window.console,
    fs = require('fs'),
    iconv = require('iconv-lite');

var source = {};

// 初始化右键菜单
var contextMenuInit = function(){
    var markdown = global.markdown,
        cache = markdown.cache,
        editor = cache.editor;

    var gui = cache.gui,
        clipboard = gui.Clipboard.get();

    function menuMachine(name, option){
        return (source[name] = new gui.MenuItem(option));
    }

    var separatorFactory = function() {
        var menuItem = new gui.MenuItem({
            type: 'separator'
        });
        return menuItem;
    }

    var clearHistoryMenuFactory = function() {
        var menuItem = new gui.MenuItem({
            label: '清除所有历史记录',
            click: function(){
                markdown.userFootprint.clearHistoryInMenu();
                markdown.userFootprint.clearInMemory('historyList');
            }
        });
        return menuItem;
    }

    var toolbarMenuFactory = function() {
        var menuItem = new gui.MenuItem({
            label: '显示工具栏', //提示通过‘查看’可再次打开
            type: 'checkbox',
            checked: true,
            click: function(){
                showToolOrConsole(this, 'toolbar');
            }
        });
        return menuItem;
    }

    var consoleMenuFactory = function() {
        var menuItem = new gui.MenuItem({
            label: '显示状态栏', //提示通过‘查看’可再次打开
            type: 'checkbox',
            checked: true,
            click: function(){
                showToolOrConsole(this, 'console');
            }
        });
        return menuItem;
    }

    source.separatorFactory = separatorFactory;
    source.clearHistoryMenuFactory = clearHistoryMenuFactory;
    source.toolbarMenuFactory = toolbarMenuFactory;
    source.consoleMenuFactory = consoleMenuFactory;

    function showToolOrConsole(that, type){
        var footer = $('#J-footer'),
            toolbar = $('#J-toolbar'),
            main = $('#J-tab, #codeMirror, #showdown'),
            fireOnloadLocalData = cache.tempFlag === 'localData';

        // 这一次是 加载本地数据时进来的 取反
        if(fireOnloadLocalData){
            that.checked = !that.checked;
        }

        if(type === 'toolbar'){
            if(that.checked){
                toolbar.removeClass('hide');
                main.css('top', '+=30px');
            }else{
                toolbar.addClass('hide');
                main.css('top', '-=30px');
            }
        }
        else if(type === 'console'){
            if(that.checked){
                footer.removeClass('hide');
                main.css('bottom', '25px');
            }else{
                footer.addClass('hide');
                main.css('bottom', '0px');
            }
        }

        markdown.adjustWorkspace();

        // 保存用户选择的工具栏和状态栏显隐值
        markdown.userFootprint.saveToMemory(type, that.checked);

        if(fireOnloadLocalData){
            delete cache.tempFlag;
        }
    }

    // 工具栏 menu
    var toolbarMenu = new gui.Menu();
    toolbarMenu.append(menuMachine('toolbar', {
        label: '显示工具栏', //提示通过‘查看’可再次打开
        type: 'checkbox',
        checked: true,
        click: function(){
            showToolOrConsole(this, 'toolbar');
        }
    }));

    // 状态栏 menu
    var consoleMenu = new gui.Menu();
    consoleMenu.append(menuMachine('console', {
        label: '显示状态栏', //提示通过‘查看’可再次打开
        type: 'checkbox',
        checked: true,
        click: function(){
            showToolOrConsole(this, 'console');
        }
    }));

    // 文件 menu
    var fileMenu = new gui.Menu();
    fileMenu.append(menuMachine('file-newDoc', {
        label: '新建文档 (Ctrl+N)',
        click: function(){
            $('#J-tool-new').trigger('click');
        }
    }));
    fileMenu.append(menuMachine('file-saveDoc', {
        label: '保存文档 (Ctrl+S)',
        click: function(){
            $('#J-tool-save').trigger('click');
        }
    }));
    fileMenu.append(menuMachine('file-openDoc', {
        label: '打开文件... (Ctrl+O)',
        click: function(){
            $('#J-tool-select').trigger('click');
        }
    }));
    fileMenu.append(menuMachine('file-saveAsDoc', {
        label: '另存为... (Ctrl+Shift+S)',
        click: function(){
            markdown.file.saveAs();
        }
    }));
    fileMenu.append(menuMachine('file-docDir', {
        label: '查看文件目录',
        click: function(){
            markdown.file.viewPath();
        }
    }));
    fileMenu.append(menuMachine('file-docDir', {
        label: '从磁盘删除',
        click: function(){
            markdown.file.delFileFromDisk();
        }
    }));
    fileMenu.append(menuMachine('separator', {
        type: 'separator'
    }));
    fileMenu.append(menuMachine('file-saveAsDoc', {
        label: '关闭 (Alt+C)',
        click: function(){
            markdown.tab.close(markdown.tab.getCurrentTab().id);
        }
    }));
    fileMenu.append(menuMachine('file-saveAsDoc', {
        label: '全部关闭',
        click: function(){
            markdown.tab.closeAll();
        }
    }));
    fileMenu.append(menuMachine('file-saveAsDoc', {
        label: '关闭当前以外所有标签',
        click: function(){
            markdown.tab.closeOthers();
        }
    }));
    fileMenu.append(source.separator);
    fileMenu.append(menuMachine('file-exit', {
        label: '打印 (Ctrl+P)',
        enabled: false,
        click: function(){
            window.print();
        }
    }));
    fileMenu.append(source.separator);
    fileMenu.append(menuMachine('file-exit', {
        label: '退出 (Exit)',
        click: function(){
            cache.guiWin.close();
        }
    }));

    source.file = fileMenu;


    // 编辑 menu
    var editMenu = new gui.Menu();
    editMenu.append(menuMachine('edit-undo', {
        label: '撤消 (Ctrl+Z)',
        click: function(){
            $('#J-tool-undo').trigger('click');
        }
    }));
    editMenu.append(menuMachine('edit-redo', {
        label: '重做 (Ctrl+Y)',
        click: function(){
            $('#J-tool-redo').trigger('click');
        }
    }));
    editMenu.append(source.separator);
    editMenu.append(menuMachine('copy', {
        label: '复制 (Ctrl+C)',
        click: function(){
            clipboard.set(editor.getSelection());
        }
    }));
    editMenu.append(menuMachine('cut', {
        label: '剪切 (Ctrl+X)',
        click: function(){
            clipboard.set(editor.getSelection());
            editor.replaceSelection('');
{'fraction_non_alphanumeric': 0.15836462244472257, 'fraction_numerical': 0.002002503128911139, 'mean_word_length': 1.8750299832094028, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5854829', 'n_tokens_mistral': 3718, 'n_tokens_neox': 3747, 'n_words': 587}