Skip to content

Commit

Permalink
deploy: 87bfd47
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjamar committed Apr 17, 2024
1 parent 450c60d commit 8f0366c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
32 changes: 18 additions & 14 deletions projects/editor/src/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Copyright (c) 2024 ninjamar
https://github.com/ninjamar/editor
*/
import { toggleStyle, styles, styleAction, ElementOptions, createOptionsFromElement, extractGreatestParent, removeStyle } from "./style.js";
import { toggleStyle, styles, styleAction, ElementOptions, createOptionsFromElement, getAllAppliedStyles, extractGreatestParent, removeStyle } from "./style.js";
import { attributesToDict } from "./utils.js";

// Set options for the context menu
const menuOptions = {
Expand Down Expand Up @@ -288,20 +289,23 @@ export class Editor {
item.classList.remove("editor-context-menu-active");
}
let parent = extractGreatestParent(range);
let options = getAllAppliedStyles(range.commonAncestorContainer).filter(x => x instanceof HTMLElement).map(x => new ElementOptions(x.tagName, attributesToDict(x.attributes)));;

if (parent.firstElementChild){
let options = createOptionsFromElement(parent.firstElementChild);
for (let option of options){
// Special handling for A tags
if (option.tagName == "A"){
this.menu.querySelector("[name='LINK']").parentElement.classList.add("editor-context-menu-active");
} else {
// Iterate over every tag in the menu bar
for (let key of Object.keys(menuOptions.listeners)){
// Check equality with styles[key]
// Use optional chaining
if (styles[key]?.applied.equals(option)){
this.menu.querySelector(`[name='${key}']`).parentElement.classList.add("editor-context-menu-active");
}
options = options.concat(createOptionsFromElement(parent.firstElementChild));
}

for (let option of options){
// Special handling for A tags
if (option.tagName == "A"){
this.menu.querySelector("[name='LINK']").parentElement.classList.add("editor-context-menu-active");
} else {
// Iterate over every tag in the menu bar
for (let key of Object.keys(menuOptions.listeners)){
// Check equality with styles[key]
// Use optional chaining
if (styles[key]?.applied.equals(option)){
this.menu.querySelector(`[name='${key}']`).parentElement.classList.add("editor-context-menu-active");
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion projects/editor/src/editor/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,12 @@ export function extractGreatestParent(range){
* Get all applied style for an element
* TODO: This should probably be called getAllParentElements
*
* @export
* @param {HTMLElement} element - Element to check
* @param {HTMLElement} [max=document.body] - Maxomum parent
* @returns {Array.<HTMLElement>} - All applied styles
*/
function getAllAppliedStyles(element, max = document.body){
export function getAllAppliedStyles(element, max = document.body){
let curr = element;
let styles = [curr];
while (curr.parentElement && curr.parentElement != max){
Expand Down

0 comments on commit 8f0366c

Please sign in to comment.