Skip to content

Commit

Permalink
Updated to Plugin standards
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinRigsby committed Feb 27, 2024
1 parent a73a9d5 commit 5d090f6
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 29 deletions.
28 changes: 20 additions & 8 deletions mystyles.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.markdown-rendered pre .obsidian-embed-code-file {
.markdown-rendered pre .embed-code-file {
position: absolute;
top: 5px;
left: 0px;
Expand All @@ -18,7 +18,7 @@
top: 8px!important;
}

.markdown-rendered div pre button.obsidian-markdown-code-edit {
.markdown-rendered div pre button.markdown-code-edit {
display:none;
margin: 6px;
padding: 6px 8px;
Expand All @@ -33,21 +33,21 @@
right: 110px;
}

.markdown-rendered div pre:hover button.obsidian-markdown-code-edit {
.markdown-rendered div pre:hover button.markdown-code-edit {
display: block;
}

.markdown-rendered div pre:hover button.copy-code-button {
display: block;
}

.markdown-rendered div pre:hover button.obsidian-markdown-code-edit:hover {
.markdown-rendered div pre:hover button.markdown-code-edit:hover {
box-shadow: var(--input-shadow);
background-color: var(--background-modifier-hover);
}


#embeded-code > pre > button.obsidian-markdown-code-refresh{
#embeded-code > pre > button.markdown-code-refresh{
display:none;
margin: 6px;
padding: 6px 8px;
Expand All @@ -62,17 +62,17 @@
right: 30px;
}

#embeded-code > pre > button.obsidian-markdown-code-refresh > svg{
#embeded-code > pre > button.markdown-code-refresh > svg{
fill:var(--text-muted);
}


#embeded-code > pre:hover > button.obsidian-markdown-code-refresh {
#embeded-code > pre:hover > button.markdown-code-refresh {
display: block;
}


#embeded-code > pre:hover > button.obsidian-markdown-code-refresh:hover {
#embeded-code > pre:hover > button.markdown-code-refresh:hover {
box-shadow: var(--input-shadow);
background-color: var(--background-modifier-hover);
}
Expand All @@ -97,4 +97,16 @@
height: 6rem !important;
}

.setting-elm {
min-width: 2.3em;
text-align: right;
}

.dialog-height {
height: var(--dialog-height)
}

.btn-close-modal {
background: var(--modal-background);
z-index: 9999;
}
3 changes: 1 addition & 2 deletions src/codeFilesSettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ export class CodeFilesSettingsTab extends PluginSettingTab {
}))
.settingEl.createDiv('', (el) => {
fontSizeText = el;
el.style.minWidth = "2.3em";
el.style.textAlign = "right";
el.className = "setting-elm"
el.innerText = " " + this.plugin.settings.fontSize.toString();
});

Expand Down
12 changes: 10 additions & 2 deletions src/createCodeFileModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,21 @@ export class CreateCodeFileModal extends Modal {
async complete() {
this.close();
console.log(this.parent);
const parent = (this.parent instanceof TFile ? this.parent.parent : this.parent) as TFolder;
let parent = null;

if (this.parent instanceof TFile){
parent = this.parent.parent as TFolder;
}
else {
parent = this.parent as TFolder;
}

const newPath = `${parent.path}/${this.fileName}.${this.fileExtension}`;
const existingFile = this.app.vault.getAbstractFileByPath(normalizePath(newPath));
if (existingFile && existingFile instanceof TFile) {
new Notice("File already exists");
const leaf = this.app.workspace.getLeaf(true);
leaf.openFile(existingFile as any);
leaf.openFile(existingFile);
return;
}

Expand Down
5 changes: 2 additions & 3 deletions src/fenceEditModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ export class FenceEditModal extends Modal {
"--dialog-width": "90vw",
"--dialog-height": "90vh",
});
this.modalEl.style.height = "var(--dialog-height)";
this.modalEl.classList.add("dialog-height")

let closeButton = this.modalEl.querySelector<HTMLDivElement>(
".modal-close-button"
)
closeButton!.style.background = "var(--modal-background)";
closeButton!.style.zIndex = "9999";
this.modalEl.classList.add(".btn-close-modal")
}

