digi-mathchem-editor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # digi-mathchem-editor
2
+
3
+ A standalone custom Math and Chemistry editor package. It provides an intuitive UI for rendering Math and Chemistry formulas using MathLive.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install digi-mathchem-editor
9
+ ```
10
+
11
+ ```bash
12
+ npm install digi-mathchem-editor mathlive @fortawesome/fontawesome-svg-core @fortawesome/free-solid-svg-icons @fortawesome/react-fontawesome
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```jsx
18
+ import React, { useState } from 'react';
19
+ import { CustomMathEditor } from 'digi-mathchem-editor';
20
+ import 'digi-mathchem-editor/dist/style.css'; // Don't forget to import the styles!
21
+
22
+ function App() {
23
+ const [isEditorOpen, setIsEditorOpen] = useState(false);
24
+ const [mode, setMode] = useState('math'); // 'math' or 'chem'
25
+
26
+ const handleInsert = (latex) => {
27
+ console.log("Inserted LaTeX:", latex);
28
+ // Handle the inserted latex formula...
29
+ };
30
+
31
+ return (
32
+ <div>
33
+ <button onClick={() => { setMode('math'); setIsEditorOpen(true); }}>
34
+ Open Math Editor
35
+ </button>
36
+ <button onClick={() => { setMode('chem'); setIsEditorOpen(true); }}>
37
+ Open Chemistry Editor
38
+ </button>
39
+
40
+ <CustomMathEditor
41
+ isOpen={isEditorOpen}
42
+ mode={mode}
43
+ initialValue=""
44
+ onInsert={handleInsert}
45
+ onClose={() => setIsEditorOpen(false)}
46
+ />
47
+ </div>
48
+ );
49
+ }
50
+
51
+ export default App;
52
+ ```
53
+
54
+ ## Props
55
+
56
+ - `isOpen` (boolean): Controls the visibility of the editor popup.
57
+ - `mode` (string): Either `'math'` or `'chem'`.
58
+ - `initialValue` (string): Optional initial LaTeX value for the editor.
59
+ - `onInsert` (function): Callback function triggered when the user clicks the insert button. Receives the `latex` string as an argument.
60
+ - `onClose` (function): Callback function triggered when the user closes the editor.
61
+
62
+
63
+
64
+
65
+
66
+
67
+
68
+ ## Using in Angular
69
+
70
+ Since `digi-mathchem-editor` is a React component, you can use it in Angular by rendering it dynamically into an Angular element using `react` and `react-dom`.
71
+
72
+ **1. Install React dependencies:**
73
+ ```bash
74
+ npm install react react-dom
75
+ ```
76
+
77
+ ```bash
78
+ npm install digi-mathchem-editor react react-dom mathlive @fortawesome/fontawesome-svg-core @fortawesome/free-solid-svg-icons @fortawesome/react-fontawesome
79
+
80
+ ```
81
+
82
+ **2. Create an Angular wrapper component:**
83
+
84
+ ```typescript
85
+ import { Component, ElementRef, OnInit, OnDestroy, ViewChild, ViewEncapsulation } from '@angular/core';
86
+ import * as React from 'react';
87
+ import { createRoot } from 'react-dom/client';
88
+ import { CustomMathEditor } from 'digi-mathchem-editor';
89
+ // Import the styles in your styles.scss or component
90
+ import 'digi-mathchem-editor/dist/style.css';
91
+
92
+ @Component({
93
+ selector: 'app-math-editor-wrapper',
94
+ template: \`<div #editorContainer></div>\`,
95
+ encapsulation: ViewEncapsulation.None
96
+ })
97
+ export class MathEditorWrapperComponent implements OnInit, OnDestroy {
98
+ @ViewChild('editorContainer', { static: true }) editorContainer!: ElementRef;
99
+ private root: any;
100
+
101
+ ngOnInit() {
102
+ // Create a React root
103
+ this.root = createRoot(this.editorContainer.nativeElement);
104
+ this.renderReactComponent();
105
+ }
106
+
107
+ ngOnDestroy() {
108
+ // Clean up React tree when Angular component is destroyed
109
+ if (this.root) {
110
+ this.root.unmount();
111
+ }
112
+ }
113
+
114
+ renderReactComponent() {
115
+ this.root.render(
116
+ React.createElement(CustomMathEditor, {
117
+ isOpen: true,
118
+ mode: 'math',
119
+ initialValue: '',
120
+ onInsert: (latex: string) => {
121
+ console.log("Formula inserted:", latex);
122
+ // Handle logic...
123
+ },
124
+ onClose: () => {
125
+ console.log("Editor closed");
126
+ }
127
+ })
128
+ );
129
+ }
130
+ }
131
+ ```
132
+
133
+ This method mounts the React component securely inside your Angular view, allowing them to coexist and pass data smoothly.