-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #185 from marp-team/update-browser-script
Update browser script to make changeable the target root
- Loading branch information
Showing
9 changed files
with
115 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import browser from '../src/browser' | ||
import { browser } from '../src/browser' | ||
|
||
browser() | ||
const script = document.currentScript | ||
|
||
browser(script ? script.getRootNode() : document) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,29 @@ | ||
import observer from './observer' | ||
|
||
import { observer } from './observer' | ||
export { observer } | ||
|
||
export default function browser(): void { | ||
const marpCoreBrowserScript = Symbol() | ||
|
||
export const browser = (target: ParentNode = document): (() => void) => { | ||
if (typeof window === 'undefined') { | ||
throw new Error( | ||
"Marp Core's browser script is valid only in browser context." | ||
) | ||
} | ||
|
||
if (window['marpCoreBrowserScript']) { | ||
console.warn("Marp Core's browser script has already executed.") | ||
return | ||
if (target[marpCoreBrowserScript]) return target[marpCoreBrowserScript] | ||
|
||
const cleanupObserver = observer({ target }) | ||
const cleanup = () => { | ||
cleanupObserver() | ||
delete target[marpCoreBrowserScript] | ||
} | ||
|
||
Object.defineProperty(window, 'marpCoreBrowserScript', { value: true }) | ||
observer() | ||
Object.defineProperty(target, marpCoreBrowserScript, { | ||
configurable: true, | ||
value: cleanup, | ||
}) | ||
|
||
return cleanup | ||
} | ||
|
||
export default browser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,45 @@ | ||
import { polyfills } from '@marp-team/marpit-svg-polyfill' | ||
import fittingObserver from './fitting/observer' | ||
|
||
// Observer is divided for usage in Marp Web. | ||
export default function observer(keep = true): void { | ||
for (const polyfill of polyfills()) polyfill() | ||
fittingObserver() | ||
type ObserverOptions = { | ||
once?: boolean | ||
target?: ParentNode | ||
} | ||
|
||
export function observer(opts?: ObserverOptions): () => void | ||
/** @deprecated Usage of observer() with boolean option has been deprecated. Please replace with the usage of option object. */ | ||
export function observer(keep?: boolean): () => void | ||
export function observer(opts: ObserverOptions | boolean = {}): () => void { | ||
const _opts = | ||
typeof opts === 'boolean' | ||
? ((keep): ObserverOptions => { | ||
const once = !keep | ||
console.warn( | ||
`[DEPRECATION WARNING] Usage of observer() with boolean option has been deprecated. Please replace with the usage of option object: observer({ once: ${ | ||
once ? 'true' : 'false' | ||
} }).` | ||
) | ||
|
||
return { once } | ||
})(opts) | ||
: opts | ||
|
||
const { once = false, target = document } = _opts | ||
const polyfillFuncs = polyfills() | ||
|
||
if (keep) window.requestAnimationFrame(() => observer(keep)) | ||
let enabled = !once | ||
|
||
const observer = () => { | ||
for (const polyfill of polyfillFuncs) polyfill({ target }) | ||
fittingObserver(target) | ||
|
||
if (enabled) window.requestAnimationFrame(observer) | ||
} | ||
observer() | ||
|
||
return () => { | ||
enabled = false | ||
} | ||
} | ||
|
||
export default observer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
const dummy = 'This is a placeholder for the content of built browser script.' | ||
const placeholder = | ||
'This is a placeholder for the content of built browser script.' | ||
|
||
export default dummy | ||
export default placeholder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters