-
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Few changes in the AddOrEditTaskModal #59
Comments
|
I am going to use the code from the following link and create a new file in my project : https://github.com/mgmeyers/obsidian-kanban/blob/main/src/components/Editor/MarkdownEditor.tsx Then I will update the below code in my AddOrEditTaskModal to use this MarkdownEditor : To embed the `MarkdownEditor` inside the `EditTaskContent` component and replace the existing `textarea`, here’s how you can modify the code.
### Steps:
1. **Create the `EmbeddedMarkdownEditor.tsx` file**:
Place the provided `MarkdownEditor` code in the `EmbeddedMarkdownEditor.tsx` file as planned.
2. **Modify `EditTaskContent` to Use `MarkdownEditor`**:
Replace the existing `textarea` with the `MarkdownEditor` from your `EmbeddedMarkdownEditor.tsx` file, while handling the necessary props, such as `value`, `onChange`, `onSubmit`, etc.
Here is how you can update the `EditTaskContent` component to use the new `MarkdownEditor`:
### Code Changes in `EditTaskContent`:
Replace the `textarea` block with the following:
```tsx
// Import the MarkdownEditor from the EmbeddedMarkdownEditor file
import { MarkdownEditor } from './Editor/EmbeddedMarkdownEditor';
// ...
const EditTaskContent: React.FC<{ app: App, plugin: TaskBoard, root: HTMLElement, task?: taskItem, taskExists?: boolean, filePath: string; onSave: (updatedTask: taskItem) => void; onClose: () => void }> = ({ app, plugin, root, task = taskItemEmpty, taskExists, filePath, onSave, onClose }) => {
// Replace the textarea state handler with the MarkdownEditor state handler
const [taskContent, setTaskContent] = useState(task.body ? task.body.join('\n') : '');
// Function to handle the submission of the task from MarkdownEditor
const handleMarkdownSubmit = (editor: EditorView) => {
const updatedContent = editor.state.doc.toString();
const updatedTask = parseTaskContent(updatedContent);
setTaskContent(updatedContent);
setUpdatedTask(updatedTask);
onSave(updatedTask);
onClose();
};
return (
<div className="EditTaskModalHome">
{/* Your other code */}
{/* Conditional rendering based on active tab */}
<div className={`EditTaskModalTabContent ${activeTab === 'preview' ? 'show' : 'hide'}`}>
{/* Preview Section */}
<div className="EditTaskModalHomePreview" style={{ display: activeTab === 'preview' ? 'block' : 'none' }}>
{/* Your Preview logic */}
</div>
</div>
<div className={`EditTaskModalTabContent ${activeTab === 'editor' ? 'show' : 'hide'}`}>
<div className="EditTaskModalHomePreviewHeader">
Directly Edit any value or add more sub tasks and description for this task.
</div>
{/* Embed the MarkdownEditor */}
<MarkdownEditor
value={taskContent}
onChange={(update) => setTaskContent(update.state.doc.toString())}
onSubmit={handleMarkdownSubmit}
className="EditTaskModalBodyDescription"
placeholder="Body content"
/>
</div>
{/* Rest of your component code */}
<button className="EditTaskModalHomeSaveBtn" onClick={handleSave}>Save</button>
</div>
);
};
// Rest of your modal logic Key Changes:
By following these steps, you'll be able to replace the standard
|
Following is the simplest code GPT gave using the codeMirror library, just sharing here to refer, because I am deleting the below file /src/components/MarkdownEditor.tsx : // /src/components/MarkdownEditor.tsx
import { EditorView, basicSetup } from 'codemirror';
import React, { useEffect, useRef, useState } from 'react';
import { markdown, markdownLanguage } from '@codemirror/lang-markdown';
import { indentWithTab } from '@codemirror/commands';
import { keymap } from '@codemirror/view';
import { oneDark } from '@codemirror/theme-one-dark';
class MarkdownEditor {
private editorView: EditorView | null = null;
private onChangeCallback: (value: string) => void;
constructor(onChangeCallback: (value: string) => void) {
this.onChangeCallback = onChangeCallback;
}
initializeEditor(editorContainer: HTMLDivElement, initialValue: string) {
if (this.editorView) {
this.editorView.destroy();
}
this.editorView = new EditorView({
doc: initialValue,
extensions: [
basicSetup,
markdown({ base: markdownLanguage }),
oneDark,
keymap.of([indentWithTab]), // Correctly wrap keybindings with `keymap.of()`
EditorView.updateListener.of((update) => {
if (update.docChanged) {
const currentValue = this.editorView?.state.doc.toString() || '';
this.onChangeCallback(currentValue);
}
}),
],
parent: editorContainer,
});
}
destroyEditor() {
if (this.editorView) {
this.editorView.destroy();
this.editorView = null;
}
}
getEditorValue(): string {
return this.editorView?.state.doc.toString() || '';
}
static extractIndentedLines(content: string): string[] {
return content
.split('\n')
.filter((line) => /^\s+[^- \[]/.test(line)); // lines with indentation not starting with `- [ ]`
}
}
export default function CodeMirrorEditor({ initialContent, onChange }: { initialContent: string, onChange: (bodyContent: string[]) => void }) {
const editorRef = useRef<HTMLDivElement>(null);
const [editorInstance, setEditorInstance] = useState<MarkdownEditor | null>(null);
useEffect(() => {
if (editorRef.current) {
const editor = new MarkdownEditor((value: string) => {
const indentedLines = MarkdownEditor.extractIndentedLines(value);
onChange(indentedLines);
});
editor.initializeEditor(editorRef.current, initialContent);
setEditorInstance(editor);
return () => {
editor.destroyEditor();
};
}
}, [initialContent, onChange]);
return <div ref={editorRef} className="markdown-editor"></div>;
} |
Creating a new branch for this Markdown Editor Implementation called |
The required functionalities of this issue has been achieved, the MarkdownEditor functionality will be continued in the following issue : #50 Closing this issue. |
Move the section of adding SubTasks right below the Main Title
Then There will be directly the Preview Section. In this preview Section there will be a button 'Edit', pressing this button will move the preview section below. And above this preview section the TextArea component will open, in which the whole content of the task, same to same, as you see in the MD file in source mode will be there and the user can edit the previous content, as well as will able to add more lines for SubTasks for multilevel subtasks or description lines. (I can show display short message to user, after they open this textArea, that 'They can edit the previous fields as well as add more lines for description or multilevel subtasks.')
Also, the subTasks are not getting detected/displayed in this modal, must be a small issue due to the indentations have been added.
The text was updated successfully, but these errors were encountered: