ObsidianにCosenseのようなリスト動作を実装
- Auto Bulletというプラグインを改造して、Cosenseのように行頭空白でリストに、さらに空白でインデント、削除でアウトデントの動きを実装。
- Gemini頼みで。
- 書き心地良くなった。
- 行頭空白でインデント、削除でアウトデント。
※ 自分用に保管。Gemini仕事で、もしかしたら問題をきたす怖れ。
※ 「Auto Bullet」というプラグインを参考に。
code:javascript
- /*
- THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
- if you want to view the source, please visit the github repository of this plugin
- */
- var b=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var S=(l,i)=>{for(var e in i)b(l,e,{get:ie,enumerable:!0})},W=(l,i,e,t)=>{if(i&&typeof i=="object"||typeof i=="function")for(let s of C(i))!w.call(l,s)&&s!==e&&b(l,s,{get:()=>is,enumerable:!(t=m(i,s))||t.enumerable});return l};var y=l=>W(b({},"__esModule",{value:!0}),l);var T={};S(T,{default:()=>h});module.exports=y(T);var r=require("obsidian"),v={enableHalfWidthSpace:!0,enableFullWidthSpace:!0,enableTab:!0,customizeHomeKey:!0},h=class extends r.Plugin{constructor(){super(...arguments);this.handleEditorChange=e=>{let t=e.getCursor(),s=e.getLine(t.line),n=s.charAt(t.ch-1),o=a=>a.trim().startsWith("```"),c=a=>a.trim().startsWith("$$"),u=!1,g=!1;for(let a=0;a<=t.line;a++){let d=e.getLine(a);o(d)&&(u=!u),c(d)&&(g=!g)}if(!(u||g||o(s)||c(s)||s.trim().startsWith("#")||s.trim().startsWith(">")||/^\s*\d+.\s/.test(s)||/^\s*|.|\s$/.test(s)||/|.|/.test(s.trim())||/^\s(-{3,}|[[]]{3,}|_{3,})\s$/.test(s))){
- // ---
- // ここからが修正されたロジック
- // 「- 」の後にスペースが入力された場合の自動インデント
- const beforeCursor = s.slice(0, t.ch);
- const leadingIndent = beforeCursor.match(/^\s*/)0;
- const bulletAndSpace = beforeCursor.slice(leadingIndent.length);
- // カーソル直前の文字が空白で、その直前の文字列が「- 」で始まる場合にトリガー
- if (n === " " && bulletAndSpace.startsWith("- ") && bulletAndSpace.length === 3) {
- // 「- 」の部分を「タブ-空白」に置き換える
- const replacement = "\t" + "- ";
- e.replaceRange(replacement, {line: t.line, ch: leadingIndent.length}, {line: t.line, ch: leadingIndent.length + 3});
- e.setCursor({line: t.line, ch: t.ch});
- return;
- }
- // ---
- if((n===" "&&this.settings.enableHalfWidthSpace||n===" "&&this.settings.enableTab||n==="\u3000"&&this.settings.enableFullWidthSpace)){let a=s.slice(0,t.ch);if(a.trim()===""&&!s.trim().startsWith("- ")){ - let f="- "; - let currentIndentLength = a.length; - let newIndentLength = Math.max(0, currentIndentLength - 1); - let newIndent = ""; - if (newIndentLength > 0 && s[[0]] === "\t") { - newIndent = "\t".repeat(newIndentLength); - } else { - newIndent = " ".repeat(newIndentLength); - } - let newContent = newIndent + f;- e.replaceRange(newContent, {line: t.line, ch: 0}, {line: t.line, ch: a.length}); - e.setCursor({line: t.line, ch: newContent.length}); - }}- }};
- this.handleKeydown = (event) => {
- // Backspaceキーが押されたことを確認
- if (event.key === "Backspace") {
- const activeEditor = this.app.workspace.activeEditor;
- if (!activeEditor) return;
- const editor = activeEditor.editor; - const cursor = editor.getCursor(); - const lineText = editor.getLine(cursor.line);- // カーソルがリスト記号の直後にある場合、「- 」をまとめて削除 - const listRegex = /^\s*-\s/; - const match = lineText.match(listRegex); - if (match && cursor.ch === match[[0]].length) { - event.preventDefault(); - const startOfBullet = { line: cursor.line, ch: cursor.ch - 1 }; - editor.replaceRange("", startOfBullet, cursor); - return; - }- // カーソルがインデントの直後にある場合、インデントを1つだけ削除 - const indentRegex = /^\s+$/; - const indentMatch = lineText.match(indentRegex); - if (indentMatch && cursor.ch === indentMatch[[0]].length) { - event.preventDefault(); - const newCursorCh = cursor.ch - 1; - editor.replaceRange("", { line: cursor.line, ch: newCursorCh }, cursor); - return; - } - }- };}
- async onload(){await this.loadSettings(),this.addSettingTab(new p(this.app,this)),this.registerEvent(this.app.workspace.on("editor-change",this.handleEditorChange)),this.registerDomEvent(document, "keydown", this.handleKeydown);this.addCommand({id:"move-cursor-after-bullet",name:"Move cursor after bullet point",editorCallback:e=>{let t=e.getCursor(),s=e.getLine(t.line);s.trim().startsWith("- ")?this.moveCursorAfterBullet(e):/^\s*\d+.\s/.test(s)?this.moveCursorAfterNumberedList(e):e.setCursor({line:t.line,ch:0})}})}moveCursorAfterBullet(e){try{let t=e.getCursor(),s=e.getLine(t.line);if(s.trim().startsWith("- ")){let n=0;for(;n<s.length&&(sn===" "||sn==="\u3000"||sn==="\t");)n++;if(s.substring(n).startsWith("- ")){let o=n+2;if(t.ch!==o)return e.setCursor({line:t.line,ch:o}),!0}}}catch(t){console.error(
Error in moveCursorAfterBullet: ${t.message})}return!1}async loadSettings(){this.settings=Object.assign({},v,await this.loadData())}async saveSettings(){await this.saveData(this.settings)}onunload(){}},p=class extends r.PluginSettingTab{constructor(i,e){super(i,e),this.plugin=e}display(){let{containerEl:i}=this;i.empty(),new r.Setting(i).setName("Half-width space").setDesc("Insert bullet points when you press a half-width space at the beginning of a line").addToggle(e=>e.setValue(this.plugin.settings.enableHalfWidthSpace).onChange(async t=>{this.plugin.settings.enableHalfWidthSpace=t,await this.plugin.saveSettings()})),new r.Setting(i).setName("Full-width space").setDesc("Insert bullet points when you press a full-width space at the beginning of a line").addToggle(e=>e.setValue(this.plugin.settings.enableFullWidthSpace).onChange(async t=>{this.plugin.settings.enableFullWidthSpace=t,await this.plugin.saveSettings()})),new r.Setting(i).setName("Tab").setDesc("Insert bullet points when you press a tab at the beginning of a line").addToggle(e=>e.setValue(this.plugin.settings.enableTab).onChange(async t=>{this.plugin.settings.enableTab=t,await this.plugin.saveSettings()})),new r.Setting(i).setName("Customize home key").setDesc("Move cursor after bullet point (- ) when pressing Home or Ctrl+A in a bullet line").addToggle(e=>e.setValue(this.plugin.settings.customizeHomeKey).onChange(async t=>{this.plugin.settings.customizeHomeKey=t,await this.plugin.saveSettings()}))}}; - /* nosourcemap */