aboutsummaryrefslogtreecommitdiffstats
path: root/takahashiPresentation.js
blob: dbeaf8b3ebe8af8004c82aee73d7d15d8fcacc8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * ==VimperatorPlugin==
 * @name			KeywordStore.js
 * @description			Store the keywords when ":open" or ":tabopen" launched
 * @author			Y. Maeda (clouds.across.the.moon@gmail.com)
 * @link			
 * @version			0.1
 * ==/VimperatorPlugin==
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Usage:
 *  :kssearch <str>
 * if <str> is nothing, incremental search starts by last keyword used by ":open" or ":tabopen".
 * <str> can be completed by using history of keywords.
 * 
 * Tested on:
 *	Firefox version: 3.0.3
 *	Vimperator version: 1.2
 *		URL: https://addons.mozilla.org/firefox/addon/4891
 */
(function (){
var queue = [];

function push(strs){
	for(let i
// PLUGIN_INFO//{{{
var PLUGIN_INFO =
<VimperatorPlugin>
    <name>{NAME}</name>
    <description>simple takahashi-method presentation tool</description>
    <author mail="konbu.komuro@gmail.com" homepage="http://d.hatena.ne.jp/hogelog/">hogelog</author>
    <version>0.1.1</version>
    <minVersion>2.3pre</minVersion>
    <maxVersion>2.3pre</maxVersion>
    <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/takahashiPresentation.js</updateURL>
    <date>2008/12/23 09:20:07</date>
    <detail><![CDATA[

== COMMANDS ==
presentation:
   start presentation
== HOWTO ==
open HTML file includes <pre id="page">...</pre> and <div id="text">...</div>.
start :presentation.
     ]]></detail>
</VimperatorPlugin>;
//}}}
(function() {
    let keys = [
        ['<Right>', 'next page', function() nextPage()],
        ['<Left>', 'prev page', function() prevPage()],
        ['^', 'first page', function() loadPage(0)],
        ['$', 'last page', function() loadPage(pages.length-1)],
        ['.', 'last page', function(count) loadPage(count?count-1:0), {count: true}],
        ['q', 'stop presentation', function() stop()],
    ];
    let win;
    let doc;
    let pages = [];
    let curpage = 0;
    let pre;
    let header;
    let fontSize = 18.0;

    function addKeys() {
        keys.forEach(function([key, desc, action, extra])
            mappings.addUserMap([modes.NORMAL], [key], desc, action, extra));
    }
    function fitPage() {
        if(pre.innerHTML=='') return;
        pre.style.display = 'inline';
        let parentWidth = pre.parentNode.offsetWidth;
        let parentHeight = pre.parentNode.offsetHeight;
        let width = pre.offsetWidth;
        let height = pre.offsetHeight;
        let preRatio = width/height;
        let winRatio = parentWidth/parentHeight;
        if(preRatio>winRatio) {
            fontSize *= 0.9*(parentWidth-10)/width;
        } else {
            fontSize *= 0.9*(parentHeight-10)/height;
        }
        pre.style.fontSize = fontSize+'px';
        pre.style.display = 'block';
    }
    function loadPage(page) {
        let text = pages[page];
        pre.innerHTML = text;
        if(header) {
            header.innerHTML = (page+1)+'/'+pages.length;
        }
        fitPage();
    }
    function nextPage() {
        curpage = curpage>=pages.length-1 ? 0 : curpage+1;
        loadPage(curpage);
    }
    function prevPage() {
        curpage = curpage<=0 ? pages.length-1 : curpage-1;
        loadPage(curpage);
    }
    function parsePages(text) {
        return text.split('----')
                   .map(function(txt) txt.replace(/^(?:\r\n|[\r\n])|(?:\r\n|[\r\n])$/g, ''));
    }
    function save_setting(setting) {
        setting.fullscreen = options.fullscreen;
        setting.guioptions = options.guioptions;
        // TODO: save key mapping
        //setting.mappings = keys.map(function([key,]) {
        //    let mapping = mappings.get(modes.NORMAL, key);
        //    return [mapping.modes, key, mapping.description, mapping.action, mapping.extra];
        //});
    }
    function load_setting(setting) {
        options.fullscreen = setting.fullscreen;
        options.guioptions = setting.guioptions;
        // TODO: load key mapping
        //setting.mappings.forEach(function([modes, key, desc, action, extra]) {
        //    mappings.addUserMap(modes, [key], desc, action, extra);
        //});
    }
    let original_setting = {};
    function start() {
        save_setting(original_setting);

        options.fullscreen = true;
        options.guioptions = '';
        win = window.content;
        doc = win.document;
        let text = util.evaluateXPath('//div[@id="text"]').snapshotItem(0);
        pages = parsePages(text.innerHTML);
        addKeys();

        header = util.evaluateXPath('//*[@id="header"]').snapshotItem(0);

        pre = util.evaluateXPath('//pre[@id="page"]').snapshotItem(0);
        pre.style.fontSize = fontSize+'px';
        pre.style.margin = '0px';

        loadPage(0);
    }
    function stop() {
        load_setting(original_setting);
    }

    commands.add(['presentation'], 'start presentation', //{{{
        function(args) {
            start();
        },
        {
            argCount: '0',
        }); //}}}

})();
// vim: fdm=marker sw=4 ts=4 et: