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

Added new option to highlight text, not background. Fixed a minor TS issue #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
144 changes: 144 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@rollup/plugin-commonjs": "^18.0.0",
"@rollup/plugin-node-resolve": "^11.2.1",
"@rollup/plugin-typescript": "^8.2.1",
"@types/color": "^3.0.3",
"@types/node": "^14.14.37",
"@types/sortablejs": "^1.10.7",
"obsidian": "^0.12.6",
Expand All @@ -23,6 +24,7 @@
},
"dependencies": {
"@simonwep/pickr": "^1.8.2",
"color": "^4.2.3",
"copy": "^0.0.1",
"dotenv": "^10.0.0",
"react": "^17.0.2",
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import contextMenu from "src/plugin/contextMenu";
import highlighterMenu from "src/ui/highlighterMenu";
import { createHighlighterIcons } from "src/icons/customIcons";

import { createStyles } from "src/utils/createStyles";
import { createStyles, getStyles } from "src/utils/createStyles";
export default class HighlightrPlugin extends Plugin {
app: App;
editor: Editor;
Expand Down Expand Up @@ -144,7 +144,7 @@ export default class HighlightrPlugin extends Plugin {
prefix:
this.settings.highlighterMethods === "css-classes"
? `<mark class="hltr-${highlighterKey.toLowerCase()}">`
: `<mark style="background: ${this.settings.highlighters[highlighterKey]};">`,
: `<mark style="${getStyles(this.settings, highlighterKey)}">`,
suffix: "</mark>",
},
};
Expand Down
3 changes: 3 additions & 0 deletions src/settings/settingsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const HIGHLIGHTER_STYLES = [
];

export const HIGHLIGHTER_METHODS = ["css-classes", "inline-styles"];
export const HIGHLIGHTER_TARGETS = ["background", "text", "bold-text"];

export interface Highlighters {
[color: string]: string;
Expand All @@ -15,13 +16,15 @@ export interface Highlighters {
export interface HighlightrSettings {
highlighterStyle: string;
highlighterMethods: string;
highlighterTarget: string;
highlighters: Highlighters;
highlighterOrder: string[];
}

const DEFAULT_SETTINGS: HighlightrSettings = {
highlighterStyle: "none",
highlighterMethods: "inline-styles",
highlighterTarget: "background",
highlighters: {
Pink: "#FFB8EBA6",
Red: "#FF5582A6",
Expand Down
30 changes: 26 additions & 4 deletions src/settings/settingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "obsidian";
import Pickr from "@simonwep/pickr";
import Sortable from "sortablejs";
import { HIGHLIGHTER_METHODS, HIGHLIGHTER_STYLES } from "./settingsData";
import { HIGHLIGHTER_METHODS, HIGHLIGHTER_STYLES, HIGHLIGHTER_TARGETS } from "./settingsData";
import { setAttributes } from "src/utils/setAttributes";

export class HighlightrSettingTab extends PluginSettingTab {
Expand Down Expand Up @@ -52,6 +52,28 @@ export class HighlightrSettingTab extends PluginSettingTab {
});
});

new Setting(containerEl)
.setName("Choose highlight target")
.setDesc(
`Select what should be hightlighted - text or background.`
)
.addDropdown((dropdown) => {
let targets: Record<string, string> = {};
HIGHLIGHTER_TARGETS.map((target) => (targets[target] = target));
dropdown.addOptions(targets);
dropdown
.setValue(this.plugin.settings.highlighterTarget)
.onChange((highlightrTarget) => {
this.plugin.settings.highlighterTarget = highlightrTarget;
setTimeout(() => {
dispatchEvent(new Event("Highlightr-NewCommand"));
}, 100);
this.plugin.saveSettings();
this.plugin.saveData(this.plugin.settings);
this.display();
});
});

const stylesSetting = new Setting(containerEl);

stylesSetting
Expand All @@ -77,9 +99,9 @@ export class HighlightrSettingTab extends PluginSettingTab {
const d = createEl("p");
d.setAttribute("style", "font-size: .925em; margin-top: 12px;");
d.innerHTML = `
<span style="background:#FFB7EACC;padding: .125em .125em;--lowlight-background: var(--background-primary);border-radius: 0;background-image: linear-gradient(360deg,rgba(255, 255, 255, 0) 40%,var(--lowlight-background) 40%) !important;">Lowlight</span>
<span style="background:#93C0FFCC;--floating-background: var(--background-primary);border-radius: 0;padding-bottom: 5px;background-image: linear-gradient(360deg,rgba(255, 255, 255, 0) 28%,var(--floating-background) 28%) !important;">Floating</span>
<span style="background:#9CF09CCC;margin: 0 -0.05em;padding: 0.1em 0.4em;border-radius: 0.8em 0.3em;-webkit-box-decoration-break: clone;box-decoration-break: clone;text-shadow: 0 0 0.75em var(--background-primary-alt);">Realistic</span>
<span style="background:#FFB7EACC;padding: .125em .125em;--lowlight-background: var(--background-primary);border-radius: 0;background-image: linear-gradient(360deg,rgba(255, 255, 255, 0) 40%,var(--lowlight-background) 40%) !important;">Lowlight</span>
<span style="background:#93C0FFCC;--floating-background: var(--background-primary);border-radius: 0;padding-bottom: 5px;background-image: linear-gradient(360deg,rgba(255, 255, 255, 0) 28%,var(--floating-background) 28%) !important;">Floating</span>
<span style="background:#9CF09CCC;margin: 0 -0.05em;padding: 0.1em 0.4em;border-radius: 0.8em 0.3em;-webkit-box-decoration-break: clone;box-decoration-break: clone;text-shadow: 0 0 0.75em var(--background-primary-alt);">Realistic</span>
<span style="background:#CCA9FFCC;margin: 0 -0.05em;padding: 0.125em 0.15em;border-radius: 0.2em;-webkit-box-decoration-break: clone;box-decoration-break: clone;">Rounded</span>`;
return d;
};
Expand Down
2 changes: 1 addition & 1 deletion src/ui/highlighterMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const highlighterMenu = (
plugin: HighlightrPlugin,
settings: HighlightrSettings,
editor: Editor,
event: MouseEvent
event?: MouseEvent
): void => {
if (editor && editor.hasFocus()) {
const cursor = editor.getCursor("from");
Expand Down
10 changes: 9 additions & 1 deletion src/utils/createStyles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HighlightrSettings } from "src/settings/settingsData";
import { setAttributes } from "./setAttributes";
import Color from 'color';

function addNewStyle(selector: any, style: any, sheet: HTMLElement) {
sheet.textContent += selector + `{\n ${style}\n}\n\n`;
Expand All @@ -19,8 +20,15 @@ export function createStyles(settings: HighlightrSettings) {
let colorLowercase = highlighter.toLowerCase();
addNewStyle(
`.hltr-${colorLowercase},\nmark.hltr-${colorLowercase},\n.markdown-preview-view mark.hltr-${colorLowercase}`,
`background: ${settings.highlighters[highlighter]};`,
getStyles(settings, highlighter),
styleSheet
);
});
}

export function getStyles(settings: HighlightrSettings, highlighter: string) {
return settings.highlighterTarget === 'background' ?
`background: ${settings.highlighters[highlighter]};`
:
`color: ${Color(settings.highlighters[highlighter]).opaquer(1)}; ${settings.highlighterTarget === 'bold-text' ? 'font-weight: bold;' : ''} background: transparent;`
}