Skip to content

Commit

Permalink
fileClass & readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelobelle committed May 23, 2021
1 parent 9d13c20 commit 9f739a5
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 20 deletions.
76 changes: 69 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ a.internal-link[data-link-next-actions]:hover::after{

<img src="https://raw.githubusercontent.com/mdelobelle/obsidian_supercharged_links/master/images/link-styling-hover-in-note.png" style="width:500px">

### Some videos examples
### Some demo

#### Internal link simple styling
https://youtu.be/tyEdsmAQb_4
Expand All @@ -133,7 +133,7 @@ Right click on a link will automatically display an item per target note's front
1. Change the value in the modal's prompt
1. Type `enter` to save or click X or `esc` to cancel

example:
demo:
https://youtu.be/qhtPKstdnhI

### Update boolean property
Expand All @@ -142,7 +142,7 @@ https://youtu.be/qhtPKstdnhI
1. Toggle the swith in the modal to change the value
1. Press `esc` to leave the modal

example:
demo:
https://youtu.be/iwL-HqvoNOs

### Update multiple values property
Expand All @@ -153,7 +153,7 @@ https://youtu.be/iwL-HqvoNOs

**this doesn't work with indented lists YAML format**

example:
demo:
https://youtu.be/WaW6xElq0T4

### Preset values for property
Expand All @@ -169,7 +169,7 @@ Back in a note Right-click on the link
6. Change the value in the modal's dropdown
7. Click on the save button to save or click X or `esc` to cancel

example:
demo:
https://youtu.be/GryvvJ6qIg4

### Multi select preset values for property
Expand All @@ -184,7 +184,7 @@ Back in a note Right-click on the link
5. Change the values by toggling the switches in the modal
6. Click on the save button to save or click X or `esc` to cancel

example:
demo:
https://youtu.be/iyIG6LmCcuc

### Cycle through preset values
Expand All @@ -196,11 +196,73 @@ Back in a note Right-click on the link

3. Click on " .. > .. " to change the property's value for the next one in the settings list

example:
demo:
https://youtu.be/7BqG4sG15jc

### Add a new property at section

1. Right-click on the link
2. Click on "Add field at section"
3. Select the line in the target file where you want to insert the new field
4. Select the field
5. Select/input the value for this field (if the field has preset values, you will be prompted to choose one)

demo:
https://youtu.be/JYURK2CM3Es

## Manage Authorized / Ignored fields

### Disable field options in context menu

In the settings

1. toggle "display field option in context menu"
If toggled on, the context menu will include field options
If toggled off, the context menu wont include field options

demo:
https://youtu.be/PC3MC0CfG0E

### Ignore fields globally

In the settings

1. Add the fields you don't want to see in your context menu, comma separated

demo:
https://youtu.be/eFkxECqBvvY

### define a class for a file and authorized fields for this class

a class file is basically a simple note
the name of the file will be the name of the class
the lines of the file will be the fields managed for this class

1. Define the folder where you wan't to store your class files
2. Create a note in this folder, let say `music.md`, containing lines with the name of fields that you want to manage for this class
```md
music.md
=========
genre
difficulty
artist
tone
```
3. In a regular note, let's say `Black Dog.md`, insert a frontmatter field named `fileClass`
4. The value of `fileClass` has to be the name of the file Class where you have the fields that you want to manage for this note. e.g in our case
```yaml
---
fileClass: music
---
```
5. when right-clicking on a link to `Black Dog.md`, the fields in the context menu will be filter to show only the ones that are also included in `music.md`

demo:
https://youtu.be/Av7DeYZILUk

## Roadmap

- [x] link context menu to modify frontmatter attributes
- [ ] manage indented lists multi-values frontmatter property
- [x] extend options management to iinline-fields
- [ ] fileClass fields types and validators
2 changes: 1 addition & 1 deletion data.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"targetAttributes":["category","next-actions"],"presetFields":[{"name":"category","values":{"1":"people","2":"movie","3":"book","4":"system"},"id":"1","isCycle":false,"isMulti":true,"valuesListNotePath":""},{"name":"Status","values":{},"id":"2","isCycle":false,"isMulti":false,"valuesListNotePath":"⚙️/Status.md"},{"name":"genre","values":{},"id":"3","isCycle":false,"isMulti":true,"valuesListNotePath":"⚙️/music genre.md"}],"displayFieldsInContextMenu":true,"globallyIgnoredFields":["genre","Tab"]}
{"targetAttributes":["category","next-actions"],"presetFields":[{"name":"category","values":{"1":"people","2":"movie","3":"book","4":"system"},"id":"1","isCycle":false,"isMulti":true,"valuesListNotePath":""},{"name":"Status","values":{},"id":"2","isCycle":false,"isMulti":false,"valuesListNotePath":"⚙️/Status.md"},{"name":"genre","values":{},"id":"3","isCycle":false,"isMulti":true,"valuesListNotePath":"⚙️/music genre.md"}],"displayFieldsInContextMenu":true,"globallyIgnoredFields":[""],"classFilesPath":"🗃/"}
60 changes: 51 additions & 9 deletions src/linkContextMenu/linkContextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,53 @@ class linkContextMenu {
const cache = this.plugin.app.metadataCache.getCache(abstractFile.path)
if(cache.frontmatter){
const {position, ...attributes} = cache.frontmatter
const filteredAttributes: Record<string, string> = {}
Object.keys(attributes).forEach(key => {
if(!this.plugin.settings.globallyIgnoredFields.includes(key)){
filteredAttributes[key] = attributes[key]
delete attributes.key
}
})
menu.addSeparator()
this.createExtraOptionsListForFrontmatter(filteredAttributes, menu).then(() => {
this.createExtraOptionsListForInlineFields(this.file, menu).then(() => {
menu.addSeparator()
this.addSectionSelectModalOption(this.plugin, menu)
let fileClassForFields = false
let fileClassFields: string[] = []
if(Object.keys(attributes).includes('fileClass')){
const fileClass = attributes['fileClass']
const classFiles = this.plugin.app.vault.getMarkdownFiles().filter(mdFile =>
(mdFile.path == `${this.plugin.settings.classFilesPath}${fileClass}.md`))
if(classFiles.length > 0){
const classFile = classFiles[0]
fileClassForFields = true
this.plugin.app.vault.read(classFile).then(result => {
result.split('\n').forEach(line => {
fileClassFields.push(line)
})
Object.keys(attributes).forEach(key => {
if(!fileClassFields.includes(key)){
delete attributes.key
}
})
this.createExtraOptionsListForFrontmatter(attributes, menu).then(() => {
this.createExtraOptionsListForInlineFields(this.file, menu, fileClassForFields, fileClassFields).then(() => {
menu.addSeparator()
this.addSectionSelectModalOption(this.plugin, menu)
})
})
})
} else {
this.createExtraOptionsListForFrontmatter(attributes, menu).then(() => {
this.createExtraOptionsListForInlineFields(this.file, menu).then(() => {
menu.addSeparator()
this.addSectionSelectModalOption(this.plugin, menu)
})
})
}
} else {
this.createExtraOptionsListForFrontmatter(attributes, menu).then(() => {
this.createExtraOptionsListForInlineFields(this.file, menu).then(() => {
menu.addSeparator()
this.addSectionSelectModalOption(this.plugin, menu)
})
})
})
}
} else {
this.createExtraOptionsListForInlineFields(this.file, menu).then(() => {
menu.addSeparator()
Expand All @@ -61,7 +95,9 @@ class linkContextMenu {

}

async createExtraOptionsListForInlineFields(file:TFile, menu: Menu):Promise<void>{


async createExtraOptionsListForInlineFields(file:TFile, menu: Menu, fileClassForFields: boolean = false, fileClassFields: string[] = []):Promise<void>{
let attributes: Record<string, string> = {}
const regex = new RegExp(/[_\*~`]*([0-9\w\p{Letter}\p{Emoji_Presentation}][-0-9\w\p{Letter}\p{Emoji_Presentation}\s]*)[_\*~`]*\s*::(.+)?/u)
this.plugin.app.vault.read(file).then((result: string) => {
Expand All @@ -70,7 +106,13 @@ class linkContextMenu {
if(regexResult
&& regexResult.length > 0
&& !this.plugin.settings.globallyIgnoredFields.includes(regexResult[1].trim())){
attributes[regexResult[1].trim()] = regexResult.length > 1 && regexResult[2] ? regexResult[2].trim() : ""
if(fileClassForFields){
if(fileClassFields.includes(regexResult[1].trim())){
attributes[regexResult[1].trim()] = regexResult.length > 1 && regexResult[2] ? regexResult[2].trim() : ""
}
}else{
attributes[regexResult[1].trim()] = regexResult.length > 1 && regexResult[2] ? regexResult[2].trim() : ""
}
}
})
if(Object.keys(attributes).length > 0){
Expand Down
55 changes: 55 additions & 0 deletions src/linkContextMenu/sectionContentModal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {TextAreaComponent, Modal, ButtonComponent, ExtraButtonComponent, DropdownComponent, App, TFile} from "obsidian"

export default class sectionContentModal extends Modal {
file: TFile
startLine: number
endLine: number

constructor(app: App, file: TFile,startLine: number, endLine: number){
super(app)
this.file = file
this.startLine = startLine
this.endLine = endLine
}

onOpen(){
this.titleEl.setText("Hello")
const contentDiv = this.contentEl.createDiv()
const inputDiv = contentDiv.createDiv()
const inputEl = new TextAreaComponent(inputDiv)
inputEl.inputEl.setAttr("cols", "50")
inputEl.inputEl.setAttr("rows", "5")
const footerDiv = contentDiv.createDiv({
cls: "frontmatter-textarea-buttons"
})
const positionSelectorContainer = footerDiv.createDiv({
cls: "position-selector-container"
})
const positionDropdown = new DropdownComponent(positionSelectorContainer)
positionDropdown.addOption("1", "begining")
positionDropdown.addOption("2", "end")
positionDropdown.setValue("2")
const saveButton = new ButtonComponent(footerDiv)
saveButton
.setIcon("checkmark")
.onClick(() => {
this.app.vault.read(this.file).then(result => {
let newContent: string[] = []
result.split("\n").forEach((line, lineNumber) =>{
newContent.push(line)
if(lineNumber == this.startLine && positionDropdown.getValue() == "1"){
newContent.push(inputEl.getValue())
}
if(lineNumber == this.startLine && positionDropdown.getValue() == "1"){
newContent.push(inputEl.getValue())
}
})
console.log(newContent.join('\n'))
})
})
const cancelButton = new ExtraButtonComponent(footerDiv)
cancelButton
.setIcon("cross")
.onClick(() => this.close())
}
}
14 changes: 13 additions & 1 deletion src/settings/SuperchargedLinksSettingTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,19 @@ export default class SuperchargedLinksSettingTab extends PluginSettingTab {
text.inputEl.rows = 6;
text.inputEl.cols = 25;
});


/* Set classFiles Path*/
new Setting(containerEl)
.setName('class Files path')
.setDesc('Path to the files containing the authorized fields for a type of note')
.addText((text) => {text
.setPlaceholder('Path/')
.setValue(this.plugin.settings.classFilesPath)
.onChange(async (value) => {
this.plugin.settings.classFilesPath = value
await this.plugin.saveSettings();
})
});

/* Add new property for which we want to preset values*/
new Setting(containerEl)
Expand Down
6 changes: 4 additions & 2 deletions src/settings/SuperchargedLinksSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ export interface SuperchargedLinksSettings {
targetAttributes: Array<string>;
presetFields: Array<Field>;
displayFieldsInContextMenu: boolean;
globallyIgnoredFields: Array<string>
globallyIgnoredFields: Array<string>;
classFilesPath: string
}

export const DEFAULT_SETTINGS: SuperchargedLinksSettings = {
targetAttributes: [],
presetFields: [],
displayFieldsInContextMenu: true,
globallyIgnoredFields: []
globallyIgnoredFields: [],
classFilesPath: ""
}

0 comments on commit 9f739a5

Please sign in to comment.