Skip to content

Commit 6b64955

Browse files
committed
Update custom functions documentation
1 parent 942bdff commit 6b64955

4 files changed

Lines changed: 75 additions & 11 deletions

File tree

docs/_config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ nav_external_links:
2929
- title: Raise an Issue
3030
url: https://github.com/hrushikeshrv/guimath/issues
3131
opens_in_new_tab: true
32+
- title: Ask a Question
33+
url: https://github.com/hrushikeshrv/guimath/discussions
34+
opens_in_new_tab: true
3235

3336
favicon_ico: "/media/favicon.png"

docs/api/components.md

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,56 +28,94 @@ If you want to add a function to the editor widget that is not present out of th
2828

2929
To identify which component class you should inherit from, determine how many blocks your function has. For example, a fraction has two blocks - the numerator and the denominator, and the square root function has just one block. As a guideline, you can usually consider each `{}` in the LaTeX representation of a function as a block - fraction is written as `\frac{}{}`, therefore it has 2 blocks, and square root is written as `\sqrt{}`, and therefore has only 1 block.
3030

31-
Once you determine which component class to inherit from, you will need to override the `toHTML()` and `toLatex()` methods.
31+
Once you determine which component class to inherit from, you will need to override the `toHTML()` and `toLatex()` methods.
32+
33+
For any questions about implementing custom components, please [start a discussion on GitHub](https://github.com/hrushikeshrv/guimath/discussions)
3234

3335
## Defining `toHTML(cursorBlock, cursorPosition)`
34-
This is where you will be able to define how the HTML is generated for your function. See the inheritance examples below to get a quick idea of how to implement this method. `toHTML()` takes two arguments:
36+
This is where you will be able to define how the HTML is generated for your function. See the examples below to get a quick idea of how to implement this method. `toHTML()` is supplied two arguments:
3537

3638
1. `cursorBlock` - The `Block` instance that the cursor is currently in
3739
2. `cursorPosition` - The position of the cursor within the block it is in (`Number`)
3840

39-
You will likely not need to use these arguments yourself. They are used during rendering to provide feedback for hover and click events, and to highlight which `Block` the cursor is currently in. All this is handled by the `GUIMath` and `Block` class for you. However, it is important that you pass these same arguments as they are to any recursive calls to `toHTML()` you make in your definition to preserve this functionality.
41+
You will likely not need to use these arguments yourself in your implementation. They are used during rendering to provide feedback for hover and click events, and to highlight which `Block` the cursor is currently in. All this is handled by the `GUIMath` and `Block` class for you. However, it is important that you pass these same arguments to any recursive calls to `toHTML()` you make to preserve this functionality.
4042

41-
You should return a string containing what the HTML representation of the component should look like in the editor window.
43+
You should return a string containing what the HTML representation of the component should look like in the editor window. For more guidance on how to implement this method, see the examples below or [start a discussion on GitHub](https://github.com/hrushikeshrv/guimath/discussions).
4244

4345
## Defining `toLatex()`
44-
This is where you will be able to define how the LaTeX is generated for your function. Inside the `toLatex()` method, you can access the `blocks` attribute of the instance, and call `toLatex()` for each block to construct the final LaTeX expression step-by-step. There is an example implementation under each heading below to demonstrate.
46+
This is where you will be able to define how the LaTeX is generated for your function. See the examples below to get a quick idea of how to implement this method.
47+
48+
Inside the `toLatex()` method, you can access the `blocks` attribute of the `GUIMath` instance, and call `toLatex()` for each block to construct the final LaTeX expression step-by-step. There is an example implementation under each heading below to demonstrate. For more guidance on how to implement this method, see the examples below or [start a discussion on GitHub](https://github.com/hrushikeshrv/guimath/discussions).
4549

4650
## One Block Component
47-
Inherit from this class if your function has one block. Examples of functions which have one block include $ \sqrt{\boxed{}} $, $ \sin{\boxed{}} $, $ \sin^{2}{\boxed{}} $, etc.
51+
Inherit from this class if your function has one block. Examples of functions which have one block include square root ($ \sqrt{\boxed{}} $), sine ($ \sin{\boxed{}} $), sine squared ($ \sin^{2}{\boxed{}} $), etc.
4852

4953
### Inheritance Example: $ \sin^{2}{\boxed{}} $
5054
```javascript
5155
class SinSquaredComponent extends OneBlockComponent {
5256
toLatex() {
5357
return `\\sin^{2}{${this.blocks[0].toLatex()}}`;
5458
}
59+
60+
toHTML(cursorBlock, cursorPosition) {
61+
return `
62+
<div class="_guimath_component _guimath_flexbox_row">
63+
<div class='_guimath_block' style="font-style: normal;">sin</div>
64+
<div class='_guimath_block _guimath_small_block' style="top: -0.5em">2</div>
65+
<div class='_guimath_block'>${this.blocks[0].toHTML(cursorBlock, cursorPosition)}</div>
66+
</div>
67+
`;
68+
}
5569
}
5670
```
5771

5872
## Two Block Component
59-
Inherit from this class if your function has two blocks. Examples of functions which have two blocks include $ \frac{\boxed{}}{\boxed{}} $, $ \sqrt[\boxed{}]{\boxed{}} $, etc.
73+
Inherit from this class if your function has two blocks. Examples of functions which have two blocks include fraction ($ \frac{\boxed{}}{\boxed{}} $), n-th root ($ \sqrt[\boxed{}]{\boxed{}} $), etc.
6074

6175
### Inheritance Example: $ \frac{\boxed{}}{\boxed{}} $
6276
```javascript
6377
class Fraction extends TwoBlockComponent {
6478
toLatex() {
6579
return `\\frac{${this.blocks[0].toLatex()}}{${this.blocks[1].toLatex()}}`;
6680
}
81+
82+
toHTML(cursorBlock, cursorPosition) {
83+
return `
84+
<div class="_guimath_component _guimath_flexbox_column">
85+
<div class='_guimath_block' style='border-bottom: 2px solid var(--default-font-color); padding-bottom: 0.35em;'>${this.blocks[0].toHTML(cursorBlock, cursorPosition)}</div>
86+
<div class='_guimath_block' style='padding-top: 0.05em;'>${this.blocks[1].toHTML(cursorBlock, cursorPosition)}</div>
87+
</div>
88+
`;
89+
}
6790
}
6891
```
6992

7093
## Three Block Component
71-
Inherit from this class if your function has three blocks. Examples of functions which have three blocks include $ \sum_{\boxed{}}^{\boxed{}}{\boxed{}} $, $ \int_{\boxed{}}^{\boxed{}}{\boxed{}} $, etc.
94+
Inherit from this class if your function has three blocks. Examples of functions which have three blocks include sum ($ \sum_{\boxed{}}^{\boxed{}}{\boxed{}} $), integral ($ \int_{\boxed{}}^{\boxed{}}{\boxed{}} $), etc.
7295

7396
### Inheritance Example: $ \left.\frac{\boxed{}}{\boxed{}}\right|_{\boxed{}} $
7497
```javascript
7598
class OneSidedFence extends ThreeBlockComponent {
7699
toLatex() {
77100
return `\\left.\\frac{${this.blocks[0].toLatex()}}{${this.blocks[1].toLatex()}}\\right|_{${this.blocks[2].toLatex()}}`;
78101
}
102+
103+
toHTML(cursorBlock, cursorPosition) {
104+
return `
105+
<div class="_guimath_component _guimath_flexbox_column">
106+
<div class="_guimath_flexbox_row">
107+
<div style="border-right: 2px solid var(--default-font-color);">
108+
<div class='_guimath_block' style='border-bottom: 2px solid var(--default-font-color); padding-bottom: 0.35em;'>${this.blocks[0].toHTML(cursorBlock, cursorPosition)}</div>
109+
<div class='_guimath_block' style='padding-top: 0.05em;'>${this.blocks[1].toHTML(cursorBlock, cursorPosition)}</div>
110+
</div>
111+
<div class="_guimath_small_block" style="align-self: flex-end; justify-self: flex-end;">
112+
${this.blocks[2].toHTML(cursorBlock, cursorPosition)}
113+
</div>
114+
</div>
115+
</div>`;
116+
}
79117
}
80118
```
81119

82120
## Base Component Class
83-
Inherit from this class if your function has more than three blocks.
121+
Inherit from this class if your function has more than three blocks. For more guidance on how to extend this class, [start a discussion on GitHub](https://github.com/hrushikeshrv/guimath/discussions).

docs/customizing/functionality.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ class SinSquaredComponent extends OneBlockComponent {
8888
toLatex() {
8989
return `\\sin^{2}{${this.blocks[0].toLatex()}}`;
9090
}
91+
92+
toHTML(cursorBlock, cursorPosition) {
93+
return `
94+
<div class="_guimath_component _guimath_flexbox_row">
95+
<div class='_guimath_block' style="font-style: normal;">sin</div>
96+
<div class='_guimath_block _guimath_small_block' style="top: -0.5em">2</div>
97+
<div class='_guimath_block'>${this.blocks[0].toHTML(cursorBlock, cursorPosition)}</div>
98+
</div>
99+
`;
100+
}
91101
}
92102

93103
// Create a GUIMath instance
@@ -104,8 +114,8 @@ guimath.registerFunction(
104114
<mn>2</mn>
105115
</msup>
106116
</math>`,
107-
title = 'Sine squared',
108-
typeset = false
117+
title = "Sine squared",
118+
tabName = "functions"
109119
);
110120
```
111121
You can see this example [here](../examples/add-custom-function.html).

docs/examples/add-custom-function.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@
135135
toLatex() {
136136
return `\\sin^{2}{${this.blocks[0].toLatex()}}`;
137137
}
138+
139+
toHTML(cursorBlock, cursorPosition) {
140+
return `
141+
<div class="_guimath_component _guimath_flexbox_row">
142+
<div class='_guimath_block' style="font-style: normal;">sin</div>
143+
<div class='_guimath_block _guimath_small_block' style="top: -0.5em">2</div>
144+
<div class='_guimath_block'>${this.blocks[0].toHTML(
145+
cursorBlock,
146+
cursorPosition,
147+
)}</div>
148+
</div>
149+
`;
150+
}
138151
}
139152

140153
guimath.registerFunction(

0 commit comments

Comments
 (0)