Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event and total number of class change #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 80 additions & 58 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {html, render} from 'lit-html';
import {styleMap} from 'lit-html/directives/style-map.js';
import { html, render } from "lit-html";
import { styleMap } from "lit-html/directives/style-map.js";

export default (editor, opts = {}) => {
const sm = editor.SelectorManager;
const listEl = document.createElement('div');
const prefix = editor.Config.selectorManager.pStylePrefix;
const selIdAttr = 'data-sel-id';
const listEl = document.createElement("div");
const prefix = editor.Config.selectorManager.pStylePrefix;
const selIdAttr = "data-sel-id";

const options = { ...{
// default options
containerStyle: `
const options = {
...{
// default options
containerStyle: `
.${prefix}suggest {
position: absolute;
z-index: 999;
Expand All @@ -22,7 +23,7 @@ export default (editor, opts = {}) => {
padding: 0 5px;
}
`,
tagStyle: `
tagStyle: `
div.${prefix}suggest__class {
list-style: none;
cursor: pointer;
Expand All @@ -33,66 +34,87 @@ export default (editor, opts = {}) => {
font-size: x-small;
}
`,
}, ...opts };
},
...opts,
};

function update(show, filter = '') {
const allComps = []
editor.Pages.getAll()
.forEach(page => {
page.getMainComponent()
.onAll((comp => allComps.push(comp)))
})
render(html`
<div
class="${prefix}suggest ${prefix}one-bg"
style=${styleMap({
opacity: show ? '1' : '0',
'pointer-events': show ? 'initial' : 'none',
})}
>
${ sm.getAll()
.filter(sel => !sel.private && !sm.getSelected().includes(sel) && sel.getLabel().includes(filter))
// count the number of times each css class is used
.map(sel => ({
sel,
count: allComps.reduce((num, comp) => comp.getClasses().includes(sel.id) ? num+1 : num, 0),
}))
.sort((first, second) => second.count - first.count)
// only classes which are in use
.filter(({sel, count}) => count > 0)
// same structure as the tags in the input field
.map(({sel, count}) => html`
<div
data-sel-id=${sel.id}
class="${prefix}clm-tag ${prefix}three-bg ${prefix}suggest__class"
@mousedown=${() => select(sel.id)}
>
<span data-tag-name="">${sel.getLabel()}</span>
<span class="${prefix}clm-tag-status ${prefix}suggest__count" data-tag-status="">${count}</span>
</div>
`)
}
</div>
`, listEl);
function update(show, filter = "") {
const allComps = [];
editor.Pages.getAll().forEach((page) => {
page.getMainComponent().onAll((comp) => allComps.push(comp));
});
render(
html`
<div
class="${prefix}suggest ${prefix}one-bg"
style=${styleMap({
opacity: show ? "1" : "0",
"pointer-events": show ? "initial" : "none",
})}
>
${sm
.getAll()
.filter(
(sel) =>
!sel.private &&
!sm.getSelected().includes(sel) &&
sel.getLabel().includes(filter)
)
// count the number of times each css class is used
.map((sel) => ({
sel,
count: allComps.reduce(
(num, comp) =>
comp.getClasses().includes(sel.id) ? num + 1 : num,
0
),
}))
.sort((first, second) => second.count - first.count)
// only classes which are in use
.filter(({ sel, count }) => count > 0)
.slice(0, 5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea !

But this should be an option, e.g. options.numClasses

// same structure as the tags in the input field
.map(
({ sel, count }) => html`
<div
data-sel-id=${sel.id}
class="${prefix}clm-tag ${prefix}three-bg ${prefix}suggest__class"
@mousedown=${() => select(sel.id)}
>
<span data-tag-name="">${sel.getLabel()}</span>
<span
class="${prefix}clm-tag-status ${prefix}suggest__count"
data-tag-status=""
>${count}</span
>
</div>
`
)}
</div>
`,
listEl
);
}

function select(selId) {
const selector = sm.getAll().find(s => s.id === selId);
const selector = sm.getAll().find((s) => s.id === selId);
sm.addSelected(selector);
}

editor.on('load', () => {
editor.on("load", () => {
// build the UI
const tags = editor.getContainer().querySelector(`#${prefix}clm-tags-field`);
const tags = editor
.getContainer()
.querySelector(`#${prefix}clm-tags-field`);
const input = tags.querySelector(`#${prefix}clm-new`);
tags.parentNode.insertBefore(listEl, tags.nextSibling);
const styleEl = document.createElement('style');
const styleEl = document.createElement("style");
styleEl.innerHTML = options.containerStyle + options.tagStyle;
document.head.appendChild(styleEl);
// bind to events
input.addEventListener('blur', () => update(false));
input.addEventListener('focus', () => update(true, input.value));
editor.on('selector', () => update(false));
input.addEventListener('keyup', () => update(true, input.value));
})
input.addEventListener("blur", () => update(false));
input.addEventListener("focus", () => update(true, input.value));
editor.on("start:open-sm", () => update(false));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch @ahmadlimbada !!

input.addEventListener("keyup", () => update(true, input.value));
});
};