Skip to content

Commit

Permalink
feat(dice-mod): add setting for template folder to prevent applicatio… (
Browse files Browse the repository at this point in the history
#263)

* feat(dice-mod): add setting for template folder to prevent application of dice-mod in templates

add setting to prevent full application of dice-mod in live preview

* fix: Adds folder suggester

* fix: Only check if a file is in a template folder if we're actually modifying

---------

Co-authored-by: Jeremy Valentine <[email protected]>
  • Loading branch information
twiescha and valentine195 authored Nov 9, 2023
1 parent 4090d0b commit 0c02b78
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 47 deletions.
16 changes: 13 additions & 3 deletions src/assets/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,26 @@
margin: 0 6px;
}

.dice-roller-setting-additional-container
> .additional
> .setting-item
> .setting-item-control
> .dice-mod-template-use-subfolders {
margin: 0 px;
font-style: italic;
font-size: small;
}

.dice-roller-setting-additional-container .add-new-formula {
margin: 0 1rem;
padding: 1rem 1rem 0 1rem;
border-radius: 0.5rem;
box-shadow: 0 0 0.25rem var(--background-modifier-box-shadow);
}

.dice-roller-setting-additional-container
.add-new-formula
.formula-data
.dice-roller-setting-additional-container
.add-new-formula
.formula-data
.setting-item {
border: 0;
}
Expand Down
21 changes: 16 additions & 5 deletions src/live-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
} from "obsidian";
import DiceRollerPlugin from "./main";
import { BasicRoller } from "./roller/roller";
import { isTemplateFolder } from "./utils/util";

function selectionAndRangeOverlap(
selection: EditorSelection,
Expand All @@ -63,7 +64,7 @@ function selectionAndRangeOverlap(
function inlineRender(view: EditorView, plugin: DiceRollerPlugin) {
// still doesn't work as expected for tables and callouts

const currentFile = app.workspace.getActiveFile();
const currentFile = this.app.workspace.getActiveFile();
if (!currentFile) return;

const widgets: Range<Decoration>[] = [];
Expand All @@ -86,8 +87,19 @@ function inlineRender(view: EditorView, plugin: DiceRollerPlugin) {
// symbols) overlap
if (selectionAndRangeOverlap(selection, start, end + 1)) return;


const original = view.state.doc.sliceString(start, end).trim();
if (/^dice\-mod:\s*([\s\S]+)\s*?/.test(original)) {

const isTemplate =
isTemplateFolder(
plugin.data.diceModTemplateFolders,
currentFile
)
if (
/^dice\-mod:\s*([\s\S]+)\s*?/.test(original) &&
!isTemplate &&
plugin.data.replaceDiceModInLivePreview
) {
let [, content] = original.match(
/dice\-mod:\s*([\s\S]+)\s*?/
);
Expand Down Expand Up @@ -124,13 +136,12 @@ function inlineRender(view: EditorView, plugin: DiceRollerPlugin) {
const transaction = view.state.update({ changes: mod });
view.dispatch(transaction);
});

return;
}

if (!/^dice(?:\+|\-)?:\s*([\s\S]+)\s*?/.test(original)) return;
if (!/^dice(?:\+|\-|\-mod)?:\s*([\s\S]+)\s*?/.test(original)) return;
let [, content] = original.match(
/^dice(?:\+|\-)?:\s*([\s\S]+)\s*?/
/^dice(?:\+|\-|\-mod)?:\s*([\s\S]+)\s*?/
);
const roller = plugin.getRollerSync(content, currentFile.path);

Expand Down
9 changes: 8 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { inlinePlugin } from "./live-preview";
import API from "./api/api";
import { DEFAULT_ICONS, DiceIcon } from "./view/view.icons";
import copy from "fast-copy";
import { isTemplateFolder } from "./utils/util";
/* import GenesysView, { GENESYS_VIEW_TYPE } from "./view/genesys"; */
String.prototype.matchAll =
String.prototype.matchAll ||
Expand Down Expand Up @@ -176,6 +177,8 @@ interface DiceRollerSettings {
icons: DiceIcon[];

showRenderNotice: boolean;
diceModTemplateFolders: Record<string, boolean>;
replaceDiceModInLivePreview: boolean;
}

export const DEFAULT_SETTINGS: DiceRollerSettings = {
Expand Down Expand Up @@ -209,7 +212,9 @@ export const DEFAULT_SETTINGS: DiceRollerSettings = {
round: Round.None,
initialDisplay: ExpectedValue.Roll,
icons: copy(DEFAULT_ICONS),
showRenderNotice: true
showRenderNotice: true,
diceModTemplateFolders: {},
replaceDiceModInLivePreview: true
};

export default class DiceRollerPlugin extends Plugin {
Expand Down Expand Up @@ -396,6 +401,8 @@ export default class DiceRollerPlugin extends Plugin {
/^dice\-mod:\s*([\s\S]+)\s*?/.test(node.innerText) &&
info
) {
if (isTemplateFolder(this.data.diceModTemplateFolders, file))
continue;
try {
if (!replacementFound) {
fileContent = (
Expand Down
Loading

0 comments on commit 0c02b78

Please sign in to comment.