@omegagrid/markdown 0.11.19 → 0.11.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,9 +20,23 @@ export declare class MarkdownEditor extends LitElement implements Layout {
20
20
  slider: Slider;
21
21
  onUpload: (file: File) => Promise<string>;
22
22
  modifyImageUrl: (file: string) => Promise<string> | string;
23
+ /**
24
+ * Rewrites the href the browser sees for a link (e.g. a page name into the address of that page).
25
+ * The markdown keeps the destination as written and MarkdownLinkEvent.href reports it; the rewritten
26
+ * address is what a copied link or a middle click gets. Applied when the editor is (re)created.
27
+ */
28
+ modifyLinkUrl: (href: string) => string;
29
+ /**
30
+ * The id of the element the content scrolls to once it has rendered (a link or an address with a
31
+ * fragment), kept in view while the content settles. Set on a rendered document it scrolls right
32
+ * away; it is consumed by the scroll, so a later reload of the content starts at the top.
33
+ */
34
+ get anchor(): string;
35
+ set anchor(value: string);
23
36
  get milkdownElm(): HTMLElement;
24
37
  get editor(): milkdown.core.Editor;
25
38
  constructor();
39
+ /** Scrolls the rendered content to an element: an anchor (`#id`, see #findElement) or any selector. */
26
40
  scrollToSelector(selector: string): void;
27
41
  connectedCallback(): void;
28
42
  updateWidth(): void;
@@ -1 +1 @@
1
- {"version":3,"file":"markdownEditor.d.ts","sourceRoot":"","sources":["../../src/components/markdownEditor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,kBAAkB,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAElF,OAAO,EAAE,UAAU,EAAQ,MAAM,KAAK,CAAC;AAMvC,OAAO,KAAK,KAAK,QAAQ,MAAM,yBAAyB,CAAC;AAKzD,qBAAa,iBAAkB,SAAQ,KAAK;IAC3C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,CAAC,EAAE,iBAAiB,CAAC;IAC9B,QAAQ,CAAC,SAAS,EAAE,YAAY,CAAC;gBAErB,IAAI,EAAE,OA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ownEditor_editor,_MarkdownEditor_creating,_MarkdownEditor_onScroll,_MarkdownEditor_closeCodeEditing,_MarkdownEditor_restoreScroll,_MarkdownEditor_applySpellcheck,__decorate=this&&this.__decorate||function(t,e,o,r){var i,d=arguments.length,n=d<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(t,e,o,r);else for(var s=t.length-1;s>=0;s--)(i=t[s])&&(n=(d<3?i(n):d>3?i(e,o,n):i(e,o))||n);return d>3&&n&&Object.defineProperty(e,o,n),n},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(t,e,o,r){if("a"===o&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===o?r:"a"===o?r.call(t):r?r.value:e.get(t)},__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(t,e,o,r,i){if("m"===r)throw new TypeError("Private method is not writable");if("a"===r&&!i)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!i:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===r?i.call(t,o):i?i.value=o:e.set(t,o),o};import{dom,events}from"@omegagrid/core";import{LitElement,html}from"lit";import{customElement,query,property}from"lit/decorators.js";import{style}from"./markdownEditor.style";import styles from"./styles";import{createEditor}from"../utils";const SCROLL_SETTLE_TIMEOUT=2e3;export class MarkdownLinkEvent extends Event{constructor(t){super("markdown.link",{bubbles:!0,composed:!0,cancelable:!0}),Object.assign(this,t)}}let MarkdownEditor=class extends LitElement{get content(){return __classPrivateFieldGet(this,_MarkdownEditor_content,"f")}set content(t){t!==__classPrivateFieldGet(this,_MarkdownEditor_content,"f")&&__classPrivateFieldSet(this,_MarkdownEditor_scrollTop,0,"f"),__classPrivateFieldSet(this,_MarkdownEditor_content,t,"f"),__classPrivateFieldGet(this,_MarkdownEditor_editor,"f")?.destroy(),__classPrivateFieldSet(this,_MarkdownEditor_editor,null,"f"),this.requestUpdate()}get milkdownElm(){return this.container.firstElementChild}get editor(){return __classPrivateFieldGet(this,_MarkdownEditor_editor,"f")}constructor(){super(),_MarkdownEditor_instances.add(this),_MarkdownEditor_content.set(this,void 0),this.maxContentWidth=800,this.contentAlignment="left",this.readOnly=!1,this.spellcheck=!1,_MarkdownEditor_mouseIn.set(this,!1),_MarkdownEditor_scrollTop.set(this,0),_MarkdownEditor_editor.set(this,void 0),_MarkdownEditor_creating.set(this,!1),_MarkdownEditor_onScroll.set(this,(()=>{__classPrivateFieldSet(this,_MarkdownEditor_scrollTop,this.milkdownElm?.scrollTop??0,"f"),__classPrivateFieldGet(this,_MarkdownEditor_instances,"m",_MarkdownEditor_closeCodeEditing).call(this)})),this._onChange=(t,e)=>{__classPrivateFieldSet(this,_MarkdownEditor_content,t,"f"),this.dispatchEvent(new events.ChangeEvent({value:t,oldValue:e}))},this.render=()=>html`
1
+ var _MarkdownEditor_instances,_MarkdownEditor_content,_MarkdownEditor_mouseIn,_MarkdownEditor_scrollTop,_MarkdownEditor_anchor,_MarkdownEditor_editor,_MarkdownEditor_creating,_MarkdownEditor_onScroll,_MarkdownEditor_closeCodeEditing,_MarkdownEditor_restoreScroll,_MarkdownEditor_scrollToAnchor,_MarkdownEditor_settleScroll,_MarkdownEditor_findElement,_MarkdownEditor_applySpellcheck,__decorate=this&&this.__decorate||function(t,e,o,r){var i,n=arguments.length,d=n<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)d=Reflect.decorate(t,e,o,r);else for(var s=t.length-1;s>=0;s--)(i=t[s])&&(d=(n<3?i(d):n>3?i(e,o,d):i(e,o))||d);return n>3&&d&&Object.defineProperty(e,o,d),d},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(t,e,o,r){if("a"===o&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===o?r:"a"===o?r.call(t):r?r.value:e.get(t)},__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(t,e,o,r,i){if("m"===r)throw new TypeError("Private method is not writable");if("a"===r&&!i)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!i:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===r?i.call(t,o):i?i.value=o:e.set(t,o),o};import{dom,events}from"@omegagrid/core";import{LitElement,html}from"lit";import{customElement,query,property}from"lit/decorators.js";import{style}from"./markdownEditor.style";import styles from"./styles";import{createEditor,LINK_HREF_ATTRIBUTE}from"../utils";const SCROLL_SETTLE_TIMEOUT=2e3;export class MarkdownLinkEvent extends Event{constructor(t){super("markdown.link",{bubbles:!0,composed:!0,cancelable:!0}),Object.assign(this,t)}}let MarkdownEditor=class extends LitElement{get content(){return __classPrivateFieldGet(this,_MarkdownEditor_content,"f")}set content(t){t!==__classPrivateFieldGet(this,_MarkdownEditor_content,"f")&&__classPrivateFieldSet(this,_MarkdownEditor_scrollTop,0,"f"),__classPrivateFieldSet(this,_MarkdownEditor_content,t,"f"),__classPrivateFieldGet(this,_MarkdownEditor_editor,"f")?.destroy(),__classPrivateFieldSet(this,_MarkdownEditor_editor,null,"f"),this.requestUpdate()}get anchor(){return __classPrivateFieldGet(this,_MarkdownEditor_anchor,"f")}set anchor(t){__classPrivateFieldSet(this,_MarkdownEditor_anchor,t,"f"),t&&__classPrivateFieldGet(this,_MarkdownEditor_editor,"f")&&__classPrivateFieldGet(this,_MarkdownEditor_instances,"m",_MarkdownEditor_scrollToAnchor).call(this)}get milkdownElm(){return this.container.firstElementChild}get editor(){return __classPrivateFieldGet(this,_MarkdownEditor_editor,"f")}constructor(){super(),_MarkdownEditor_instances.add(this),_MarkdownEditor_content.set(this,void 0),this.maxContentWidth=800,this.contentAlignment="left",this.readOnly=!1,this.spellcheck=!1,_MarkdownEditor_mouseIn.set(this,!1),_MarkdownEditor_scrollTop.set(this,0),_MarkdownEditor_anchor.set(this,void 0),_MarkdownEditor_editor.set(this,void 0),_MarkdownEditor_creating.set(this,!1),_MarkdownEditor_onScroll.set(this,(()=>{__classPrivateFieldSet(this,_MarkdownEditor_scrollTop,this.milkdownElm?.scrollTop??0,"f"),__classPrivateFieldGet(this,_MarkdownEditor_instances,"m",_MarkdownEditor_closeCodeEditing).call(this)})),this._onChange=(t,e)=>{__classPrivateFieldSet(this,_MarkdownEditor_content,t,"f"),this.dispatchEvent(new events.ChangeEvent({value:t,oldValue:e}))},this.render=()=>html`
2
2
  <style>
3
3
  .ProseMirror.editor { max-width: ${this.maxContentWidth}px; }
4
4
  </style>
5
5
  <div id="container" spellcheck="${this.spellcheck}"></div>
6
6
  <og-slider direction="vertical" hidden></og-slider>
7
- `,dom.on(this,"click","a",((t,e)=>{if(!this.dispatchEvent(new MarkdownLinkEvent({href:e.getAttribute("href"),a:e,origEvent:t})))return void t.preventDefault();const o=e.getAttribute("href");o&&o.startsWith("#")&&(t.preventDefault(),this.scrollToSelector(o))}))}scrollToSelector(t){const e=this.milkdownElm.querySelector(t);e&&(this.milkdownElm.scrollTop=e.offsetTop,this.slider.updatePosition())}connectedCallback(){super.connectedCallback(),this.requestUpdate()}updateWidth(){const t=Math.min(this.maxContentWidth,this.clientWidth);this.milkdownElm.style.width=`${t}px`}firstUpdated(){this.slider.hide(),this.addEventListener("mouseenter",(()=>{__classPrivateFieldSet(this,_MarkdownEditor_mouseIn,!0,"f"),this.updateSlider()})),this.addEventListener("mouseleave",(()=>{__classPrivateFieldSet(this,_MarkdownEditor_mouseIn,!1,"f"),this.updateSlider()})),this.slider.addEventListener("slideEnd",(()=>{this.updateSlider()}))}async updated(t){if(t.has("spellcheck")&&__classPrivateFieldGet(this,_MarkdownEditor_instances,"m",_MarkdownEditor_applySpellcheck).call(this),__classPrivateFieldGet(this,_MarkdownEditor_editor,"f")||__classPrivateFieldGet(this,_MarkdownEditor_creating,"f"))return;__classPrivateFieldSet(this,_MarkdownEditor_creating,!0,"f");const e=this.content;let o;try{o=await createEditor({container:this.container,content:e,readOnly:this.readOnly,onUpload:this.onUpload,onChange:this._onChange,modifyImageUrl:this.modifyImageUrl})}finally{__classPrivateFieldSet(this,_MarkdownEditor_creating,!1,"f")}if(e!==this.content||!this.isConnected)return o.destroy(),dom.empty(this.container),void(this.isConnected&&this.requestUpdate());__classPrivateFieldSet(this,_MarkdownEditor_editor,o,"f"),__classPrivateFieldGet(this,_MarkdownEditor_instances,"m",_MarkdownEditor_applySpellcheck).call(this),this.slider.attachElement(this.milkdownElm),this.milkdownElm.addEventListener("scroll",__classPrivateFieldGet(this,_MarkdownEditor_onScroll,"f")),__classPrivateFieldGet(this,_MarkdownEditor_instances,"m",_MarkdownEditor_restoreScroll).call(this)}updateSlider(){this.slider&&this.milkdownElm&&(this.milkdownElm.scrollHeight>this.milkdownElm.clientHeight&&(__classPrivateFieldGet(this,_MarkdownEditor_mouseIn,"f")||this.slider.sliding)?this.slider.show():this.slider.hide())}disconnectedCallback(){super.disconnectedCallback(),__classPrivateFieldGet(this,_MarkdownEditor_editor,"f")?.destroy(),__classPrivateFieldSet(this,_MarkdownEditor_editor,null,"f"),dom.empty(this.container)}layout(){}};_MarkdownEditor_content=new WeakMap,_MarkdownEditor_mouseIn=new WeakMap,_MarkdownEditor_scrollTop=new WeakMap,_MarkdownEditor_editor=new WeakMap,_MarkdownEditor_creating=new WeakMap,_MarkdownEditor_onScroll=new WeakMap,_MarkdownEditor_instances=new WeakSet,_MarkdownEditor_closeCodeEditing=function(){const t=this.milkdownElm;t&&(t.querySelectorAll("og-code-block").forEach((t=>t.endEdit())),t.querySelectorAll("og-markdown-html-block").forEach((t=>{t.editing&&(t.editor?.model&&t.commit(),t.editor?.endEdit())})))},_MarkdownEditor_restoreScroll=function(){const t=this.milkdownElm,e=t?.firstElementChild,o=__classPrivateFieldGet(this,_MarkdownEditor_scrollTop,"f");if(!e||!o)return;let r=-1;const i=()=>!(!e.isConnected||r>-1&&Math.abs(t.scrollTop-r)>1||(r=Math.max(0,Math.min(o,t.scrollHeight-t.clientHeight)),t.scrollTop=r,this.slider.value=r,this.slider.updatePosition(),0));t.style.overflowAnchor="none",e.style.overflowAnchor="none";const d=new ResizeObserver((()=>{i()||n()})),n=()=>{clearTimeout(s),d.disconnect(),t.style.overflowAnchor="",e.style.overflowAnchor=""},s=setTimeout(n,2e3);i(),d.observe(e)},_MarkdownEditor_applySpellcheck=function(){this.milkdownElm?.setAttribute("spellcheck",String(this.spellcheck))},MarkdownEditor.styles=[style,...styles],__decorate([property({type:String})],MarkdownEditor.prototype,"content",null),__decorate([property({type:Number})],MarkdownEditor.prototype,"maxContentWidth",void 0),__decorate([property({type:String,reflect:!0})],MarkdownEditor.prototype,"contentAlignment",void 0),__decorate([property({type:Boolean,reflect:!0})],MarkdownEditor.prototype,"readOnly",void 0),__decorate([property({type:Boolean})],MarkdownEditor.prototype,"spellcheck",void 0),__decorate([query("#container")],MarkdownEditor.prototype,"container",void 0),__decorate([query("og-slider")],MarkdownEditor.prototype,"slider",void 0),__decorate([property({type:Object})],MarkdownEditor.prototype,"onUpload",void 0),__decorate([property({type:Object})],MarkdownEditor.prototype,"modifyImageUrl",void 0),MarkdownEditor=__decorate([customElement("og-markdown-editor")],MarkdownEditor);export{MarkdownEditor};
7
+ `,dom.on(this,"click","a",((t,e)=>{const o=e.getAttribute(LINK_HREF_ATTRIBUTE)??e.getAttribute("href");this.dispatchEvent(new MarkdownLinkEvent({href:o,a:e,origEvent:t}))?o&&o.startsWith("#")&&(t.preventDefault(),this.scrollToSelector(o)):t.preventDefault()}))}scrollToSelector(t){const e=this.container?.firstElementChild,o=__classPrivateFieldGet(this,_MarkdownEditor_instances,"m",_MarkdownEditor_findElement).call(this,t);o&&(e.scrollTop+=o.getBoundingClientRect().top-e.getBoundingClientRect().top,this.slider.updatePosition())}connectedCallback(){super.connectedCallback(),this.requestUpdate()}updateWidth(){const t=Math.min(this.maxContentWidth,this.clientWidth);this.milkdownElm.style.width=`${t}px`}firstUpdated(){this.slider.hide(),this.addEventListener("mouseenter",(()=>{__classPrivateFieldSet(this,_MarkdownEditor_mouseIn,!0,"f"),this.updateSlider()})),this.addEventListener("mouseleave",(()=>{__classPrivateFieldSet(this,_MarkdownEditor_mouseIn,!1,"f"),this.updateSlider()})),this.slider.addEventListener("slideEnd",(()=>{this.updateSlider()}))}async updated(t){if(t.has("spellcheck")&&__classPrivateFieldGet(this,_MarkdownEditor_instances,"m",_MarkdownEditor_applySpellcheck).call(this),__classPrivateFieldGet(this,_MarkdownEditor_editor,"f")||__classPrivateFieldGet(this,_MarkdownEditor_creating,"f"))return;__classPrivateFieldSet(this,_MarkdownEditor_creating,!0,"f");const e=this.content;let o;try{o=await createEditor({container:this.container,content:e,readOnly:this.readOnly,onUpload:this.onUpload,onChange:this._onChange,modifyImageUrl:this.modifyImageUrl,modifyLinkUrl:this.modifyLinkUrl})}finally{__classPrivateFieldSet(this,_MarkdownEditor_creating,!1,"f")}if(e!==this.content||!this.isConnected)return o.destroy(),dom.empty(this.container),void(this.isConnected&&this.requestUpdate());__classPrivateFieldSet(this,_MarkdownEditor_editor,o,"f"),__classPrivateFieldGet(this,_MarkdownEditor_instances,"m",_MarkdownEditor_applySpellcheck).call(this),this.slider.attachElement(this.milkdownElm),this.milkdownElm.addEventListener("scroll",__classPrivateFieldGet(this,_MarkdownEditor_onScroll,"f")),__classPrivateFieldGet(this,_MarkdownEditor_anchor,"f")?__classPrivateFieldGet(this,_MarkdownEditor_instances,"m",_MarkdownEditor_scrollToAnchor).call(this):__classPrivateFieldGet(this,_MarkdownEditor_instances,"m",_MarkdownEditor_restoreScroll).call(this),this.dispatchEvent(new Event("markdown.ready",{bubbles:!0,composed:!0}))}updateSlider(){this.slider&&this.milkdownElm&&(this.milkdownElm.scrollHeight>this.milkdownElm.clientHeight&&(__classPrivateFieldGet(this,_MarkdownEditor_mouseIn,"f")||this.slider.sliding)?this.slider.show():this.slider.hide())}disconnectedCallback(){super.disconnectedCallback(),__classPrivateFieldGet(this,_MarkdownEditor_editor,"f")?.destroy(),__classPrivateFieldSet(this,_MarkdownEditor_editor,null,"f"),dom.empty(this.container)}layout(){}};_MarkdownEditor_content=new WeakMap,_MarkdownEditor_mouseIn=new WeakMap,_MarkdownEditor_scrollTop=new WeakMap,_MarkdownEditor_anchor=new WeakMap,_MarkdownEditor_editor=new WeakMap,_MarkdownEditor_creating=new WeakMap,_MarkdownEditor_onScroll=new WeakMap,_MarkdownEditor_instances=new WeakSet,_MarkdownEditor_closeCodeEditing=function(){const t=this.milkdownElm;t&&(t.querySelectorAll("og-code-block").forEach((t=>t.endEdit())),t.querySelectorAll("og-markdown-html-block").forEach((t=>{t.editing&&(t.editor?.model&&t.commit(),t.editor?.endEdit())})))},_MarkdownEditor_restoreScroll=function(){const t=__classPrivateFieldGet(this,_MarkdownEditor_scrollTop,"f");t&&__classPrivateFieldGet(this,_MarkdownEditor_instances,"m",_MarkdownEditor_settleScroll).call(this,(()=>t))},_MarkdownEditor_scrollToAnchor=function(){const t=__classPrivateFieldGet(this,_MarkdownEditor_anchor,"f");__classPrivateFieldSet(this,_MarkdownEditor_anchor,null,"f"),t&&__classPrivateFieldGet(this,_MarkdownEditor_instances,"m",_MarkdownEditor_settleScroll).call(this,(()=>{const e=this.milkdownElm,o=__classPrivateFieldGet(this,_MarkdownEditor_instances,"m",_MarkdownEditor_findElement).call(this,`#${t}`);return o?e.scrollTop+o.getBoundingClientRect().top-e.getBoundingClientRect().top:null}))},_MarkdownEditor_settleScroll=function(t){const e=this.milkdownElm,o=e?.firstElementChild;if(!o)return;let r=-1;const i=()=>{if(!o.isConnected)return!1;if(r>-1&&Math.abs(e.scrollTop-r)>1)return!1;const i=t();return null!=i&&(e.scrollTop=Math.max(0,i),r=e.scrollTop,this.slider.value=r,this.slider.updatePosition(),!0)};e.style.overflowAnchor="none",o.style.overflowAnchor="none";const n=new ResizeObserver((()=>{i()||d()})),d=()=>{clearTimeout(s),n.disconnect(),e.style.overflowAnchor="",o.style.overflowAnchor=""},s=setTimeout(d,2e3);i()?n.observe(o):d()},_MarkdownEditor_findElement=function(t){const e=this.container?.firstElementChild;if(!e||!t)return null;if(!t.startsWith("#"))return e.querySelector(t);let o=t.slice(1);try{o=decodeURIComponent(o)}catch{}return e.querySelector(`#${CSS.escape(o)}`)??e.querySelector(`[name="${CSS.escape(o)}"]`)},_MarkdownEditor_applySpellcheck=function(){this.milkdownElm?.setAttribute("spellcheck",String(this.spellcheck))},MarkdownEditor.styles=[style,...styles],__decorate([property({type:String})],MarkdownEditor.prototype,"content",null),__decorate([property({type:Number})],MarkdownEditor.prototype,"maxContentWidth",void 0),__decorate([property({type:String,reflect:!0})],MarkdownEditor.prototype,"contentAlignment",void 0),__decorate([property({type:Boolean,reflect:!0})],MarkdownEditor.prototype,"readOnly",void 0),__decorate([property({type:Boolean})],MarkdownEditor.prototype,"spellcheck",void 0),__decorate([query("#container")],MarkdownEditor.prototype,"container",void 0),__decorate([query("og-slider")],MarkdownEditor.prototype,"slider",void 0),__decorate([property({type:Object})],MarkdownEditor.prototype,"onUpload",void 0),__decorate([property({type:Object})],MarkdownEditor.prototype,"modifyImageUrl",void 0),__decorate([property({type:Object})],MarkdownEditor.prototype,"modifyLinkUrl",void 0),__decorate([property({type:String,noAccessor:!0})],MarkdownEditor.prototype,"anchor",null),MarkdownEditor=__decorate([customElement("og-markdown-editor")],MarkdownEditor);export{MarkdownEditor};
@@ -1 +1 @@
1
- {"version":3,"file":"markdownEditor.js","sourceRoot":"","sources":["../../src/components/markdownEditor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,EAAsC,MAAM,iBAAiB,CAAC;AAElF,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,KAAK,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,KAA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BAAoB,CAAC;GACvB,cAAc,CAgP1B","sourcesContent":["import { dom, events, HorizontalPosition, Layout, Slider } from '@omegagrid/core';\nimport { CodeBlock } from '@omegagrid/code';\nimport { LitElement, html } from 'lit';\nimport { customElement, query, property } from 'lit/decorators.js';\nimport { style } from './markdownEditor.style';\nimport styles from './styles';\nimport { createEditor } from '../utils';\nimport { HtmlBlockComponent } from './features/htmlBlock';\nimport type * as milkdown from '@omegagrid/milkdown-kit';\n\n// how long the restored scroll offset is held while the content renders\nconst SCROLL_SETTLE_TIMEOUT = 2000;\n\nexport class MarkdownLinkEvent extends Event {\n\treadonly href: string;\n\treadonly a: HTMLAnchorElement;\n\treadonly origEvent: PointerEvent;\n\n\tconstructor(args: Partial<MarkdownLinkEvent>) {\n\t\tsuper(`markdown.link`, {bubbles: true, composed: true, cancelable: true});\n\t\tObject.assign(this, args);\n\t}\n}\n\n@customElement('og-markdown-editor')\nexport class MarkdownEditor extends LitElement implements Layout {\n\n\tstatic styles = [style, ...styles];\n\n\t#content: string\n\t@property({type: String})\n\tget content() { return this.#content }\n\tset content(val: string) {\n\t\tif (val !== this.#content) this.#scrollTop = 0; // another document starts at the top\n\t\tthis.#content = val;\n\t\tthis.#editor?.destroy();\n\t\tthis.#editor = null;\n\t\tthis.requestUpdate();\n\t}\n\n\t@property({type: Number})\n\tmaxContentWidth = 800;\n\n\t@property({type: String, reflect: true})\n\tcontentAlignment: HorizontalPosition = 'left';\n\n\t@property({type: Boolean, reflect: true})\n\treadOnly = false;\n\n\t@property({type: Boolean})\n\tspellcheck = false;\n\n\t@query('#container')\n\tcontainer: HTMLDivElement;\n\n\t@query('og-slider')\n\tslider: Slider;\n\n\t#mouseIn = false;\n\t#scrollTop = 0;\n\n\t@property({type: Object})\n\tonUpload: (file: File) => Promise<string>;\n\n\t@property({type: Object})\n\tmodifyImageUrl: (file: string) => Promise<string>|string;\n\n\t#editor: milkdown.core.Editor;\n\t#creating = false;\n\n\tget milkdownElm () { return this.container.firstElementChild as HTMLElement }\n\tget editor() { return this.#editor }\n\n\tconstructor() {\n\t\tsuper();\n\t\tdom.on(this, 'click', 'a', (e: PointerEvent, a) => {\n\t\t\tconst canceled = !this.dispatchEvent(new MarkdownLinkEvent({\n\t\t\t\thref: a.getAttribute('href'), \n\t\t\t\ta: a as HTMLAnchorElement,\n\t\t\t\torigEvent: e\n\t\t\t}));\n\n\t\t\tif (canceled) {\n\t\t\t\te.preventDefault();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst href = a.getAttribute('href');\n\t\t\tif (href && href.startsWith('#')) {\n\t\t\t\te.preventDefault();\n\t\t\t\tthis.scrollToSelector(href);\n\t\t\t}\n\t\t});\n\t}\n\n\t#onScroll = () => {\n\t\tthis.#scrollTop = this.milkdownElm?.scrollTop ?? 0;\n\t\tthis.#closeCodeEditing();\n\t}\n\n\t// Monaco renders into an overlay pinned to the document, so an open code editor\n\t// cannot travel with the scrolled content - close it instead of leaving it\n\t// standing over the rest of the document.\n\t#closeCodeEditing() {\n\t\tconst elm = this.milkdownElm;\n\t\tif (!elm) return;\n\n\t\telm.querySelectorAll<CodeBlock>('og-code-block').forEach(block => block.endEdit());\n\n\t\telm.querySelectorAll<HtmlBlockComponent>('og-markdown-html-block').forEach(block => {\n\t\t\tif (!block.editing) return;\n\t\t\t// the html block keeps its value in the code editor, let it save first\n\t\t\tif (block.editor?.model) block.commit();\n\t\t\tblock.editor?.endEdit();\n\t\t});\n\t}\n\n\t// The editor is destroyed on disconnect and rebuilt on reconnect (switching\n\t// tabs takes the component out of the DOM), which puts the fresh content back\n\t// to the top - put it back where the user left off.\n\t#restoreScroll() {\n\t\tconst elm = this.milkdownElm;\n\t\tconst content = elm?.firstElementChild as HTMLElement;\n\t\tconst target = this.#scrollTop;\n\t\tif (!content || !target) return;\n\n\t\tlet applied = -1;\n\t\tconst apply = () => {\n\t\t\tif (!content.isConnected) return false; // the editor is gone\n\t\t\tif (applied > -1 && Math.abs(elm.scrollTop - applied) > 1) return false; // the user took over\n\t\t\tapplied = Math.max(0, Math.min(target, elm.scrollHeight - elm.clientHeight));\n\t\t\telm.scrollTop = applied;\n\t\t\tthis.slider.value = applied;\n\t\t\tthis.slider.updatePosition();\n\t\t\treturn true;\n\t\t};\n\n\t\t// Images, code blocks and the like render asynchronously and keep growing\n\t\t// the content, so hold the offset until it settles. Scroll anchoring would\n\t\t// shift it along with the growing content, so turn it off while we do.\n\t\telm.style.overflowAnchor = 'none';\n\t\tcontent.style.overflowAnchor = 'none';\n\n\t\tconst observer = new ResizeObserver(() => { if (!apply()) stop() });\n\t\tconst stop = () => {\n\t\t\tclearTimeout(timer);\n\t\t\tobserver.disconnect();\n\t\t\telm.style.overflowAnchor = '';\n\t\t\tcontent.style.overflowAnchor = '';\n\t\t};\n\t\tconst timer = setTimeout(stop, SCROLL_SETTLE_TIMEOUT);\n\n\t\tapply();\n\t\tobserver.observe(content);\n\t}\n\n\tscrollToSelector(selector: string) {\n\t\tconst elm = this.milkdownElm.querySelector(selector) as HTMLElement;\n\t\tif (!elm) return;\n\t\tthis.milkdownElm.scrollTop = elm.offsetTop;\n\t\tthis.slider.updatePosition();\n\t}\n\n\tconnectedCallback() {\n\t\tsuper.connectedCallback();\n\t\tthis.requestUpdate();\n\t}\n\n\tupdateWidth() {\n\t\tconst width = Math.min(this.maxContentWidth, this.clientWidth);\n\t\tthis.milkdownElm.style.width = `${width}px`;\n\t}\n\n\t_onChange = (markdown: string, prevMarkdown: string) => {\n\t\tthis.#content = markdown;\n\t\tthis.dispatchEvent(new events.ChangeEvent({value: markdown, oldValue: prevMarkdown}));\n\t}\n\n\tfirstUpdated() {\n\t\tthis.slider.hide();\n\n\t\tthis.addEventListener('mouseenter', () => {\n\t\t\tthis.#mouseIn = true;\n\t\t\tthis.updateSlider();\n\t\t});\n\n\t\tthis.addEventListener('mouseleave', () => {\n\t\t\tthis.#mouseIn = false;\n\t\t\tthis.updateSlider();\n\t\t});\n\n\t\tthis.slider.addEventListener('slideEnd', () => {\n\t\t\tthis.updateSlider();\n\t\t});\n\t}\n\n\tasync updated(changed: Map<PropertyKey, unknown>) {\n\t\tif (changed.has('spellcheck')) this.#applySpellcheck();\n\t\t// Only one editor may be under construction at a time. createEditor() is async\n\t\t// (it may still be downloading Milkdown), so a content change arriving meanwhile\n\t\t// must not start a second instance in the same container.\n\t\tif (this.#editor || this.#creating) return;\n\t\tthis.#creating = true;\n\t\tconst content = this.content;\n\t\tlet editor: milkdown.core.Editor;\n\t\ttry {\n\t\t\teditor = await createEditor({\n\t\t\t\tcontainer: this.container,\n\t\t\t\tcontent,\n\t\t\t\treadOnly: this.readOnly,\n\t\t\t\tonUpload: this.onUpload,\n\t\t\t\tonChange: this._onChange,\n\t\t\t\tmodifyImageUrl: this.modifyImageUrl\n\t\t\t});\n\t\t} finally {\n\t\t\tthis.#creating = false;\n\t\t}\n\t\t// The content changed (or the element was detached) while the editor was being\n\t\t// created: this instance is stale. Drop it and let the next update build a fresh one.\n\t\tif (content !== this.content || !this.isConnected) {\n\t\t\teditor.destroy();\n\t\t\tdom.empty(this.container);\n\t\t\tif (this.isConnected) this.requestUpdate();\n\t\t\treturn;\n\t\t}\n\t\tthis.#editor = editor;\n\t\tthis.#applySpellcheck();\n\t\tthis.slider.attachElement(this.milkdownElm);\n\t\tthis.milkdownElm.addEventListener('scroll', this.#onScroll);\n\t\tthis.#restoreScroll();\n\t}\n\n\t// The browser only re-checks spelling on the element it edits, so set the\n\t// attribute on the ProseMirror editable itself (not just the container).\n\t#applySpellcheck() {\n\t\tthis.milkdownElm?.setAttribute('spellcheck', String(this.spellcheck));\n\t}\n\n\tupdateSlider() {\n\t\tif (!this.slider || !this.milkdownElm) return;\n\t\tconst needsScroll = this.milkdownElm.scrollHeight > this.milkdownElm.clientHeight;\n\t\tif (needsScroll && (this.#mouseIn || this.slider.sliding)) {\n\t\t\tthis.slider.show();\n\t\t} else {\n\t\t\tthis.slider.hide();\n\t\t}\n\t}\n\n\tdisconnectedCallback() {\n\t\tsuper.disconnectedCallback();\n\t\tthis.#editor?.destroy();\n\t\tthis.#editor = null;\n\t\tdom.empty(this.container);\n\t}\n\n\trender = () => html`\n\t\t<style>\n\t\t\t.ProseMirror.editor { max-width: ${this.maxContentWidth}px; }\n\t\t</style>\n\t\t<div id=\"container\" spellcheck=\"${this.spellcheck}\"></div>\n\t\t<og-slider direction=\"vertical\" hidden></og-slider>\n\t`;\n\n\tlayout() { }\n\n}"]}
1
+ {"version":3,"file":"markdownEditor.js","sourceRoot":"","sources":["../../src/components/markdownEditor.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,EAAsC,MAAM,iBAAiB,CAAC;AAElF,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,KAAK,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AAC/C,OAAO,MAAM,MAAM,UAAU,CAAC;AAC9B,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAI7D,wEAAwE;AACxE,MAAM,qBAAqB,GAAG,IAAI,CAAC;AAEnC,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAK3C,YAAY,IAAgC;QAC3C,KAAK,CAAC,eAAe,EAAE,EAAC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAC,CAAC,CAAC;QAC1E,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAC3B,CAAC;CACD;AAGM,IAAM,cAAc,GAApB,MAAM,cAAe,SAAQ,UAAU;IAM7C,IAAI,OAAO,KAAK,OAAO,uBAAA,IAAI,+BAAS,CAAA,CAAC,CAAC;IACtC,IAAI,OAAO,CAAC,GAAW;QACtB,IAAI,GAAG,KAAK,uBAAA,IAAI,+BAAS;YAAE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;IADC,KAAK,CAAC,YAAY,CAAC;iDACM;AAG1B;IADC,KAAK,CAAC,WAAW,CAAC;8CACJ;AAMf;IADC,QAAQ,CAAC,EAAC,IAAI,EAAE,MAAM,EAAC,CAAC;gDACiB;AAG1C;IADC,QAAQ,CAAC,EAAC,IAAI,EAAE,MAAM,EAAC,CAAC;sDACgC;AAQzD;IADC,QAAQ,CAAC,EAAC,IAAI,EAAE,MAAM,EAAC,CAAC;qDACe;AASxC;IADC,QAAQ,CAAC,EAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,EAAC,CAAC;4CACP;AAzDxB,cAAc;IAD1B,aAAa,CAAC,oBAAoB,CAAC;GACvB,cAAc,CAgT1B","sourcesContent":["import { dom, events, HorizontalPosition, Layout, Slider } from '@omegagrid/core';\nimport { CodeBlock } from '@omegagrid/code';\nimport { LitElement, html } from 'lit';\nimport { customElement, query, property } from 'lit/decorators.js';\nimport { style } from './markdownEditor.style';\nimport styles from './styles';\nimport { createEditor, LINK_HREF_ATTRIBUTE } from '../utils';\nimport { HtmlBlockComponent } from './features/htmlBlock';\nimport type * as milkdown from '@omegagrid/milkdown-kit';\n\n// how long the restored scroll offset is held while the content renders\nconst SCROLL_SETTLE_TIMEOUT = 2000;\n\nexport class MarkdownLinkEvent extends Event {\n\treadonly href: string;\n\treadonly a: HTMLAnchorElement;\n\treadonly origEvent: PointerEvent;\n\n\tconstructor(args: Partial<MarkdownLinkEvent>) {\n\t\tsuper(`markdown.link`, {bubbles: true, composed: true, cancelable: true});\n\t\tObject.assign(this, args);\n\t}\n}\n\n@customElement('og-markdown-editor')\nexport class MarkdownEditor extends LitElement implements Layout {\n\n\tstatic styles = [style, ...styles];\n\n\t#content: string\n\t@property({type: String})\n\tget content() { return this.#content }\n\tset content(val: string) {\n\t\tif (val !== this.#content) this.#scrollTop = 0; // another document starts at the top\n\t\tthis.#content = val;\n\t\tthis.#editor?.destroy();\n\t\tthis.#editor = null;\n\t\tthis.requestUpdate();\n\t}\n\n\t@property({type: Number})\n\tmaxContentWidth = 800;\n\n\t@property({type: String, reflect: true})\n\tcontentAlignment: HorizontalPosition = 'left';\n\n\t@property({type: Boolean, reflect: true})\n\treadOnly = false;\n\n\t@property({type: Boolean})\n\tspellcheck = false;\n\n\t@query('#container')\n\tcontainer: HTMLDivElement;\n\n\t@query('og-slider')\n\tslider: Slider;\n\n\t#mouseIn = false;\n\t#scrollTop = 0;\n\n\t@property({type: Object})\n\tonUpload: (file: File) => Promise<string>;\n\n\t@property({type: Object})\n\tmodifyImageUrl: (file: string) => Promise<string>|string;\n\n\t/**\n\t * Rewrites the href the browser sees for a link (e.g. a page name into the address of that page).\n\t * The markdown keeps the destination as written and MarkdownLinkEvent.href reports it; the rewritten\n\t * address is what a copied link or a middle click gets. Applied when the editor is (re)created.\n\t */\n\t@property({type: Object})\n\tmodifyLinkUrl: (href: string) => string;\n\n\t#anchor: string;\n\t/**\n\t * The id of the element the content scrolls to once it has rendered (a link or an address with a\n\t * fragment), kept in view while the content settles. Set on a rendered document it scrolls right\n\t * away; it is consumed by the scroll, so a later reload of the content starts at the top.\n\t */\n\t@property({type: String, noAccessor: true})\n\tget anchor() { return this.#anchor }\n\tset anchor(value: string) {\n\t\tthis.#anchor = value;\n\t\tif (value && this.#editor) this.#scrollToAnchor();\n\t}\n\n\t#editor: milkdown.core.Editor;\n\t#creating = false;\n\n\tget milkdownElm () { return this.container.firstElementChild as HTMLElement }\n\tget editor() { return this.#editor }\n\n\tconstructor() {\n\t\tsuper();\n\t\tdom.on(this, 'click', 'a', (e: PointerEvent, a) => {\n\t\t\t// the destination written in the markdown, whatever modifyLinkUrl made of the href\n\t\t\tconst href = a.getAttribute(LINK_HREF_ATTRIBUTE) ?? a.getAttribute('href');\n\t\t\tconst canceled = !this.dispatchEvent(new MarkdownLinkEvent({\n\t\t\t\thref,\n\t\t\t\ta: a as HTMLAnchorElement,\n\t\t\t\torigEvent: e\n\t\t\t}));\n\n\t\t\tif (canceled) {\n\t\t\t\te.preventDefault();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (href && href.startsWith('#')) {\n\t\t\t\te.preventDefault();\n\t\t\t\tthis.scrollToSelector(href);\n\t\t\t}\n\t\t});\n\t}\n\n\t#onScroll = () => {\n\t\tthis.#scrollTop = this.milkdownElm?.scrollTop ?? 0;\n\t\tthis.#closeCodeEditing();\n\t}\n\n\t// Monaco renders into an overlay pinned to the document, so an open code editor\n\t// cannot travel with the scrolled content - close it instead of leaving it\n\t// standing over the rest of the document.\n\t#closeCodeEditing() {\n\t\tconst elm = this.milkdownElm;\n\t\tif (!elm) return;\n\n\t\telm.querySelectorAll<CodeBlock>('og-code-block').forEach(block => block.endEdit());\n\n\t\telm.querySelectorAll<HtmlBlockComponent>('og-markdown-html-block').forEach(block => {\n\t\t\tif (!block.editing) return;\n\t\t\t// the html block keeps its value in the code editor, let it save first\n\t\t\tif (block.editor?.model) block.commit();\n\t\t\tblock.editor?.endEdit();\n\t\t});\n\t}\n\n\t// The editor is destroyed on disconnect and rebuilt on reconnect (switching\n\t// tabs takes the component out of the DOM), which puts the fresh content back\n\t// to the top - put it back where the user left off.\n\t#restoreScroll() {\n\t\tconst target = this.#scrollTop;\n\t\tif (!target) return;\n\t\tthis.#settleScroll(() => target);\n\t}\n\n\t// Scrolls to the anchor set (an element id) and consumes it: a navigation intent, applied once\n\t// the content is in the DOM and held while the content settles - a later reload starts at the top.\n\t#scrollToAnchor() {\n\t\tconst anchor = this.#anchor;\n\t\tthis.#anchor = null;\n\t\tif (!anchor) return;\n\t\tthis.#settleScroll(() => {\n\t\t\tconst elm = this.milkdownElm;\n\t\t\tconst target = this.#findElement(`#${anchor}`);\n\t\t\tif (!target) return null;\n\t\t\treturn elm.scrollTop + target.getBoundingClientRect().top - elm.getBoundingClientRect().top;\n\t\t});\n\t}\n\n\t// Scrolls to the offset the callback gives (null = nothing to scroll to) and keeps it there\n\t// while the content settles. Images, code blocks and the like render asynchronously and keep\n\t// growing the content, so the offset is applied again on every resize until the content settles\n\t// or the user scrolls. Scroll anchoring would shift the offset along with the growing content,\n\t// so it is turned off meanwhile.\n\t#settleScroll(position: () => number | null) {\n\t\tconst elm = this.milkdownElm;\n\t\tconst content = elm?.firstElementChild as HTMLElement;\n\t\tif (!content) return;\n\n\t\tlet applied = -1;\n\t\tconst apply = () => {\n\t\t\tif (!content.isConnected) return false; // the editor is gone\n\t\t\tif (applied > -1 && Math.abs(elm.scrollTop - applied) > 1) return false; // the user took over\n\t\t\tconst target = position();\n\t\t\tif (target == null) return false;\n\t\t\telm.scrollTop = Math.max(0, target);\n\t\t\t// what the browser could apply: the content may still be too short for the target (or not laid out yet)\n\t\t\tapplied = elm.scrollTop;\n\t\t\tthis.slider.value = applied;\n\t\t\tthis.slider.updatePosition();\n\t\t\treturn true;\n\t\t};\n\n\t\telm.style.overflowAnchor = 'none';\n\t\tcontent.style.overflowAnchor = 'none';\n\n\t\tconst observer = new ResizeObserver(() => { if (!apply()) stop() });\n\t\tconst stop = () => {\n\t\t\tclearTimeout(timer);\n\t\t\tobserver.disconnect();\n\t\t\telm.style.overflowAnchor = '';\n\t\t\tcontent.style.overflowAnchor = '';\n\t\t};\n\t\tconst timer = setTimeout(stop, SCROLL_SETTLE_TIMEOUT);\n\n\t\tif (!apply()) { stop(); return; }\n\t\tobserver.observe(content);\n\t}\n\n\t// The element a selector names: an anchor (`#id`, the id as a link or an address writes it -\n\t// percent-encoded, with diacritics or a leading digit) or any selector.\n\t#findElement(selector: string): HTMLElement | null {\n\t\tconst container = this.container?.firstElementChild as HTMLElement;\n\t\tif (!container || !selector) return null;\n\t\tif (!selector.startsWith('#')) return container.querySelector(selector);\n\t\tlet id = selector.slice(1);\n\t\ttry { id = decodeURIComponent(id); } catch { /* not percent-encoded, taken as written */ }\n\t\treturn container.querySelector(`#${CSS.escape(id)}`) ?? container.querySelector(`[name=\"${CSS.escape(id)}\"]`);\n\t}\n\n\t/** Scrolls the rendered content to an element: an anchor (`#id`, see #findElement) or any selector. */\n\tscrollToSelector(selector: string) {\n\t\tconst container = this.container?.firstElementChild as HTMLElement;\n\t\tconst elm = this.#findElement(selector);\n\t\tif (!elm) return;\n\t\t// the offset within the scroll container itself: offsetTop would be relative to the positioned content element\n\t\tcontainer.scrollTop += elm.getBoundingClientRect().top - container.getBoundingClientRect().top;\n\t\tthis.slider.updatePosition();\n\t}\n\n\tconnectedCallback() {\n\t\tsuper.connectedCallback();\n\t\tthis.requestUpdate();\n\t}\n\n\tupdateWidth() {\n\t\tconst width = Math.min(this.maxContentWidth, this.clientWidth);\n\t\tthis.milkdownElm.style.width = `${width}px`;\n\t}\n\n\t_onChange = (markdown: string, prevMarkdown: string) => {\n\t\tthis.#content = markdown;\n\t\tthis.dispatchEvent(new events.ChangeEvent({value: markdown, oldValue: prevMarkdown}));\n\t}\n\n\tfirstUpdated() {\n\t\tthis.slider.hide();\n\n\t\tthis.addEventListener('mouseenter', () => {\n\t\t\tthis.#mouseIn = true;\n\t\t\tthis.updateSlider();\n\t\t});\n\n\t\tthis.addEventListener('mouseleave', () => {\n\t\t\tthis.#mouseIn = false;\n\t\t\tthis.updateSlider();\n\t\t});\n\n\t\tthis.slider.addEventListener('slideEnd', () => {\n\t\t\tthis.updateSlider();\n\t\t});\n\t}\n\n\tasync updated(changed: Map<PropertyKey, unknown>) {\n\t\tif (changed.has('spellcheck')) this.#applySpellcheck();\n\t\t// Only one editor may be under construction at a time. createEditor() is async\n\t\t// (it may still be downloading Milkdown), so a content change arriving meanwhile\n\t\t// must not start a second instance in the same container.\n\t\tif (this.#editor || this.#creating) return;\n\t\tthis.#creating = true;\n\t\tconst content = this.content;\n\t\tlet editor: milkdown.core.Editor;\n\t\ttry {\n\t\t\teditor = await createEditor({\n\t\t\t\tcontainer: this.container,\n\t\t\t\tcontent,\n\t\t\t\treadOnly: this.readOnly,\n\t\t\t\tonUpload: this.onUpload,\n\t\t\t\tonChange: this._onChange,\n\t\t\t\tmodifyImageUrl: this.modifyImageUrl,\n\t\t\t\tmodifyLinkUrl: this.modifyLinkUrl\n\t\t\t});\n\t\t} finally {\n\t\t\tthis.#creating = false;\n\t\t}\n\t\t// The content changed (or the element was detached) while the editor was being\n\t\t// created: this instance is stale. Drop it and let the next update build a fresh one.\n\t\tif (content !== this.content || !this.isConnected) {\n\t\t\teditor.destroy();\n\t\t\tdom.empty(this.container);\n\t\t\tif (this.isConnected) this.requestUpdate();\n\t\t\treturn;\n\t\t}\n\t\tthis.#editor = editor;\n\t\tthis.#applySpellcheck();\n\t\tthis.slider.attachElement(this.milkdownElm);\n\t\tthis.milkdownElm.addEventListener('scroll', this.#onScroll);\n\t\tif (this.#anchor) this.#scrollToAnchor();\n\t\telse this.#restoreScroll();\n\t\t// the content is in the DOM: anchors can be scrolled to, elements queried\n\t\tthis.dispatchEvent(new Event('markdown.ready', {bubbles: true, composed: true}));\n\t}\n\n\t// The browser only re-checks spelling on the element it edits, so set the\n\t// attribute on the ProseMirror editable itself (not just the container).\n\t#applySpellcheck() {\n\t\tthis.milkdownElm?.setAttribute('spellcheck', String(this.spellcheck));\n\t}\n\n\tupdateSlider() {\n\t\tif (!this.slider || !this.milkdownElm) return;\n\t\tconst needsScroll = this.milkdownElm.scrollHeight > this.milkdownElm.clientHeight;\n\t\tif (needsScroll && (this.#mouseIn || this.slider.sliding)) {\n\t\t\tthis.slider.show();\n\t\t} else {\n\t\t\tthis.slider.hide();\n\t\t}\n\t}\n\n\tdisconnectedCallback() {\n\t\tsuper.disconnectedCallback();\n\t\tthis.#editor?.destroy();\n\t\tthis.#editor = null;\n\t\tdom.empty(this.container);\n\t}\n\n\trender = () => html`\n\t\t<style>\n\t\t\t.ProseMirror.editor { max-width: ${this.maxContentWidth}px; }\n\t\t</style>\n\t\t<div id=\"container\" spellcheck=\"${this.spellcheck}\"></div>\n\t\t<og-slider direction=\"vertical\" hidden></og-slider>\n\t`;\n\n\tlayout() { }\n\n}"]}
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import type * as milkdown from '@omegagrid/milkdown-kit';
2
+ /** The attribute of a rendered <a> that keeps the destination written in the markdown when modifyLinkUrl rewrote its href */
3
+ export declare const LINK_HREF_ATTRIBUTE = "data-md-href";
1
4
  export type CreateEditorOptions = {
2
5
  container?: HTMLElement;
3
6
  content?: string;
@@ -5,7 +8,12 @@ export type CreateEditorOptions = {
5
8
  onUpload?: (file: File) => Promise<string>;
6
9
  onChange?: (markdown: string, prevMarkdown: string) => void;
7
10
  modifyImageUrl?: (file: string) => Promise<string> | string;
11
+ /**
12
+ * Rewrites the href the browser sees for a link, e.g. a page name into the address of that page. The
13
+ * markdown keeps the destination as written; the rendered <a> carries it in LINK_HREF_ATTRIBUTE.
14
+ */
15
+ modifyLinkUrl?: (href: string) => string;
8
16
  };
9
- export declare function createEditor(options?: CreateEditorOptions): Promise<import("@milkdown/core").Editor>;
17
+ export declare function createEditor(options?: CreateEditorOptions): Promise<milkdown.core.Editor>;
10
18
  export declare function markdownToHtml(markdown: string, options?: Omit<CreateEditorOptions, 'content' | 'readOnly'>): Promise<string>;
11
19
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,mBAAmB,GAAG;IACjC,SAAS,CAAC,EAAE,WAAW,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IAC3C,QAAQ,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5D,cAAc,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,GAAC,MAAM,CAAC;CAC1D,CAAA;AAED,wBAAsB,YAAY,CAAC,OAAO,CAAC,EAAE,mBAAmB,4CAgE/D;AAED,wBAAsB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,mBAAmB,EAAE,SAAS,GAAC,UAAU,CAAC,mBAuB/G"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,QAAQ,MAAM,yBAAyB,CAAC;AAEzD,6HAA6H;AAC7H,eAAO,MAAM,mBAAmB,iBAAiB,CAAC;AAElD,MAAM,MAAM,mBAAmB,GAAG;IACjC,SAAS,CAAC,EAAE,WAAW,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IAC3C,QAAQ,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5D,cAAc,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,GAAC,MAAM,CAAC;IAC1D;;;OAGG;IACH,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;CACzC,CAAA;AAgCD,wBAAsB,YAAY,CAAC,OAAO,CAAC,EAAE,mBAAmB,iCAmE/D;AAED,wBAAsB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,mBAAmB,EAAE,SAAS,GAAC,UAAU,CAAC,mBAuB/G"}
package/dist/utils.js CHANGED
@@ -1 +1 @@
1
- import{MilkdownLoader}from"./loader";import{configureFeatures,getFeatures}from".";export async function createEditor(e){const t=await MilkdownLoader.get(),r=t.core.Editor.make(),n=Array.from(getFeatures(r,t,e).entries()).filter((([e,t])=>t.default));return r.config(configureFeatures(n.map((e=>e[0])),t)).config((r=>{e?.container&&r.set(t.core.rootCtx,e.container),e?.content&&r.set(t.core.defaultValueCtx,e.content),r.update(t.core.editorViewOptionsCtx,(n=>{const o={...n};return e?.readOnly&&(o.editable=()=>!1),e?.onUpload&&(o.handlePaste=(n,o)=>{const i=Array.from(o.clipboardData?.files??[]).filter((e=>e.type.startsWith("image/")));return!!i.length&&(o.preventDefault(),(async()=>{const o=t.components.imageBlock.imageBlockSchema.type(r);for(const t of i){const r=await e.onUpload(t);if(!r)continue;const i=o.create({src:r});n.dispatch(n.state.tr.replaceSelectionWith(i))}})(),!0)}),o})),r.update(t.plugins.indent.indentConfig.key,(e=>({...e,size:4}))),r.get(t.plugins.listener.listenerCtx).markdownUpdated(((t,r,n)=>{e?.onChange&&r!==n&&e.onChange(r,n)}))})).use(t.presets.commonmark.commonmark).use(t.plugins.listener.listener).use(t.plugins.history.history).use(t.plugins.indent.indent).use(t.plugins.trailing.trailing).use(t.plugins.clipboard.clipboard).use(t.presets.gfm.gfm),n.forEach((e=>e[1].init())),await r.create(),r}export async function markdownToHtml(e,t){const r=await createEditor(Object.assign({},t,{content:e,readOnly:!0})),n=MilkdownLoader.getSync(),o=document.createElement("div"),i=r.ctx.get(n.core.schemaCtx),a=r.ctx.get(n.core.editorViewCtx),s=n.prose.model.DOMSerializer.fromSchema(i).serializeFragment(a.state.doc.content);for(const e of s.querySelectorAll("img")){const r=e.getAttribute("src");if(r&&t?.modifyImageUrl){const n=t.modifyImageUrl(r);e.setAttribute("src","string"==typeof n?n:await n)}e.hasAttribute("ratio")&&(e.style.width=100*parseFloat(e.getAttribute("ratio"))+"%")}return o.appendChild(s),r.destroy(),o.innerHTML}
1
+ import{MilkdownLoader}from"./loader";import{configureFeatures,getFeatures}from".";export const LINK_HREF_ATTRIBUTE="data-md-href";function createLinkSchema(e,t){return e.presets.commonmark.linkSchema.extendSchema((e=>r=>{const n=e(r);return{...n,parseDOM:[{tag:"a[href]",getAttrs:e=>({href:e.getAttribute(LINK_HREF_ATTRIBUTE)??e.getAttribute("href"),title:e.getAttribute("title")})}],toDOM:(e,r)=>{const o=n.toDOM?.(e,r)??["a",{...e.attrs}],i=e.attrs.href,a=i?t(i):i;if(!Array.isArray(o)||null==a||a===i)return o;const[s,c,...l]=o;return[s,{...c,href:a,[LINK_HREF_ATTRIBUTE]:i},...l]}}}))}export async function createEditor(e){const t=await MilkdownLoader.get(),r=t.core.Editor.make(),n=Array.from(getFeatures(r,t,e).entries()).filter((([e,t])=>t.default));return r.config(configureFeatures(n.map((e=>e[0])),t)).config((r=>{e?.container&&r.set(t.core.rootCtx,e.container),e?.content&&r.set(t.core.defaultValueCtx,e.content),r.update(t.core.editorViewOptionsCtx,(n=>{const o={...n};return e?.readOnly&&(o.editable=()=>!1),e?.onUpload&&(o.handlePaste=(n,o)=>{const i=Array.from(o.clipboardData?.files??[]).filter((e=>e.type.startsWith("image/")));return!!i.length&&(o.preventDefault(),(async()=>{const o=t.components.imageBlock.imageBlockSchema.type(r);for(const t of i){const r=await e.onUpload(t);if(!r)continue;const i=o.create({src:r});n.dispatch(n.state.tr.replaceSelectionWith(i))}})(),!0)}),o})),r.update(t.plugins.indent.indentConfig.key,(e=>({...e,size:4}))),r.get(t.plugins.listener.listenerCtx).markdownUpdated(((t,r,n)=>{e?.onChange&&r!==n&&e.onChange(r,n)}))})).use(t.presets.commonmark.commonmark).use(t.plugins.listener.listener).use(t.plugins.history.history).use(t.plugins.indent.indent).use(t.plugins.trailing.trailing).use(t.plugins.clipboard.clipboard).use(t.presets.gfm.gfm),e?.modifyLinkUrl&&r.use(createLinkSchema(t,e.modifyLinkUrl)),n.forEach((e=>e[1].init())),await r.create(),r}export async function markdownToHtml(e,t){const r=await createEditor(Object.assign({},t,{content:e,readOnly:!0})),n=MilkdownLoader.getSync(),o=document.createElement("div"),i=r.ctx.get(n.core.schemaCtx),a=r.ctx.get(n.core.editorViewCtx),s=n.prose.model.DOMSerializer.fromSchema(i).serializeFragment(a.state.doc.content);for(const e of s.querySelectorAll("img")){const r=e.getAttribute("src");if(r&&t?.modifyImageUrl){const n=t.modifyImageUrl(r);e.setAttribute("src","string"==typeof n?n:await n)}e.hasAttribute("ratio")&&(e.style.width=100*parseFloat(e.getAttribute("ratio"))+"%")}return o.appendChild(s),r.destroy(),o.innerHTML}
package/dist/utils.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,GAAG,CAAC;AAWnD,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,OAA6B;IAC/D,MAAM,QAAQ,GAAG,MAAM,cAAc,CAAC,GAAG,EAAE,CAAC;IAC5C,MAAM,MAAM,GAAG,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC3C,MAAM,eAAe,GAAG,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;IAEpH,MAAM,CAAC,MAAM,CAAC,iBAAiB,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;SACxE,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE;QACf,IAAI,OAAO,EAAE,SAAS;YAAE,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC;QAC1E,IAAI,OAAO,EAAE,OAAO;YAAE,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,eAAe,EAAE,OA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,MAAM,GAAG,CAAC,CAAC;QACpE,CAAC;QAED,IAAI,GAAG,CAAC,YAAY,CAAC,OAAO,CAAC,EAAE,CAAC;YAC/B,GAAG,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,GAAG,GAAG,UAAU,CAAC,GAAG,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;QACvE,CAAC;IACF,CAAC;IAEE,GAAG,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;IAC7B,MAAM,CAAC,OAAO,EAAE,CAAC;IACd,OAAO,GAAG,CAAC,SAAS,CAAC;AACzB,CAAC","sourcesContent":["import { MilkdownLoader } from \"./loader\";\nimport { configureFeatures, getFeatures } from '.';\n\nexport type CreateEditorOptions = {\n\tcontainer?: HTMLElement;\n\tcontent?: string;\n\treadOnly?: boolean;\n\tonUpload?: (file: File) => Promise<string>;\n\tonChange?: (markdown: string, prevMarkdown: string) => void;\n\tmodifyImageUrl?: (file: string) => Promise<string>|string;\n}\n\nexport async function createEditor(options?: CreateEditorOptions) {\n\tconst milkdown = await MilkdownLoader.get();\n\tconst editor = milkdown.core.Editor.make();\n\tconst enabledFeatures = Array.from(getFeatures(editor, milkdown, options).entries()).filter(([_k, v]) => v.default);\n\n\teditor.config(configureFeatures(enabledFeatures.map(f => f[0]), milkdown))\n\t\t.config((ctx) => {\n\t\t\tif (options?.container) ctx.set(milkdown.core.rootCtx, options.container);\n\t\t\tif (options?.content) ctx.set(milkdown.core.defaultValueCtx, options.content);\n\n\t\t\tctx.update(milkdown.core.editorViewOptionsCtx, (prev) => {\n\t\t\t\tconst next = {...prev};\n\t\t\t\tif (options?.readOnly) next.editable = () => false;\n\t\t\t\tif (options?.onUpload) {\n\t\t\t\t\tnext.handlePaste = (view, event) => {\n\t\t\t\t\t\tconst images = Array.from(event.clipboardData?.files ?? [])\n\t\t\t\t\t\t\t.filter(f => f.type.startsWith('image/'));\n\t\t\t\t\t\tif (!images.length) return false;\n\t\t\t\t\t\tevent.preventDefault();\n\n\t\t\t\t\t\t(async () => {\n\t\t\t\t\t\t\tconst imageType = milkdown.components.imageBlock.imageBlockSchema.type(ctx);\n\t\t\t\t\t\t\tfor (const file of images) {\n\t\t\t\t\t\t\t\tconst src = await options.onUpload(file);\n\t\t\t\t\t\t\t\tif (!src) continue;\n\t\t\t\t\t\t\t\tconst node = imageType.create({ src });\n\t\t\t\t\t\t\t\tview.dispatch(view.state.tr.replaceSelectionWith(node));\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t})();\n\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\treturn next;\n\t\t\t});\n\n\t\t\tctx.update(milkdown.plugins.indent.indentConfig.key, (value) => ({\n\t\t\t\t...value,\n\t\t\t\tsize: 4,\n\t\t\t}))\n\n\t\t\tconst listener = ctx.get(milkdown.plugins.listener.listenerCtx);\n\t\t\tlistener.markdownUpdated((_ctx, markdown, prevMarkdown) => {\n\t\t\t\tif (options?.onChange && markdown !== prevMarkdown) {\n\t\t\t\t\toptions.onChange(markdown, prevMarkdown);\n\t\t\t\t}\n\t\t\t})\n\t\t\t// listener.mounted((ctx) => {\n\t\t\t// \tconsole.log(options.container);\n\t\t\t// });\n\t\t\t\n\t\t})\n\t\t.use(milkdown.presets.commonmark.commonmark)\n\t\t.use(milkdown.plugins.listener.listener)\n\t\t.use(milkdown.plugins.history.history)\n\t\t.use(milkdown.plugins.indent.indent)\n\t\t.use(milkdown.plugins.trailing.trailing)\n\t\t.use(milkdown.plugins.clipboard.clipboard)\n\t\t.use(milkdown.presets.gfm.gfm);\n\n\tenabledFeatures.forEach(f => f[1].init());\n\n\tawait editor.create();\n\treturn editor;\n}\n\nexport async function markdownToHtml(markdown: string, options?: Omit<CreateEditorOptions, 'content'|'readOnly'>) {\n\tconst editor = await createEditor(Object.assign({}, options, {content: markdown, readOnly: true}));\n\tconst md = MilkdownLoader.getSync();\n\tconst div = document.createElement('div');\n const schema = editor.ctx.get(md.core.schemaCtx);\n const view = editor.ctx.get(md.core.editorViewCtx);\n\tconst fragment = md.prose.model.DOMSerializer.fromSchema(schema).serializeFragment(view.state.doc.content);\n\t\n\tfor (const img of fragment.querySelectorAll('img')) {\n\t\tconst src = img.getAttribute('src');\n\t\tif (src && options?.modifyImageUrl) {\n\t\t\tconst url = options.modifyImageUrl(src);\n\t\t\timg.setAttribute('src', typeof url === 'string' ? url : await url);\n\t\t}\n\n\t\tif (img.hasAttribute('ratio')) {\n\t\t\timg.style.width = (100 * parseFloat(img.getAttribute('ratio'))) + '%';\n\t\t}\n\t}\n\n div.appendChild(fragment);\n\teditor.destroy();\n return div.innerHTML;\n}\n"]}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,GAAG,CAAC;AAGnD,6HAA6H;AAC7H,MAAM,CAAC,MAAM,mBAAmB,GAAG,cAAc,CAAC;AAgBlD;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,EAAmB,EAAE,aAAuC;IACrF,OAAO,EAAE,CAAC,OAAO,CAAC,UAAU,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,EAAE;QACtE,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;QACzB,OAAO;YACN,GAAG,MAAM;YACT,QAAQ,EAAE,CAAC;oBACV,GAAG,EAAE,SAAS;oBACd,QAAQ,EAAE,CAAC,GAAgB,EAAE,EAAE,CAAC,CAAC;wBAChC,IAAI,EAAE,GAAG,CAAC,YAAY,CAAC,mBAAmB,CAAC,IAAI,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC;wBACvE,KAAK,EAAE,GAAG,CAAC,YAAY,CAAC,OAAO,CAAC;qBAChC,CAAC;iBACF,CAAC;YACF,K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,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,aAAa,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,iBAAiB,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAE3G,KAAK,MAAM,GAAG,IAAI,QAAQ,CAAC,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;QACpD,MAAM,GAAG,GAAG,GAAG,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;QACpC,IAAI,GAAG,IAAI,OAAO,EAAE,cAAc,EAAE,CAAC;YACpC,MAAM,GAAG,GAAG,OAAO,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;YACxC,GAAG,CAAC,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;QACpE,CAAC;QAED,IAAI,GAAG,CAAC,YAAY,CAAC,OAAO,CAAC,EAAE,CAAC;YAC/B,GAAG,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,GAAG,GAAG,UAAU,CAAC,GAAG,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;QACvE,CAAC;IACF,CAAC;IAEE,GAAG,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;IAC7B,MAAM,CAAC,OAAO,EAAE,CAAC;IACd,OAAO,GAAG,CAAC,SAAS,CAAC;AACzB,CAAC","sourcesContent":["import { MilkdownLoader } from \"./loader\";\nimport { configureFeatures, getFeatures } from '.';\nimport type * as milkdown from '@omegagrid/milkdown-kit';\n\n/** The attribute of a rendered <a> that keeps the destination written in the markdown when modifyLinkUrl rewrote its href */\nexport const LINK_HREF_ATTRIBUTE = 'data-md-href';\n\nexport type CreateEditorOptions = {\n\tcontainer?: HTMLElement;\n\tcontent?: string;\n\treadOnly?: boolean;\n\tonUpload?: (file: File) => Promise<string>;\n\tonChange?: (markdown: string, prevMarkdown: string) => void;\n\tmodifyImageUrl?: (file: string) => Promise<string>|string;\n\t/**\n\t * Rewrites the href the browser sees for a link, e.g. a page name into the address of that page. The\n\t * markdown keeps the destination as written; the rendered <a> carries it in LINK_HREF_ATTRIBUTE.\n\t */\n\tmodifyLinkUrl?: (href: string) => string;\n}\n\n/**\n * The link mark of the commonmark preset with its href passed through modifyLinkUrl. Only the DOM\n * changes: mark.attrs.href stays the markdown destination (so does the serialized markdown), the\n * rendered <a> keeps it in LINK_HREF_ATTRIBUTE, and parseDOM reads that attribute back first, so a\n * link edited in place does not pick up the rewritten address.\n */\nfunction createLinkSchema(md: typeof milkdown, modifyLinkUrl: (href: string) => string) {\n\treturn md.presets.commonmark.linkSchema.extendSchema((prev) => (ctx) => {\n\t\tconst schema = prev(ctx);\n\t\treturn {\n\t\t\t...schema,\n\t\t\tparseDOM: [{\n\t\t\t\ttag: 'a[href]',\n\t\t\t\tgetAttrs: (dom: HTMLElement) => ({\n\t\t\t\t\thref: dom.getAttribute(LINK_HREF_ATTRIBUTE) ?? dom.getAttribute('href'),\n\t\t\t\t\ttitle: dom.getAttribute('title'),\n\t\t\t\t}),\n\t\t\t}],\n\t\t\ttoDOM: (mark, inline) => {\n\t\t\t\tconst spec = schema.toDOM?.(mark, inline) ?? ['a', { ...mark.attrs }];\n\t\t\t\tconst href = mark.attrs.href as string;\n\t\t\t\tconst url = href ? modifyLinkUrl(href) : href;\n\t\t\t\tif (!Array.isArray(spec) || url == null || url === href) return spec;\n\t\t\t\tconst [tag, attrs, ...rest] = spec as [string, Record<string, unknown>, ...unknown[]];\n\t\t\t\treturn [tag, { ...attrs, href: url, [LINK_HREF_ATTRIBUTE]: href }, ...rest] as ReturnType<NonNullable<typeof schema.toDOM>>;\n\t\t\t},\n\t\t};\n\t});\n}\n\nexport async function createEditor(options?: CreateEditorOptions) {\n\tconst milkdown = await MilkdownLoader.get();\n\tconst editor = milkdown.core.Editor.make();\n\tconst enabledFeatures = Array.from(getFeatures(editor, milkdown, options).entries()).filter(([_k, v]) => v.default);\n\n\teditor.config(configureFeatures(enabledFeatures.map(f => f[0]), milkdown))\n\t\t.config((ctx) => {\n\t\t\tif (options?.container) ctx.set(milkdown.core.rootCtx, options.container);\n\t\t\tif (options?.content) ctx.set(milkdown.core.defaultValueCtx, options.content);\n\n\t\t\tctx.update(milkdown.core.editorViewOptionsCtx, (prev) => {\n\t\t\t\tconst next = {...prev};\n\t\t\t\tif (options?.readOnly) next.editable = () => false;\n\t\t\t\tif (options?.onUpload) {\n\t\t\t\t\tnext.handlePaste = (view, event) => {\n\t\t\t\t\t\tconst images = Array.from(event.clipboardData?.files ?? [])\n\t\t\t\t\t\t\t.filter(f => f.type.startsWith('image/'));\n\t\t\t\t\t\tif (!images.length) return false;\n\t\t\t\t\t\tevent.preventDefault();\n\n\t\t\t\t\t\t(async () => {\n\t\t\t\t\t\t\tconst imageType = milkdown.components.imageBlock.imageBlockSchema.type(ctx);\n\t\t\t\t\t\t\tfor (const file of images) {\n\t\t\t\t\t\t\t\tconst src = await options.onUpload(file);\n\t\t\t\t\t\t\t\tif (!src) continue;\n\t\t\t\t\t\t\t\tconst node = imageType.create({ src });\n\t\t\t\t\t\t\t\tview.dispatch(view.state.tr.replaceSelectionWith(node));\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t})();\n\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\treturn next;\n\t\t\t});\n\n\t\t\tctx.update(milkdown.plugins.indent.indentConfig.key, (value) => ({\n\t\t\t\t...value,\n\t\t\t\tsize: 4,\n\t\t\t}))\n\n\t\t\tconst listener = ctx.get(milkdown.plugins.listener.listenerCtx);\n\t\t\tlistener.markdownUpdated((_ctx, markdown, prevMarkdown) => {\n\t\t\t\tif (options?.onChange && markdown !== prevMarkdown) {\n\t\t\t\t\toptions.onChange(markdown, prevMarkdown);\n\t\t\t\t}\n\t\t\t})\n\t\t\t// listener.mounted((ctx) => {\n\t\t\t// \tconsole.log(options.container);\n\t\t\t// });\n\t\t\t\n\t\t})\n\t\t.use(milkdown.presets.commonmark.commonmark)\n\t\t.use(milkdown.plugins.listener.listener)\n\t\t.use(milkdown.plugins.history.history)\n\t\t.use(milkdown.plugins.indent.indent)\n\t\t.use(milkdown.plugins.trailing.trailing)\n\t\t.use(milkdown.plugins.clipboard.clipboard)\n\t\t.use(milkdown.presets.gfm.gfm);\n\n\t// used after the preset: a mark registered later under the same name replaces the preset's one\n\tif (options?.modifyLinkUrl) editor.use(createLinkSchema(milkdown, options.modifyLinkUrl));\n\n\tenabledFeatures.forEach(f => f[1].init());\n\n\tawait editor.create();\n\treturn editor;\n}\n\nexport async function markdownToHtml(markdown: string, options?: Omit<CreateEditorOptions, 'content'|'readOnly'>) {\n\tconst editor = await createEditor(Object.assign({}, options, {content: markdown, readOnly: true}));\n\tconst md = MilkdownLoader.getSync();\n\tconst div = document.createElement('div');\n const schema = editor.ctx.get(md.core.schemaCtx);\n const view = editor.ctx.get(md.core.editorViewCtx);\n\tconst fragment = md.prose.model.DOMSerializer.fromSchema(schema).serializeFragment(view.state.doc.content);\n\t\n\tfor (const img of fragment.querySelectorAll('img')) {\n\t\tconst src = img.getAttribute('src');\n\t\tif (src && options?.modifyImageUrl) {\n\t\t\tconst url = options.modifyImageUrl(src);\n\t\t\timg.setAttribute('src', typeof url === 'string' ? url : await url);\n\t\t}\n\n\t\tif (img.hasAttribute('ratio')) {\n\t\t\timg.style.width = (100 * parseFloat(img.getAttribute('ratio'))) + '%';\n\t\t}\n\t}\n\n div.appendChild(fragment);\n\teditor.destroy();\n return div.innerHTML;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omegagrid/markdown",
3
- "version": "0.11.19",
3
+ "version": "0.11.21",
4
4
  "license": "UNLICENSED",
5
5
  "description": "Markdown view webcomponent",
6
6
  "main": "./dist/index.js",
@@ -28,11 +28,11 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@fortawesome/fontawesome-svg-core": "^7.0.1",
31
- "@omegagrid/code": "^0.11.19",
32
- "@omegagrid/core": "^0.11.19",
33
- "@omegagrid/localize": "^0.11.19",
34
- "@omegagrid/milkdown-kit": "^0.11.19",
35
- "@omegagrid/tabs": "^0.11.19",
31
+ "@omegagrid/code": "^0.11.21",
32
+ "@omegagrid/core": "^0.11.21",
33
+ "@omegagrid/localize": "^0.11.21",
34
+ "@omegagrid/milkdown-kit": "^0.11.21",
35
+ "@omegagrid/tabs": "^0.11.21",
36
36
  "lit": "^3.1.1",
37
37
  "markdown-it": "^14.1.0",
38
38
  "ts-debounce": "^4.0.0"