「
ラクラク罫線
」を編集中 (節単位)
ナビゲーションに移動
検索に移動
警告:
ログインしていません。編集を行うと、あなたの IP アドレスが公開されます。
ログイン
または
アカウントを作成
すれば、あなたの編集はその利用者名とともに表示されるほか、さまざまなメリットもあります。
スパム攻撃防止用のチェックです。 決して、ここには、値の入力は
しない
でください!
== ソースコード == <syntaxhighlight lang="javascript" copy> // ruledliner.js(ラクラク罫線.js) // 最終更新:2012/12/22 var notch = ""; var depth = 35; var single_spc = " "; var bouble_spc = " "; // -- prototype関数 -- // String // 桁揃えに使う文字。 String.prototype.spcS = single_spc; String.prototype.spcW = bouble_spc; // インデックス番号iの文字が半角であるかどうか。 String.prototype.isHan = function(i) { // Shift_JIS: 0x0 ~ 0x80, 0xa0 , 0xa1 ~ 0xdf , 0xfd ~ 0xff // Unicode : 0x0 ~ 0x80, 0xf8f0, 0xff61 ~ 0xff9f, 0xf8f1 ~ 0xf8f3 var c = this.charCodeAt(i); return ((c >= 0x0 && c < 0x81) || (c == 0xf8f0) || (c >= 0xff61 && c < 0xffa0) || (c >= 0xf8f1 && c < 0xf8f4)); }; // 文字列の長さ。半角換算。 String.prototype.blength = function() { var blen = 0; for (var i = 0; i < this.length; i++) { (this.isHan(i)) ? blen += 1: blen += 2; } return blen; }; // インデックス番号の変換(半角→通常)。 String.prototype.bindex2i = function(bi) { if (bi < 0) { Alert("bi < 0 in bindex2i !!"); Quit(); } var n = 0; for (var i = 0; i < this.length; i++) { if (!this.isHan(i)) { n++; } if (i + n >= bi) { return i; } } return bi; }; // 文字列から指定した位置の文字を抜き出し。半角指定。 String.prototype.bcharAt = function(bi) { var i = this.bindex2i(bi); if (bi >= this.blength()) { i = this.length; } return this.charAt(i); }; // 文字列を分割。半角指定。 String.prototype.bdiv2ary = function(bi) { var i = this.bindex2i(bi); return { front: this.substr(0, i), tail: this.substr(i, this.length - i) }; }; // 文字列の繰り返し。 String.prototype.repeat = function(n) { var str = this; var s = ""; while (n > 0) { if (n & 1) { s += str; } n = n >> 1; str += str; } return s; }; // 文字列を指定桁数まで延長。半角指定。 String.prototype.beven = function(bn) { switch (arguments.length) { case 0: bn = 0; } var re = new RegExp(this.spcS + "+$", "ig"); var str = this; str = str.replace(re, ""); var s = ""; if (bn == 0) { s = str + this.spcS.repeat(str.blength() % 2); } else { bn += bn % 2; s = str + this.spcS.repeat(bn - str.blength()); } var re = new RegExp(this.spcS + "{2}", "ig"); s = s.replace(re, this.spcW); return s; }; // 最長要素の長さ。半角換算。 Array.prototype.maxbLen = function() { var blen = 0; for (var i = 0; i < this.length; i++) { if (typeof(this[i]) != "string") { Alert("maxbLen!"); Quit(); } var s = this[i].blength(); (blen > s) ? blen: blen = s; } return blen; }; // -- 罫線を引くオブジェクト -- var TblRuler = function() { this.initialize.apply(this, arguments); }; TblRuler.prototype = { spcS: single_spc, spcW: bouble_spc, action: "none", tbl: [], x1: 0, y1: 0, x2: 0, y2: 0, initialize: function(tblTop, tblBottom, tx, ty, bx, by, str) { this.tbl = str.split("\n"); this.y1 = ty - tblTop; this.x1 = this.tbl[this.y1].substr(0, tx - 1).blength(); this.y2 = by - tblTop; this.x2 = this.tbl[this.y2].substr(0, bx - 1).blength(); this.tx = tx; this.bx = bx; }, length: function() { return this.tbl.length; }, vline: function() { this.action = "vline"; var vksen = "│"; // 選択行の事前処理。 var l_ary = []; var r_ary = []; for (var i = this.y1; i <= this.y2; i++) { var ft = this.tbl[i].bdiv2ary(this.x1); l_ary.push(ft.front); r_ary.push(ft.tail); } for (var i = 0; i < r_ary.length; i++) { var r_aryi = r_ary[i].split(vksen); r_aryi[0] = r_aryi[0].beven(); r_ary[i] = r_aryi.join(vksen); } var lenL = l_ary.maxbLen(); var lenR = r_ary.maxbLen(); for (var i = 0; i < this.tbl.length; i++) { if (i >= this.y1 && i <= this.y2) { this.tbl[i] = l_ary[i - this.y1].beven(lenL) + vksen + r_ary[i - this.y1]; } } }, vline_at_end: function() { this.action = "vline_at_end"; for (var i = this.y1; i <= this.y2; i++) { var work = this.tbl[i].split(""); var rend = work.pop(); if (/[─│┌┐└┘├┤┬┴┼]/.test(rend)) { work.push(this.spcW + rend); } else { work.push(rend + this.spcW); } this.tbl[i] = work.join(""); } }, hline: function() { this.action = "hline"; var hksen = "─"; var s = this.tbl[this.y1].slice(tx - 1, bx - 1); var regK = new RegExp("[─│┌┐└┘├┤┬┴┼]", "i"); var reg = new RegExp("[^" + this.spcW + "]+", "i"); var mid = s.slice(1, s.length - 1); if (s.length >= 3 && regK.test(s.charAt(0)) && regK.test(s.charAt(s.length - 1)) && !reg.test(mid)) { var reg2 = new RegExp(this.spcW, "ig"); var c = this.tbl[this.y1].split(""); s = s.replace(reg2, hksen); c.splice(tx - 1, bx - tx, s); this.tbl[this.y1] = c.join(""); } else { var xmax = 0; if (this.y1 >= this.tbl.length - 1) { xmax = this.tbl[this.y1].blength(); } else { xmax = Math.max(this.tbl[this.y1].blength(), this.tbl[this.y1 + 1].blength()); } var s1 = this.spcW.repeat((this.x1 + 1) / 2); var s2 = hksen.repeat((this.x2 - this.x1 + 1) / 2); var s3 = this.spcW.repeat((xmax - s1.blength() - s2.blength() + 1) / 2); var s = s1 + s2 + s3; this.tbl.splice(this.y1 + 1, 0, s); } }, makeFrame: function() { this.action = "makeFrame"; var re = new RegExp("[" + this.spcS + this.spcW + "]+$", "i"); for (var i = 0; i < this.tbl.length; i++) { this.tbl[i] = this.tbl[i].replace(re, ""); } var len = this.tbl.maxbLen() + this.tbl.maxbLen() % 2; for (var i = 0; i < this.tbl.length; i++) { this.tbl[i] = "│" + this.tbl[i].beven(len) + "│"; } this.tbl.unshift("┌" + "─".repeat(len / 2) + "┐"); this.tbl.push("└" + "─".repeat(len / 2) + "┘"); }, linkChk: function(bx, y) { var s = 0; // 上は? if (y >= 1) { var c = this.tbl[y - 1].bcharAt(bx); if (/[┌┐│┬├┼┤]/.test(c)) { s += 8; } } // 下は? if (y < this.tbl.length - 1) { var c = this.tbl[y + 1].bcharAt(bx); if (/[└┘│┴├┼┤]/.test(c)) { s += 4; } } // 左は? if (this.tbl[y].bindex2i(bx) >= 1) { var c = this.tbl[y].charAt(this.tbl[y].bindex2i(bx) - 1); if (/[┌└─┬┼├┴]/.test(c)) { s += 2; } } // 右は? if (this.tbl[y].bindex2i(bx) < this.tbl[y].length - 1) { var c = this.tbl[y].charAt(this.tbl[y].bindex2i(bx) + 1); if (/[┐┘─┬┼┤┴]/.test(c)) { s += 1; } } return s; }, code2char: function(code) { switch (code) { case 0: return ""; case 1: return ""; case 2: return ""; case 3: return "─"; // " LR" case 4: return ""; case 5: return "┌"; // " D R" case 6: return "┐"; // " DL" case 7: return "┬"; // " DLR" case 8: return ""; case 9: return "└"; // "U R" case 10: return "┘"; // "U L " case 11: return "┴"; // "U LR" case 12: return "│"; // "UD " case 13: return "├"; // "UD R" case 14: return "┤"; // "UDL " case 15: return "┼"; // "UDLR" default: return ""; } }, adjust: function() { this.action = "adjust"; var work = this.tbl.slice(0); var re = new RegExp(this.spcS + "{2}", "ig"); for (var y = 0; y < this.tbl.length; y++) { var line = this.tbl[y].replace(re, this.spcW); var ex = ""; var bi = 0; while (bi < line.blength()) { var ks2 = this.code2char(this.linkChk(bi, y)); var s = line.bcharAt(bi); if (/[─│┌┐└┘├┤┬┴┼ ]/.test(s) && ks2.length != 0) { ex += ks2; } else { ex += s; } if (s.isHan()) { bi += 1; } else { bi += 2; } } work[y] = ex; } this.tbl = work.slice(0); var re = new RegExp("[" + this.spcS + this.spcW + "]+$", "i"); for (var i = 0; i < this.tbl.length; i++) { this.tbl[i] = this.tbl[i].replace(re, ""); } }, adjust_line: function(y) { this.action = "adjust_line"; var re = new RegExp(this.spcS + "{2}", "ig"); var line = this.tbl[y].replace(re, this.spcW); var ex = ""; var bi = 0; while (bi < line.blength()) { var ks2 = this.code2char(this.linkChk(bi, y)); var s = line.bcharAt(bi); if (/[─│┌┐└┘├┤┬┴┼ ]/.test(s) && ks2.length != 0) { ex += ks2; } else { ex += s; } if (s.isHan()) { bi += 1; } else { bi += 2; } } this.tbl[y] = ex; } }; function ruler_action() { if (tx == 1 && tx == bx && by == tblTop) { return "makeframe"; } else if (tx == 1 && tx == bx && ty == tblBottom) { return "adjust"; } else if (tx == 1 && by == ty && ax == 1) { return "adjust_line"; } else if (tx == document.GetLine(ty).length + 1) { return "vline_at_end"; } else if (tx != bx && ty == by) { return "hline"; } return "vline"; } // -------------------- // 選択範囲の記録と伸張。 var sel = Editor.ActiveDocument.Selection; var tx = sel.GetTopPointX(mePosLogical); var ty = sel.GetTopPointY(mePosLogical); var bx = sel.GetBottomPointX(mePosLogical); var by = sel.GetBottomPointY(mePosLogical); var ax = sel.GetActivePointX(mePosLogical); var ay = sel.GetActivePointY(mePosLogical); sel.SetActivePoint(mePosLogical, 1, ty, false); sel.SetActivePoint(mePosLogical, 1, by, true); if ((bx == 1) && (ty != by)) { sel.CharLeft(true, 1); bx = sel.GetBottomPointX(mePosLogical); by--; } else { sel.EndOfLine(true, mePosLogical); } var tblTop = ty; var tblBottom = by; for (var i = 0; i <= depth; i++) { var line = document.GetLine(ty - i); if (line.charAt(0) === (notch) || ty - i <= 0) { break; } tblTop = ty - i; } for (var i = 0; i <= depth; i++) { var line = document.GetLine(by + i); if (line.charAt(0) === (notch) || by + i >= document.GetLines()) { break; } tblBottom = by + i; } if (ty < tblTop) { tblTop = ty; } if (by > tblBottom) { tblBottom = by; } sel.SetActivePoint(mePosLogical, 1, tblTop, false); sel.SetActivePoint(mePosLogical, 1, tblBottom, true); sel.EndOfLine(true, mePosLogical); var tblruler = new TblRuler(tblTop, tblBottom, tx, ty, bx, by, document.selection.text); switch (ruler_action()) { case "makeframe": tblruler.makeFrame(); break; case "adjust": tblruler.adjust(); break; case "adjust_line": tblruler.adjust_line(ty - tblTop); break; case "hline": tblruler.hline(); break; case "vline_at_end": tblruler.vline_at_end(); break; default: tblruler.vline(); } sel.SetActivePoint(mePosLogical, 1, tblTop, false); sel.SetActivePoint(mePosLogical, 1, tblBottom, true); sel.EndOfLine(true, mePosLogical); sel.Text = tblruler.tbl.join("\r\n"); sel.SetActivePoint(mePosLogical, tx, ty, false); </syntaxhighlight>
編集内容の要約:
MeryWikiへの投稿はすべて、他の投稿者によって編集、変更、除去される場合があります。 自分が書いたものが他の人に容赦なく編集されるのを望まない場合は、ここに投稿しないでください。
また、投稿するのは、自分で書いたものか、パブリック ドメインまたはそれに類するフリーな資料からの複製であることを約束してください(詳細は
MeryWiki:著作権
を参照)。
著作権保護されている作品は、許諾なしに投稿しないでください!
このページを編集するには、下記の数式を計算してその答えを欄に入力してください (
ヘルプ
):
いちたすには =
キャンセル
編集ヘルプ
(新しいウィンドウで開きます)
スポンサーリンク
ナビゲーション メニュー
個人用ツール
ログインしていません
トーク
投稿記録
アカウント作成
ログイン
名前空間
ページ
議論
日本語
表示
閲覧
編集
履歴表示
その他
検索
スポンサーリンク
スポンサーリンク
案内
メインページ
ヘルプ
よくある質問
マクロリファレンス
マクロライブラリ
プラグインライブラリ
構文ファイル
テーマ
寄付・開発支援
練習用ページ
開発室
開発者のブログ
ツール
スポンサーリンク