Skip to content

Commit

Permalink
4.0.0 (#20)
Browse files Browse the repository at this point in the history
- Added Settings for saving pasted attachments automatically to different locations, based on the current file's location.
- Added handling for leading bullet (#19)
  • Loading branch information
jglev authored Sep 4, 2022
1 parent 6a59c79 commit 5e65ab1
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 35 deletions.
216 changes: 184 additions & 32 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import cloneDeep from "lodash.clonedeep";
import {
addIcon,
apiVersion,
App,
base64ToArrayBuffer,
Editor,
Expand Down Expand Up @@ -133,11 +135,17 @@ class PasteModeModal extends FuzzySuggestModal<number> {
}
}

export interface AttachmentLocation {
cursorFilePattern: string;
targetLocation: string;
}

interface PastetoIndentationPluginSettings {
blockquotePrefix: string;
mode: Mode;
saveBase64EncodedFiles: boolean;
saveFilesLocation: string;
saveFilesOverrideLocations: AttachmentLocation[];
apiVersion: number;
escapeCharactersInBlockquotes: boolean;
}
Expand All @@ -146,8 +154,9 @@ const DEFAULT_SETTINGS: PastetoIndentationPluginSettings = {
blockquotePrefix: "> ",
mode: Mode.Markdown,
saveBase64EncodedFiles: false,
saveFilesLocation: "",
apiVersion: 4,
saveFilesLocation: "Attachments",
saveFilesOverrideLocations: [],
apiVersion: 5,
escapeCharactersInBlockquotes: false,
};

Expand Down Expand Up @@ -203,19 +212,37 @@ export default class PastetoIndentationPlugin extends Plugin {
// }
const files = evt.clipboardData.files;
const fileLinks = [];
if (files.length) {
const activeFile = this.app.workspace.getActiveFile();

let filesTargetLocation = this.settings.saveFilesLocation;
let longestMatchingCursorFilePattern = 0;
this.settings.saveFilesOverrideLocations.forEach((location) => {
if (
!(await app.vault.adapter.exists(this.settings.saveFilesLocation))
activeFile.path.startsWith(location.cursorFilePattern) &&
location.cursorFilePattern.length > longestMatchingCursorFilePattern
) {
await app.vault.createFolder(this.settings.saveFilesLocation);
filesTargetLocation = location.targetLocation;
longestMatchingCursorFilePattern =
location.cursorFilePattern.length;
}
});
console.log(229, filesTargetLocation);

if (files.length) {
if (!(await app.vault.adapter.exists(filesTargetLocation))) {
await app.vault.createFolder(filesTargetLocation);
console.log(
234,
await app.vault.adapter.exists(filesTargetLocation)
);
}
}

for (var i = 0; i < files.length; i++) {
const fileObject = files[i];

const fileName = await createImageFileName(
this.settings.saveFilesLocation,
filesTargetLocation,
fileObject.type.split("/")[1]
);

Expand Down Expand Up @@ -267,10 +294,21 @@ export default class PastetoIndentationPlugin extends Plugin {

const leadingWhitespaceMatch = editor
.getLine(editor.getCursor().line)
.match(new RegExp(`^(\\s*)`));
.match(new RegExp(`^(\\s*)(.*)?`));
const leadingWhitespace =
leadingWhitespaceMatch !== null ? leadingWhitespaceMatch[1] : "";

// The length of `- ` / `* `, to accomodate a bullet list:
const additionalLeadingWhitespace =
leadingWhitespaceMatch !== null &&
leadingWhitespaceMatch[2] !== undefined
? " ".repeat(
leadingWhitespaceMatch[2].length > 3
? 3
: leadingWhitespaceMatch[2].length
)
: "";

if (
this.settings.saveBase64EncodedFiles &&
mode !== Mode.CodeBlock &&
Expand All @@ -287,14 +325,12 @@ export default class PastetoIndentationPlugin extends Plugin {
// indexes:
for (let image of images.reverse()) {
const imageFileName = await createImageFileName(
this.settings.saveFilesLocation,
filesTargetLocation,
image.groups.extension
);

if (
!(await app.vault.adapter.exists(this.settings.saveFilesLocation))
) {
await app.vault.createFolder(this.settings.saveFilesLocation);
if (!(await app.vault.adapter.exists(filesTargetLocation))) {
await app.vault.createFolder(filesTargetLocation);
}

await app.vault.adapter.writeBinary(
Expand All @@ -321,7 +357,8 @@ export default class PastetoIndentationPlugin extends Plugin {
if (i === 0) {
return line;
}
return leadingWhitespace + line;

return leadingWhitespace + additionalLeadingWhitespace + line;
});

if (mode === Mode.Text || mode === Mode.Markdown) {
Expand Down Expand Up @@ -606,14 +643,14 @@ class SettingTab extends PluginSettingTab {
.addClass("paste-to-current-indentation-settings-notice");
noticeDiv
.createEl("span", {
text: `The "Paste in Markdown Mode" and "Paste in Markdown (Blockquote) Mode" commands have been disabled, because reading non-text data from the clipboad does not work with this version of Obsidian.`,
text: `The "Paste in Markdown Mode" and "Paste in Markdown (Blockquote) Mode" attachmentOverrideLocations have been disabled, because reading non-text data from the clipboad does not work with this version of Obsidian.`,
})
.addClass("paste-to-current-indentation-settings-notice-text");
}

new Setting(containerEl)
.setName("Paste Mode")
.setDesc("Mode that the paste command will invoke.")
.setDesc("Mode that the paste attachmentLocation will invoke.")
.addDropdown((dropdown) =>
dropdown
.addOption(Mode.Text, "Plain Text")
Expand Down Expand Up @@ -649,23 +686,6 @@ class SettingTab extends PluginSettingTab {
});
});

new Setting(containerEl)
.setName("Attachment folder path")
.setDesc(
`When saving files from the clipboard, place them in this folder.`
)
.addText((text) => {
text
.setValue(
this.plugin.settings.saveFilesLocation ||
DEFAULT_SETTINGS.saveFilesLocation
)
.onChange(async (value) => {
this.plugin.settings.saveFilesLocation = value;
await this.plugin.saveSettings();
});
});

new Setting(containerEl)
.setName("Blockquote Prefix")
.setDesc(
Expand Down Expand Up @@ -703,5 +723,137 @@ class SettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
});
});

const attachmentsEl = containerEl.createEl("div");
attachmentsEl.addClass("attachment-locations");
attachmentsEl.createEl("h3", {
text: "Attachments",
});

new Setting(attachmentsEl)
.setName("Default attachment folder path")
.setDesc(
`When saving files from the clipboard, place them in this folder.`
)
.addText((text) => {
text
.setValue(
this.plugin.settings.saveFilesLocation ||
DEFAULT_SETTINGS.saveFilesLocation
)
.onChange(async (value) => {
this.plugin.settings.saveFilesLocation = value;
await this.plugin.saveSettings();
});
});

const attachmentOverrideLocationsEl = attachmentsEl.createEl("div");
attachmentOverrideLocationsEl.addClass("attachment-locations");
attachmentOverrideLocationsEl.createEl("h4", {
text: "Attachment overrides",
});

const attachmentOverrideLocations =
this.plugin.settings.saveFilesOverrideLocations;
for (const [
attachmentLocationIndex,
attachmentLocation,
] of attachmentOverrideLocations.entries()) {
const attachmentLocationEl =
attachmentOverrideLocationsEl.createEl("div");
attachmentLocationEl.addClass("attachment-override");

let deleteAttachmentLocationPrimed = false;
let attachmentLocationDeletePrimerTimer: ReturnType<
typeof setTimeout
> | null;

new Setting(attachmentLocationEl)
.setName("Current file directory")
.setDesc("If the current file is in this directory...")
.addText((text) => {
text
.setValue(attachmentLocation.cursorFilePattern)
.onChange(async (value) => {
this.plugin.settings.saveFilesOverrideLocations[
attachmentLocationIndex
].cursorFilePattern = value;
await this.plugin.saveSettings();
});
});

new Setting(attachmentLocationEl)
.setName("Saved file target location")
.setDesc("...Save a pasted file into this directory:")
.addText((text) => {
text
.setValue(attachmentLocation.targetLocation)
.onChange(async (value) => {
this.plugin.settings.saveFilesOverrideLocations[
attachmentLocationIndex
].targetLocation = value;
await this.plugin.saveSettings();
});
});

new Setting(attachmentLocationEl)
.setName("Delete location rule")
.addButton((button) => {
button
.setButtonText("Delete")
.setClass("paste-to-current-indentation-settings-delete-button")
.setTooltip("Delete override location")
.onClick(async () => {
if (attachmentLocationDeletePrimerTimer) {
clearTimeout(attachmentLocationDeletePrimerTimer);
}
if (deleteAttachmentLocationPrimed === true) {
this.plugin.settings.saveFilesOverrideLocations.splice(
attachmentLocationIndex,
1
);

await this.plugin.saveSettings();
this.display();
return;
}

attachmentLocationDeletePrimerTimer = setTimeout(
() => {
deleteAttachmentLocationPrimed = false;
attachmentLocationEl.removeClass("primed");
},
1000 * 4 // 4 second timeout
);
deleteAttachmentLocationPrimed = true;
attachmentLocationEl.addClass("primed");

new Notice(
`Click again to delete attachmentLocation ${
attachmentLocationIndex + 1
}`
);
});
});
}

const addattachmentLocationButtonEl =
attachmentOverrideLocationsEl.createEl("div", {
cls: "add-attachmentLocation-button-el",
});

new Setting(addattachmentLocationButtonEl).addButton((button) => {
button
.setButtonText("Add attachment override location")
.setClass("add-attachmentLocation-button")
.onClick(async () => {
this.plugin.settings.saveFilesOverrideLocations.push({
cursorFilePattern: "",
targetLocation: "",
});
await this.plugin.saveSettings();
this.display();
});
});
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-paste-to-current-indentation",
"name": "Paste to Current Indentation",
"version": "3.1.0",
"version": "4.0.0",
"minAppVersion": "0.14.6",
"description": "This plugin allows pasting and marking text as block-quotes at any level of indentation.",
"author": "Jacob Levernier",
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-paste-to-current-indentation",
"version": "3.1.0",
"version": "4.0.0",
"description": "This plugin allows pasting and marking text as block-quotes at any level of indentation.",
"main": "main.js",
"scripts": {
Expand All @@ -24,6 +24,8 @@
"xmldom": "^0.6.0"
},
"dependencies": {
"@types/lodash.clonedeep": "^4.5.7",
"lodash.clonedeep": "^4.5.0",
"moment": "^2.29.3"
}
}
15 changes: 15 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,19 @@

.paste-to-current-indentation-settings-notice-text {
font-style: italic;
}

.primed .paste-to-current-indentation-settings-delete-button {
background: var(--text-error) !important;
color: white !important;
}
.attachment-override {
border-bottom: 5px double var(--background-modifier-border);
padding: 0.5em 0 0.5em 0;
margin: 0.5em 0 0.5em 0
}

.attachment-override:first-of-type {
margin-top: 0;
padding-top: 0;
}
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"3.0.0": "0.14.6",
"3.0.1": "0.14.6",
"3.0.2": "0.14.6",
"3.1.0": "0.14.6"
"3.1.0": "0.14.6",
"4.0.0": "0.14.6"
}
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/lodash.clonedeep@^4.5.7":
version "4.5.7"
resolved "https://registry.yarnpkg.com/@types/lodash.clonedeep/-/lodash.clonedeep-4.5.7.tgz#0e119f582ed6f9e6b373c04a644651763214f197"
integrity sha512-ccNqkPptFIXrpVqUECi60/DFxjNKsfoQxSQsgcBJCX/fuX1wgyQieojkcWH/KpE3xzLoWN/2k+ZeGqIN3paSvw==
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.14.184"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.184.tgz#23f96cd2a21a28e106dc24d825d4aa966de7a9fe"
integrity sha512-RoZphVtHbxPZizt4IcILciSWiC6dcn+eZ8oX9IWEYfDMcocdd42f7NPI6fQj+6zI8y4E0L7gu2pcZKLGTRaV9Q==

"@types/node@*":
version "17.0.25"
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.25.tgz#527051f3c2f77aa52e5dc74e45a3da5fb2301448"
Expand Down Expand Up @@ -1090,6 +1102,11 @@ locate-path@^2.0.0:
p-locate "^2.0.0"
path-exists "^3.0.0"

lodash.clonedeep@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
integrity sha512-H5ZhCF25riFd9uB5UCkVKo61m3S/xZk1x4wA6yp/L3RFP6Z/eHH1ymQcGLo7J3GMPfm0V/7m1tryHuGVxpqEBQ==

loose-envify@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
Expand Down

0 comments on commit 5e65ab1

Please sign in to comment.