-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make CONFIG more overridable & Fix quicklink
- Deprecate quicklink.ignores but not obsolete it - Rename `load-config.js` in source/js/ to `config.js`
- Loading branch information
1 parent
f475608
commit 7a5b348
Showing
7 changed files
with
95 additions
and
64 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,6 +1,7 @@ | ||
{%- if theme.quicklink.enable %} | ||
<script src="{{ theme.vendors.quicklink }}"></script> | ||
{{ next_data('quicklink', page.quicklink, { | ||
js: theme.vendors.quicklink | ||
url: url | replace(r/index\.html$/, '') | ||
}) }} | ||
{{ next_js('third-party/quicklink.js') }} | ||
{%- endif %} |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{ | ||
if (!window.NexT) window.NexT = {}; | ||
|
||
const className = 'next-config'; | ||
|
||
const staticConfig = {}; | ||
let variableConfig = {}; | ||
|
||
const parse = (text) => { | ||
const jsonString = new DOMParser() | ||
.parseFromString(text, 'text/html').documentElement | ||
.textContent; | ||
return JSON.parse(jsonString || '{}'); | ||
}; | ||
|
||
const update = (name) => { | ||
const targetEle = document.querySelector(`.${className}[data-name="${name}"]`); | ||
if (!targetEle) return; | ||
const parsedConfig = parse(targetEle.text); | ||
if (name === 'main') { | ||
Object.assign(staticConfig, parsedConfig); | ||
} else { | ||
variableConfig[name] = parsedConfig; | ||
} | ||
}; | ||
|
||
update('main'); | ||
|
||
window.CONFIG = new Proxy({}, { | ||
get(overrideConfig, name) { | ||
let existing; | ||
if (name in staticConfig) { | ||
existing = staticConfig[name]; | ||
} else { | ||
if (!(name in variableConfig)) update(name); | ||
existing = variableConfig[name]; | ||
} | ||
|
||
let override = overrideConfig[name]; | ||
if (override === undefined && typeof existing === 'object') { | ||
override = {}; | ||
overrideConfig[name] = override; | ||
} | ||
|
||
if (typeof override === 'object') { | ||
return new Proxy({...existing, ...override}, { | ||
set(target, prop, value) { | ||
override[prop] = value; | ||
return true; | ||
} | ||
}); | ||
} | ||
return existing; | ||
} | ||
}); | ||
|
||
document.addEventListener('pjax:success', () => { | ||
variableConfig = {}; | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
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,29 +1,41 @@ | ||
/* global NexT, CONFIG, quicklink */ | ||
/* global CONFIG, quicklink */ | ||
|
||
{ | ||
const quicklinkListen = () => { | ||
quicklink.listen({ | ||
let resetFn = null; | ||
|
||
const onRefresh = () => { | ||
if (resetFn) resetFn(); | ||
if (!CONFIG.quicklink.enable) return; | ||
|
||
let ignoresArr = CONFIG.quicklink.ignores || []; | ||
if (typeof ignoresArr === 'string') { | ||
try { | ||
ignoresArr = JSON.parse(`[${ignoresArr}]`); | ||
} catch { | ||
// eslint-disable-next-line no-console | ||
console.error('Setting regex and function in config file is deprecated. Try setting `CONFIG.quicklink.ignores` in any script code.'); | ||
// eslint-disable-next-line no-eval | ||
ignoresArr = eval(`[${ignoresArr}]`); | ||
} | ||
} else if (!Array.isArray(ignoresArr)) { | ||
ignoresArr = [ignoresArr]; | ||
} | ||
|
||
resetFn = quicklink.listen({ | ||
timeout : CONFIG.quicklink.timeout, | ||
priority: CONFIG.quicklink.priority, | ||
ignores : [ | ||
uri => uri.includes('#'), | ||
uri => uri === CONFIG.quicklink.url, | ||
// TODO: `quicklink.ignores` may includes functions that can not be parsed safely in JS runtime | ||
...JSON.parse(`[${CONFIG.quicklink.ignores}]`) | ||
...ignoresArr | ||
] | ||
}); | ||
}; | ||
|
||
document.addEventListener('page:loaded', () => { | ||
if (!CONFIG.quicklink.enable) return; | ||
|
||
NexT.utils.getScript(CONFIG.quicklink.js) | ||
.then(() => { | ||
if (CONFIG.quicklink.delay) { | ||
window.addEventListener('load', quicklinkListen); | ||
} else { | ||
quicklinkListen(); | ||
} | ||
}); | ||
}); | ||
if (CONFIG.quicklink.delay) { | ||
window.addEventListener('load', onRefresh); | ||
document.addEventListener('pjax:success', onRefresh); | ||
} else { | ||
document.addEventListener('page:loaded', onRefresh); | ||
} | ||
} |