Skip to content

Commit c1b2cd3

Browse files
authored
Merge pull request #805 from UziTech/custom-element
fix: use custom element
2 parents 4bf6eb4 + 3f9f1a5 commit c1b2cd3

File tree

7 files changed

+52
-32
lines changed

7 files changed

+52
-32
lines changed

lib/decorators/element.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"use strict"
22

3-
import { registerOrUpdateElement } from "atom-utils-plus"
4-
53
/**
64
* Generates a decorator function to convert a class into a custom element through the `registerOrUpdateElement` method
75
* from `atom-utils-plus`.
@@ -17,7 +15,7 @@ import { registerOrUpdateElement } from "atom-utils-plus"
1715
* the second example.**
1816
*
1917
* @param {string} elementName The node name of the element to register
20-
* @returns {Function} The element class as returned by `document.registerElement`
18+
* @returns {void}
2119
* @element('dummy-element-name') export default class SomeClass {
2220
* // ...
2321
* }
@@ -27,5 +25,7 @@ import { registerOrUpdateElement } from "atom-utils-plus"
2725
* }
2826
*/
2927
export default function element(cls, elementName) {
30-
return registerOrUpdateElement(elementName, { class: cls })
28+
if (!window.customElements.get(elementName)) {
29+
window.customElements.define(elementName, cls)
30+
}
3131
}

lib/main.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict"
22

33
import { Emitter, CompositeDisposable } from "atom"
4-
import MinimapElement from "./minimap-element"
4+
import { createMinimapElement } from "./minimap-element"
55
import Minimap from "./minimap"
66
import * as config from "./config.json"
77
import * as PluginManagement from "./plugin-management"
@@ -12,7 +12,7 @@ import { debounce } from "./deps/underscore-plus"
1212
export * as config from "./config.json"
1313
export * from "./plugin-management"
1414
export { default as Minimap } from "./minimap"
15-
export { default as MinimapElement } from "./minimap-element"
15+
export { createMinimapElement } from "./minimap-element"
1616

1717
/**
1818
* The `Minimap` package provides an eagle-eye view of text buffers.
@@ -110,7 +110,7 @@ export function minimapViewProvider(model) {
110110
if (model instanceof Minimap) {
111111
let element = model.getMinimapElement()
112112
if (!element) {
113-
element = new MinimapElement()
113+
element = createMinimapElement()
114114
element.setModel(model)
115115
}
116116
return element
@@ -180,8 +180,8 @@ export function toggle() {
180180
* @param {string} template The name of the template to use
181181
*/
182182
async function generatePlugin(template) {
183-
const { default: MinimapPluginGeneratorElement } = await import("./minimap-plugin-generator-element")
184-
const view = new MinimapPluginGeneratorElement()
183+
const { createMinimapPluginGeneratorElement } = await import("./minimap-plugin-generator-element")
184+
const view = createMinimapPluginGeneratorElement()
185185
view.template = template
186186
view.attach()
187187
}

lib/minimap-element.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import CanvasDrawer from "./mixins/canvas-drawer"
1010
import include from "./decorators/include"
1111
import element from "./decorators/element"
1212

13-
import MinimapQuickSettingsElement from "./minimap-quick-settings-element"
13+
import { createMinimapQuickSettingsElement } from "./minimap-quick-settings-element"
1414
const elementResizeDetector = elementResizeDetectorImport({ strategy: "scroll" })
1515

1616
const SPEC_MODE = atom.inSpecMode()
17+
const TAG_NAME = "atom-text-editor-minimap"
1718