onClose() {
Expand Down
40 changes: 26 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class EmbeddedCode{
}

addModifyButton() {
let editButton = this.container.querySelector('.obsidian-markdown-code-edit') as HTMLButtonElement;
let editButton = this.container.querySelector('.markdown-code-edit') as HTMLButtonElement;

if (editButton){
editButton.addEventListener('click', async () => {
Expand All @@ -324,7 +324,7 @@ class EmbeddedCode{
new Notice(`Cannot locate Edit Button`);
}

let refreshButton = this.container.querySelector('.obsidian-markdown-code-refresh') as HTMLButtonElement;
let refreshButton = this.container.querySelector('.markdown-code-refresh') as HTMLButtonElement;

if (refreshButton){
refreshButton.addEventListener('click', async () => {
Expand Down Expand Up @@ -384,18 +384,21 @@ class EmbeddedCode{
// Creates Title
let titleElement = document.createElement("pre");
titleElement.appendText(title);
titleElement.className = "obsidian-embed-code-file";
titleElement.className = "embed-code-file";
pre.prepend(titleElement);

// Creates Modify Button
let editButton = document.createElement("button");
editButton.appendText("Edit")
editButton.addClass("obsidian-markdown-code-edit");
editButton.addClass("markdown-code-edit");
pre.append(editButton)

let refreshButton = document.createElement("button");
refreshButton.addClass("obsidian-markdown-code-refresh");
refreshButton.addClass("markdown-code-refresh");
refreshButton.ariaLabel = "Refresh Content";

// cannot use createEl as svg is not accepted
// static image with no input
refreshButton.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="20" height="20" viewBox="0 0 30 30">
<path d="M 15 3 C 12.031398 3 9.3028202 4.0834384 7.2070312 5.875 A 1.0001 1.0001 0 1 0 8.5058594 7.3945312 C 10.25407 5.9000929 12.516602 5 15 5 C 20.19656 5 24.450989 8.9379267 24.951172 14 L 22 14 L 26 20 L 30 14 L 26.949219 14 C 26.437925 7.8516588 21.277839 3 15 3 z M 4 10 L 0 16 L 3.0507812 16 C 3.562075 22.148341 8.7221607 27 15 27 C 17.968602 27 20.69718 25.916562 22.792969 24.125 A 1.0001 1.0001 0 1 0 21.494141 22.605469 C 19.74593 24.099907 17.483398 25 15 25 C 9.80344 25 5.5490109 21.062074 5.0488281 16 L 8 16 L 4 10 z"></path>
</svg>`;
Expand Down Expand Up @@ -448,8 +451,8 @@ class EmbeddedCode{

this.render().then();

let htmlContent = element.innerHTML;
element.innerHTML = htmlContent;
//var htmlContent = element.innerHTML;
//element.innerHTML = htmlContent;
}

tryRefresh(space: Workspace, currentFile: string, codePath: string, interval: number, timeout: number) {
Expand All @@ -463,7 +466,7 @@ class EmbeddedCode{
//Handle refreshing page

space.containerEl.findAll("#embeded-code").forEach (element =>{
let elementPath = element.find('.filepath')?.textContent;
var elementPath = element.find('.filepath')?.textContent;
if (elementPath == codePath)
{
this.Refresh(space, element);
Expand Down Expand Up @@ -614,19 +617,26 @@ export default class EmbedAndEditCode extends Plugin {
let targetTop = targetRect.top;
let targetBottom = targetRect.bottom;
let targeRight = targetRect.right
node.style.position = "absolute";
node.style.left = `${x + gep}px`;

let left = `${x + gep}px`;
let top = '';

let spaceBelow = window.innerHeight - y - gep * 3;
let spaceAbove = y - gep * 3;
if (spaceBelow > h) {
node.style.top = `${targetBottom + gep}px`;
top = `${targetBottom + gep}px`;
} else if (spaceAbove > h) {
node.style.top = `${targetTop - h - gep}px`;
top = `${targetTop - h - gep}px`;
} else {
node.style.top = `${targetTop - (h / 2) - gep}px`;
node.style.left = `${targeRight + gep * 2}px`;
top = `${targetTop - (h / 2) - gep}px`;
left = `${targeRight + gep * 2}px`;
}

node.setCssProps({
"postion": "absolution",
"left": left,
"top" : top
})
}

contentEl.setCssProps({
Expand Down Expand Up @@ -657,6 +667,8 @@ export default class EmbedAndEditCode extends Plugin {
}

onunload() {
const openLeaves = this.app.workspace.getLeavesOfType(viewType);
openLeaves.forEach((leaf) => leaf.detach());
this.observer.disconnect();
}

Expand Down

0 comments on commit 5d090f6

Please sign in to comment.