pcoletsos_obsidian-dynamic-.../main.js
2026-06-14 09:10:51 +03:00

2 lines
8.1 KiB
JavaScript

/* Dynamic Wide Content */
var E=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var U=Object.getOwnPropertyNames;var q=Object.prototype.hasOwnProperty;var Y=(n,e)=>{for(var t in e)E(n,t,{get:e[t],enumerable:!0})},J=(n,e,t,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of U(e))!q.call(n,a)&&a!==t&&E(n,a,{get:()=>e[a],enumerable:!(i=z(e,a))||i.enumerable});return n};var Q=n=>J(E({},"__esModule",{value:!0}),n);var ue={};Y(ue,{default:()=>p});module.exports=Q(ue);var s=require("obsidian"),o={maxWidth:1600,viewportMargin:64,widenTables:!0,widenCodeBlocks:!0,widenDiagrams:!0,widenImages:!0,noWrapTableCells:!0,readingView:!0,livePreview:!0},X=["dynamic-wide-content","dynamic-wide-content-reading","dynamic-wide-content-live-preview","dynamic-wide-content-tables","dynamic-wide-content-code","dynamic-wide-content-diagrams","dynamic-wide-content-images","dynamic-wide-content-nowrap-tables"],T="dynamic-wide-content-overflow-visible",I="dynamic-wide-content-wide-block",O="dynamic-wide-content-wide-inner",h="dynamic-wide-content-table-block",W="dynamic-wide-content-table-inner",R="dynamic-wide-content-table",v="dynamic-wide-content-code-block",H="dynamic-wide-content-code-inner",L="dynamic-wide-content-diagram-block",B="dynamic-wide-content-diagram-inner",y="dynamic-wide-content-image-block",P="dynamic-wide-content-image-inner",x="dynamic-wide-content-media",V=[T,I,O,h,W,R,v,H,L,B,y,P,x],G=["markdown-preview-sizer","markdown-preview-section","cm-contentContainer"],k=["el-table","table-wrapper"],_=["el-pre"],D=["el-embed-image","internal-embed","image-embed"],f=["el-lang-mermaid","el-lang-plantuml","el-lang-plantuml-png","el-lang-plantuml-svg","el-lang-puml","el-lang-puml-png","el-lang-puml-svg"],K=["mermaid","block-language-mermaid","block-language-plantuml","block-language-plantuml-png","block-language-plantuml-svg","block-language-puml","block-language-puml-png","block-language-puml-svg","plantuml-preview-view"],Z=V.map(n=>`.${n}`).join(", "),ee=f.map(n=>`.${n}`).join(", "),ne=K.map(n=>`.${n}`).join(", "),te=".el-embed-image, .internal-embed.image-embed, .image-embed",p=class extends s.Plugin{constructor(){super(...arguments);this.settings={...o};this.observer=null}async onload(){await this.loadSettings(),this.applySettings(),this.registerMarkdownPostProcessor(t=>{u(t)}),this.registerEvent(this.app.workspace.on("layout-change",()=>{this.applySettings(),this.refreshVisibleContent()})),this.registerEvent(this.app.workspace.on("active-leaf-change",()=>{this.applySettings(),this.refreshVisibleContent()})),this.startObserver(),this.register(()=>{this.stopObserver(),this.clearSettings()}),this.addSettingTab(new C(this.app,this)),this.addCommand({id:"refresh-styles",name:"Refresh wide content styles",callback:()=>{this.applySettings(),this.refreshVisibleContent()}})}async loadSettings(){let t=await this.loadData();this.settings=ie(t)}async saveSettings(){await this.saveData(this.settings),this.applySettings(),this.refreshVisibleContent()}applySettings(){let{body:t}=activeDocument;t.style.setProperty("--dynamic-wide-content-max-width",`${this.settings.maxWidth}px`),t.style.setProperty("--dynamic-wide-content-viewport-margin",`${this.settings.viewportMargin}px`),t.classList.add("dynamic-wide-content"),t.classList.toggle("dynamic-wide-content-reading",this.settings.readingView),t.classList.toggle("dynamic-wide-content-live-preview",this.settings.livePreview),t.classList.toggle("dynamic-wide-content-tables",this.settings.widenTables),t.classList.toggle("dynamic-wide-content-code",this.settings.widenCodeBlocks),t.classList.toggle("dynamic-wide-content-diagrams",this.settings.widenDiagrams),t.classList.toggle("dynamic-wide-content-images",this.settings.widenImages),t.classList.toggle("dynamic-wide-content-nowrap-tables",this.settings.noWrapTableCells)}clearSettings(){let{body:t}=activeDocument;t.classList.remove(...X),t.style.removeProperty("--dynamic-wide-content-max-width"),t.style.removeProperty("--dynamic-wide-content-viewport-margin"),ae(t)}refreshVisibleContent(){for(let t of this.app.workspace.getLeavesOfType("markdown"))t.view instanceof s.MarkdownView&&u(t.view.containerEl);u(activeDocument.body)}startObserver(){let t=new MutationObserver(i=>{for(let a of i)for(let w of Array.from(a.addedNodes))we(w)&&u(w)});t.observe(activeDocument.body,{childList:!0,subtree:!0}),this.observer=t}stopObserver(){var t;(t=this.observer)==null||t.disconnect(),this.observer=null}},C=class extends s.PluginSettingTab{constructor(e,t){super(e,t),this.plugin=t}display(){let{containerEl:e}=this;e.empty(),new s.Setting(e).setName("Display").setHeading(),e.createEl("p",{text:"Use wide tables, diagrams, images, and code blocks without widening normal note text."}),new s.Setting(e).setName("Maximum wide content width").setDesc("Controls how far wide blocks may expand while normal note text stays readable.").addSlider(t=>t.setLimits(900,2400,50).setValue(this.plugin.settings.maxWidth).onChange(async i=>{this.plugin.settings.maxWidth=i,await this.plugin.saveSettings()})),new s.Setting(e).setName("Viewport side margin").setDesc("Keeps widened blocks away from the edge of the pane.").addSlider(t=>t.setLimits(0,160,8).setValue(this.plugin.settings.viewportMargin).onChange(async i=>{this.plugin.settings.viewportMargin=i,await this.plugin.saveSettings()})),d(e,"Widen tables","Allow Markdown tables to break out of the normal reading column.",this.plugin,"widenTables"),d(e,"Widen diagrams","Allow Mermaid and PlantUML diagrams to use a wider scrollable frame.",this.plugin,"widenDiagrams"),d(e,"Widen images","Allow embedded images to display wider than normal note text.",this.plugin,"widenImages"),d(e,"Widen code blocks","Allow preformatted code blocks to use a wider scrollable frame.",this.plugin,"widenCodeBlocks"),d(e,"Keep table cells on one line","Prevents wide tables from wrapping every cell into tall unreadable rows.",this.plugin,"noWrapTableCells"),d(e,"Reading view support","Apply selective widening in Reading view.",this.plugin,"readingView"),d(e,"Live Preview support","Apply selective widening to rendered embeds in Live Preview.",this.plugin,"livePreview")}};function d(n,e,t,i,a){new s.Setting(n).setName(e).setDesc(t).addToggle(w=>w.setValue(i.settings[a]).onChange(async F=>{i.settings[a]=F,await i.saveSettings()}))}function ie(n){let e=se(n)?n:{};return{maxWidth:N(e.maxWidth,o.maxWidth),viewportMargin:N(e.viewportMargin,o.viewportMargin),widenTables:c(e.widenTables,o.widenTables),widenCodeBlocks:c(e.widenCodeBlocks,o.widenCodeBlocks),widenDiagrams:c(e.widenDiagrams,o.widenDiagrams),widenImages:c(e.widenImages,o.widenImages),noWrapTableCells:c(e.noWrapTableCells,o.noWrapTableCells),readingView:c(e.readingView,o.readingView),livePreview:c(e.livePreview,o.livePreview)}}function se(n){return typeof n=="object"&&n!==null}function N(n,e){return typeof n=="number"&&Number.isFinite(n)?n:e}function c(n,e){return typeof n=="boolean"?n:e}function u(n){oe(n),le(n),re(n),de(n),ce(n)}function ae(n){l(n,Z,e=>{e.classList.remove(...V)})}function oe(n){M(n,G,e=>{e.classList.add(T)})}function le(n){M(n,k,e=>{r(e,h),b(e,W)}),l(n,"table",e=>{m(e)&&(e.classList.add(R),r(g(e,k),h))})}function re(n){M(n,_,e=>{r(e,v)}),l(n,"pre",e=>{m(e)&&(r(g(e,_),v),b(e,H))})}function de(n){l(n,ee,e=>{m(e)&&(r(e,L),S(e))}),l(n,ne,e=>{m(e)&&(r(g(e,f),L),b(e,B),S(e))})}function ce(n){l(n,te,e=>{m(e)&&(r(g(e,D),y),b(e,P),S(e))}),l(n,"img",e=>{!m(e)||ge(e)||(r(g(e,D),y),$(e))})}function r(n,e){n.classList.add(I,e),me(n)}function b(n,e){n.classList.add(O,e)}function S(n){l(n,"img, svg",e=>{$(e)})}function $(n){n.classList.add(x)}function me(n){let e=n.parentElement;for(;e;)j(e,G)&&e.classList.add(T),e=e.parentElement}function g(n,e){var t;return(t=A(n,[...e,"cm-embed-block"]))!=null?t:n}function m(n){return A(n,["markdown-rendered","markdown-source-view","markdown-preview-section","markdown-preview-sizer","cm-embed-block"])!==null}function ge(n){return A(n,[...f,...K])!==null}function M(n,e,t){l(n,e.map(i=>`.${i}`).join(", "),t)}function l(n,e,t){n.matches(e)&&t(n),n.querySelectorAll(e).forEach(i=>{t(i)})}function A(n,e){let t=n;for(;t;){if(j(t,e))return t;t=t.parentElement}return null}function j(n,e){return e.some(t=>n.classList.contains(t))}function we(n){return n.nodeType===Node.ELEMENT_NODE&&"classList"in n}