1819
/**
1920
* Public: The MinimapElement is the view meant to render a {@link Minimap} instance in the DOM.
@@ -25,10 +26,10 @@ const SPEC_MODE = atom.inSpecMode()
2526
*
2627
* @example Let minimapElement = atom.views.getView(minimap)
2728
*/
28-
class MinimapElement {
29+
class MinimapElement extends HTMLElement {
2930
static initClass() {
3031
include(this, CanvasDrawer, EventsDelegation, AncestorsMethods)
31-
return element(this, "atom-text-editor-minimap")
32+
element(this, TAG_NAME)
3233
}
3334

3435
// ## ## ####### ####### ## ## ######
@@ -273,7 +274,7 @@ class MinimapElement {
273274
*
274275
* @access private
275276
*/
276-
attachedCallback() {
277+
connectedCallback() {
277278
if (typeof atom.views.pollDocument === "function") {
278279
this.subscriptions.add(
279280
atom.views.pollDocument(() => {
@@ -326,7 +327,7 @@ class MinimapElement {
326327
*
327328
* @access private
328329
*/
329-
detachedCallback() {
330+
disconnectedCallback() {
330331
this.minimap.getTextEditorElement().removeAttribute("with-minimap")
331332
this.attached = false
332333
}
@@ -363,7 +364,7 @@ class MinimapElement {
363364
}
364365

365366
const container = parent || this.minimap.getTextEditorElement()
366-
const minimaps = container.querySelectorAll("atom-text-editor-minimap")
367+
const minimaps = container.querySelectorAll(TAG_NAME)
367368
if (minimaps.length) {
368369
Array.prototype.forEach.call(minimaps, (el) => {
369370
el.destroy()
@@ -583,7 +584,7 @@ class MinimapElement {
583584
this.quickSettingsElement.destroy()
584585
this.quickSettingsSubscription.dispose()
585586
} else {
586-
this.quickSettingsElement = new MinimapQuickSettingsElement()
587+
this.quickSettingsElement = createMinimapQuickSettingsElement()
587588
this.quickSettingsElement.setModel(this)
588589
this.quickSettingsSubscription = this.quickSettingsElement.onDidDestroy(() => {
589590
this.quickSettingsElement = null
@@ -1220,8 +1221,13 @@ class MinimapElement {
12201221
}
12211222
}
12221223

1223-
const minimapElement = MinimapElement.initClass()
1224-
export default minimapElement
1224+
MinimapElement.initClass()
1225+
1226+
export function createMinimapElement() {
1227+
const element = document.createElement(TAG_NAME)
1228+
element.createdCallback()
1229+
return element
1230+
}
12251231

12261232
// ######## ## ## ######## ## ## ######## ######
12271233
// ## ## ## ## ### ## ## ## ##

lib/minimap-plugin-generator-element.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import path from "path"
66
import { BufferedProcess } from "atom"
77
import element from "./decorators/element"
88

9+
const TAG_NAME = "minimap-plugin-generator"
10+
911
/** @access private */
10-
class MinimapPluginGeneratorElement {
12+
class MinimapPluginGeneratorElement extends HTMLElement {
1113
static initClass() {
1214
this.registerCommands()
13-
return element(this, "minimap-plugin-generator")
15+
element(this, TAG_NAME)
1416
}
1517

1618
static registerCommands() {
@@ -51,7 +53,7 @@ class MinimapPluginGeneratorElement {
5153
this.appendChild(this.modal)
5254
}
5355

54-
attachedCallback() {
56+
connectedCallback() {
5557
this.previouslyFocusedElement = document.activeElement
5658
this.message.textContent = "Enter plugin path"
5759
this.setPathText("my-minimap-plugin")
@@ -152,8 +154,13 @@ class MinimapPluginGeneratorElement {
152154
}
153155
}
154156

155-
const minimapPluginGeneratorElement = MinimapPluginGeneratorElement.initClass()
156-
export default minimapPluginGeneratorElement
157+
MinimapPluginGeneratorElement.initClass()
158+
159+
export function createMinimapPluginGeneratorElement() {
160+
const element = document.createElement(TAG_NAME)
161+
element.createdCallback()
162+
return element
163+
}
157164

158165
function linkPackage(packagePath, callback) {
159166
const args = ["link"]

lib/minimap-quick-settings-element.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import * as Main from "./main"
77
import element from "./decorators/element"
88
import include from "./decorators/include"
99

10+
const TAG_NAME = "minimap-quick-settings"
11+
1012
/** @access private */
11-
class MinimapQuickSettingsElement {
13+
class MinimapQuickSettingsElement extends HTMLElement {
1214
static initClass() {
1315
include(this, EventsDelegation, SpacePenDSL.Babel)
14-
return element(this, "minimap-quick-settings")
16+
element(this, TAG_NAME)
1517
}
1618

1719
static content() {
@@ -263,5 +265,10 @@ class MinimapQuickSettingsElement {
263265
}
264266
}
265267

266-
const minimapQuickSettingsElement = MinimapQuickSettingsElement.initClass()
267-
export default minimapQuickSettingsElement
268+
MinimapQuickSettingsElement.initClass()
269+
270+
export function createMinimapQuickSettingsElement() {
271+
const element = document.createElement(TAG_NAME)
272+
element.createdCallback()
273+
return element
274+
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
"lint": "eslint . --fix",
1515
"test.lint": "eslint .",
1616
"clean": "shx rm -rf dist .parcel-cache",
17-
"dev": "cross-env NODE_ENV=development parcel watch --target main ./lib/main.js --no-scope-hoist",
18-
"build.test": "npm run clean && cross-env NODE_ENV=test parcel build --target main ./lib/main.js --no-scope-hoist",
19-
"build": "cross-env NODE_ENV=production parcel build --target main ./lib/main.js --no-scope-hoist",
17+
"dev": "cross-env NODE_ENV=development parcel watch --target main ./lib/main.js",
18+
"build.test": "npm run clean && cross-env NODE_ENV=test parcel build --target main ./lib/main.js",
19+
"build": "cross-env NODE_ENV=production parcel build --target main ./lib/main.js",
2020
"build-commit": "npm run clean && build-commit -o dist",
2121
"esdoc": "esdoc -c esdoc.json",
2222
"test": "atom --test spec",

spec/minimap-main-spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
process.env.NODE_ENV = "test"
33

44
require("./helpers/workspace")
5-
const { Minimap, MinimapElement } = require("../dist/main")
5+
const { Minimap, createMinimapElement } = require("../dist/main")
66

77
describe("Minimap package", () => {
88
let [editor, minimap, editorElement, minimapElement, workspaceElement, minimapPackage] = []
@@ -52,7 +52,7 @@ describe("Minimap package", () => {
5252
it("provider returns minimap.minimapElement", () => {
5353
const textEditor = atom.workspace.buildTextEditor({})
5454
minimap = new Minimap({ textEditor })
55-
minimapElement = new MinimapElement()
55+
minimapElement = createMinimapElement()
5656
minimapElement.setModel(minimap)
5757
const minimapView = atom.views.getView(minimap)
5858

0 commit comments

Comments
 (0)