Skip to content

Commit

Permalink
feat: added event update for markdown change and disable-autosave (#1364
Browse files Browse the repository at this point in the history
)
  • Loading branch information
srijitcoder authored Nov 12, 2024
1 parent eaf9384 commit 7fb0cd7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
8 changes: 8 additions & 0 deletions elements/storytelling/src/components/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class StoryTellingEditor extends LitElement {
storyId: { attribute: "story-id", type: String },
showEditor: { attribute: "show-editor", type: String },
isNavigation: { attribute: "markdown", type: Boolean },
disableAutosave: { attribute: "disable-autosave", type: Boolean },
};

constructor() {
Expand Down Expand Up @@ -52,6 +53,13 @@ class StoryTellingEditor extends LitElement {
*/
this.showEditor = undefined;

/**
* Disable auto save
*
* @type {Boolean}
*/
this.disableAutosave = false;

// Bind methods to the instance
this.disableTextSelection = this.disableTextSelection.bind(this);
this.enableTextSelection = this.enableTextSelection.bind(this);
Expand Down
16 changes: 11 additions & 5 deletions elements/storytelling/src/helpers/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,17 @@ export function runWhenEditorInitialised(StoryTellingEditor) {
if (StoryTellingEditor.editor.editor?.editors?.["root.Story"]) {
const easyMDEInstance =
StoryTellingEditor.editor.editor.editors["root.Story"].simplemde_instance;
generateAutoSave(
StoryTellingEditor,
StoryTellingEditor.storyId,
easyMDEInstance,
);

if (!StoryTellingEditor.disableAutosave) {
generateAutoSave(
StoryTellingEditor,
StoryTellingEditor.storyId,
easyMDEInstance,
);
} else {
const saveEle = StoryTellingEditor.querySelector(".editor-saver");
saveEle.innerText = "";
}
preventEditorOutsideScroll(StoryTellingEditor);
} else {
setTimeout(() => runWhenEditorInitialised(StoryTellingEditor), 100);
Expand Down
21 changes: 20 additions & 1 deletion elements/storytelling/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class EOxStoryTelling extends LitElement {
showNav: { attribute: "show-nav", type: Boolean },
showEditor: { attribute: "show-editor", type: String },
noShadow: { attribute: "no-shadow", type: Boolean },
disableAutosave: { attribute: "disable-autosave", type: Boolean },
unstyled: { type: Boolean },
addCustomSectionIndex: { type: Number, state: true },
selectedCustomElement: { type: Object, state: true },
Expand Down Expand Up @@ -94,6 +95,13 @@ export class EOxStoryTelling extends LitElement {
*/
this.noShadow = false;

/**
* Disable auto save
*
* @type {Boolean}
*/
this.disableAutosave = false;

/**
* Enable or disable editor
*
Expand Down Expand Up @@ -155,6 +163,14 @@ export class EOxStoryTelling extends LitElement {

// Check if 'markdown' property itself has changed and generate sanitized html
if (changedProperties.has("markdown")) {
if (this.markdown) {
this.dispatchEvent(
new CustomEvent("changed", {
detail: this.markdown,
}),
);
}

const unsafeHTML = md.render(this.markdown);

validateMarkdownAttrs(md.attrs.sections, this);
Expand Down Expand Up @@ -230,7 +246,9 @@ export class EOxStoryTelling extends LitElement {
}
}, 1000);

initSavedMarkdown(this);
if (!this.disableAutosave) {
initSavedMarkdown(this);
}
addLightBoxScript(this);

// Check if this.#html is initialized, if not, wait for it
Expand Down Expand Up @@ -276,6 +294,7 @@ export class EOxStoryTelling extends LitElement {
show-editor=${this.showEditor}
@change=${this.#debounceUpdateMarkdown}
.markdown=${this.markdown}
.disableAutosave=${this.disableAutosave}
></eox-storytelling-editor>
`,
)}
Expand Down
22 changes: 11 additions & 11 deletions package-lock.json

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

0 comments on commit 7fb0cd7

Please sign in to comment.