2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2018-11-09 20:00:53 +00:00
|
|
|
const invariant = (truthy, error) => {
|
|
|
|
if (!truthy) throw new Error(error);
|
|
|
|
};
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
export default function (context, selector) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const cmService = context.owner.lookup('service:code-mirror');
|
2018-11-09 20:00:53 +00:00
|
|
|
|
2022-11-09 23:15:31 +00:00
|
|
|
const element = document.querySelector(selector);
|
2018-11-09 20:00:53 +00:00
|
|
|
invariant(element, `Selector ${selector} matched no elements`);
|
|
|
|
|
2022-11-09 23:15:31 +00:00
|
|
|
const cm = cmService.instanceFor(element.id);
|
2018-11-09 20:00:53 +00:00
|
|
|
invariant(cm, `No registered CodeMirror instance for ${selector}`);
|
|
|
|
|
|
|
|
return cm;
|
|
|
|
}
|