Skip to content

Commit 1f81238

Browse files
committed
feat(pat-tooltip): Soft-depend on pat-markdown.
Only register the markdown data type handler when the pattern is available. Not hard-depending and importing pat-markdown fixes a problem where only including pat-tooltip also included pat-markdown and pat-syntax-highlight with it's big highlight.js library. This can reduce the generated bundle size significantly.
1 parent 3f17e99 commit 1f81238

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/pat/tooltip/tooltip.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ class Pattern extends BasePattern {
345345
let content;
346346
if (url) {
347347
// Tooltip from remote page.
348-
const handler = this._ajaxDataTypeHandlers[this.options.ajaxDataType];
348+
const handler = this.data_type_handlers[this.options.ajaxDataType];
349349
try {
350350
// TODO: use pat-inject, once it supports async
351351
const response = await fetch(url, {
@@ -393,7 +393,7 @@ class Pattern extends BasePattern {
393393
return { url, selector };
394394
}
395395

396-
_ajaxDataTypeHandlers = {
396+
static data_type_handlers = {
397397
html(text, url, selector) {
398398
let tmp = document.createElement("div");
399399
tmp.innerHTML = text;
@@ -404,16 +404,34 @@ class Pattern extends BasePattern {
404404
},
405405

406406
async markdown(text, url, selector) {
407-
const pat_markdown = await import("../markdown/markdown");
408-
const pat = pat_markdown.default.init($("<div/>"));
407+
const Markdown = registry.patterns.markdown;
408+
if (!Markdown) {
409+
return text;
410+
}
411+
412+
const instance = new Markdown($("<div/>"));
413+
await events.await_pattern_init(instance);
414+
409415
const cfg = { url };
410416
if (selector) {
411417
cfg.source = selector;
412418
}
413-
const ret = await pat.renderForInjection(cfg, text);
419+
420+
const ret = await instance.renderForInjection(cfg, text);
414421
return ret[0];
415422
},
416423
};
424+
425+
static register_type_handler(type, handler) {
426+
Pattern.data_type_handlers[type] = handler;
427+
}
428+
429+
constructor(...args) {
430+
super(...args);
431+
432+
this.register_type_handler = this.constructor.register_type_handler;
433+
this.data_type_handlers = this.constructor.data_type_handlers;
434+
}
417435
}
418436

419437
registry.register(Pattern);

src/pat/tooltip/tooltip.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,7 @@ describe("pat-tooltip", () => {
13171317
});
13181318

13191319
it("7.3 - will handle markdown content", async () => {
1320+
await import("../markdown/markdown");
13201321
global.fetch = jest.fn().mockImplementation(mockFetch("## hello."));
13211322

13221323
const $el = testutils.createTooltip({
@@ -1331,6 +1332,7 @@ describe("pat-tooltip", () => {
13311332

13321333
testutils.click($el);
13331334
await utils.timeout(1); // wait a tick for async fetch
1335+
await utils.timeout(1);
13341336

13351337
expect(spy_ajax).toHaveBeenCalled();
13361338
expect(spy_show).toHaveBeenCalled();
@@ -1345,6 +1347,7 @@ describe("pat-tooltip", () => {
13451347
});
13461348

13471349
it("7.4 - will extract a section from markdown", async () => {
1350+
await import("../markdown/markdown");
13481351
global.fetch = jest.fn().mockImplementation(
13491352
mockFetch(`
13501353
# note a limitation
@@ -1369,6 +1372,7 @@ this will be extracted.
13691372

13701373
testutils.click($el);
13711374
await utils.timeout(1); // wait a tick for async fetch
1375+
await utils.timeout(1);
13721376

13731377
expect(spy_ajax).toHaveBeenCalled();
13741378
expect(spy_show).toHaveBeenCalled();

0 commit comments

Comments
 (0)