本文整理汇总了TypeScript中@jupyterlab/codemirror.Mode.ensure方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Mode.ensure方法的具体用法?TypeScript Mode.ensure怎么用?TypeScript Mode.ensure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类@jupyterlab/codemirror.Mode
的用法示例。
在下文中一共展示了Mode.ensure方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('should add a spec loader', async () => {
let called = 0;
let loaded = 0;
Mode.addSpecLoader(async spec => {
called++;
if (spec.mode !== 'bar') {
return false;
}
loaded++;
return true;
}, 42);
CodeMirror.modeInfo.push(fakeMode('bar'));
let spec = await Mode.ensure('bar');
expect(called).to.equal(1);
expect(loaded).to.equal(1);
expect(spec.name).to.equal('BAR');
spec = await Mode.ensure('python');
expect(called).to.equal(1);
expect(loaded).to.equal(1);
try {
spec = await Mode.ensure('APL');
} catch (err) {
// apparently one cannot use webpack `require` in jest
}
expect(called).to.equal(2);
expect(loaded).to.equal(1);
});
示例2: callback
highlight: (code, lang, callback) => {
let cb = (err: Error | null, code: string) => {
if (callback) {
callback(err, code);
}
return code;
};
if (!lang) {
// no language, no highlight
return cb(null, code);
}
Mode.ensure(lang).then(spec => {
let el = document.createElement('div');
if (!spec) {
console.log(`No CodeMirror mode: ${lang}`);
return cb(null, code);
}
try {
Mode.run(code, spec.mime, el);
return cb(null, el.innerHTML);
} catch (err) {
console.log(`Failed to highlight ${lang} code`, err);
return cb(err, code);
}
}).catch(err => {
console.log(`No CodeMirror mode: ${lang}`);
console.log(`Require CodeMirror mode error: ${err}`);
return cb(null, code);
});
return code;
}