文字列をまるめる
Jetpack(Firefox?)のコンテキストメニューのlabelは62文字くらいで省略されてしまうのでUIを考えた。
jetpack.future.import("menu"); jetpack.menu.context.page.add("1234567890123456789012345678901234567890123456789012345678901234567890")
問題点
sample
任意の文字数に収まるように、真ん中を省略する関数を考えた。
function round_string(str, num) { let separator = "...", n = Math.floor((num - separator.length) / 2); return str.substr(0, n) + separator + str.substr(str.length - n); } jetpack.future.import("menu"); let path = "C:\\Users\\hoge\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\piyo.default\\JetpackFeatures"; let newPath = round_string(path, 50); let aFile = Components.classes["@mozilla.org/file/local;1"]. createInstance(Components.interfaces.nsILocalFile); aFile.initWithPath(path); jetpack.menu.context.page.add(path); // length : 83 jetpack.menu.context.page.add(newPath); // length : 49 jetpack.menu.context.page.add(aFile.leafName);
感想
こっそり製作中のJetpack featureで使う。
関連
- String - MDC
- Menu - MDC
- nsILocalFile Interface
- substrとsubstringの違い - おもしろWEBサービス開発日記
- substrはECMAで標準化されていない。ほー。