Skip to content
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

Feature/JS-4830: Inline latex #368

Merged
merged 28 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e99c57e
inline latex test
ra3orblade Nov 21, 2023
9ae4a27
inline latex experiment
ra3orblade Nov 21, 2023
95b1e28
Merge branch 'main' of github.com:anyproto/anytype-ts into feature/in…
ra3orblade Dec 29, 2023
4fd8948
merge
ra3orblade Jan 1, 2024
9fef7c0
merge
ra3orblade Jan 9, 2024
4110b52
merge
ra3orblade Jan 9, 2024
8c94bdb
merge
ra3orblade Jan 18, 2024
4d32d13
merge
ra3orblade Jun 17, 2024
6bb739e
logic update
ra3orblade Jul 3, 2024
22dc2e1
Merge branch 'main' of github.com:anyproto/anytype-ts into feature/in…
ra3orblade Jul 3, 2024
48e21b2
fix multiple latex entries
ra3orblade Jul 3, 2024
1ac7d82
small refactoring
ra3orblade Jul 3, 2024
d4169fa
small refactoring
ra3orblade Jul 3, 2024
4c277b8
fix rendering
ra3orblade Jul 3, 2024
b76dc14
cleanup tags inside latex markup
ra3orblade Jul 3, 2024
77aeb64
small refactoring
ra3orblade Jul 3, 2024
f65668d
JS-4765: design review fix
ra3orblade Jul 3, 2024
6487e69
Merge branch 'main' of github.com:anyproto/anytype-ts into feature/in…
ra3orblade Jul 3, 2024
c2672d9
fix tags in icon
ra3orblade Jul 3, 2024
c0394ac
add latex to twin symbols
ra3orblade Jul 4, 2024
c732cca
Merge branch 'main' of github.com:anyproto/anytype-ts into feature/in…
ra3orblade Jul 4, 2024
ea42f71
merge
ra3orblade Jul 5, 2024
11aec1e
Merge branch 'main' of github.com:anyproto/anytype-ts into feature/in…
ra3orblade Jul 16, 2024
86a364a
Merge branch 'main' of github.com:anyproto/anytype-ts into feature/in…
ra3orblade Jul 19, 2024
532cce9
Merge branch 'main' of github.com:anyproto/anytype-ts into feature/in…
ra3orblade Jul 22, 2024
96634be
merge
ra3orblade Jul 28, 2024
f305da1
Merge branch 'main' of github.com:anyproto/anytype-ts into feature/in…
ra3orblade Jul 30, 2024
b2bf684
JS-4975: fix
ra3orblade Jul 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/scss/block/text.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
}
markuplink.disabled, markupobject.disabled { border-width: 0px; }

markuplatex { user-select: all !important; }

markupmention { display: inline; user-select: all !important; position: relative; cursor: default; white-space: nowrap; }
markupmention * { user-select: text !important; }
markupmention {
Expand Down
4 changes: 1 addition & 3 deletions src/scss/widget/view/calendar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@
}
}

.day { @include text-common; }
.day, .day .inner { display: flex; flex-direction: row; align-items: center; justify-content: center; }
.day.today { color: var(--color-system-accent-125); font-weight: 500; }
.day.other { color: var(--color-text-secondary); }

.day, .day .inner { display: flex; flex-direction: row; align-items: center; justify-content: center; }

.day {
.inner { width: 100%; max-width: 28px; height: 28px; border-radius: 4px; transition: $transitionAllCommon; position: relative; }
.bullet { width: 3px; height: 3px; border-radius: 50%; position: absolute; bottom: 2px; left: 50%; margin-left: -1.5px; background: var(--color-control-active); }
Expand Down
70 changes: 58 additions & 12 deletions src/ts/component/block/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ for (const lang of langs) {
require(`prismjs/components/prism-${lang}.js`);
};

const katex = require('katex');
require('katex/dist/contrib/mhchem');

const BlockText = observer(class BlockText extends React.Component<Props> {

public static defaultProps = {
Expand Down Expand Up @@ -220,6 +223,7 @@ const BlockText = observer(class BlockText extends React.Component<Props> {

this.marks = U.Common.objectCopy(marks || []);
this.setValue(text);
this.renderLatex();
};

componentDidUpdate () {
Expand All @@ -237,6 +241,8 @@ const BlockText = observer(class BlockText extends React.Component<Props> {

if (focused == block.id) {
focus.apply();
} else {
this.renderLatex();
};

if (onUpdate) {
Expand Down Expand Up @@ -589,6 +595,42 @@ const BlockText = observer(class BlockText extends React.Component<Props> {
});
};

renderLatex () {
if (!this._isMounted) {
return;
};

const value = this.refEditable.getHtmlValue();
const reg = /\$((?:[^$\\]|\\.)*?)\$/g;

if (!reg.test(value)) {
return;
};

const tag = Mark.getTag(I.MarkType.Latex);
const html = value.replace(reg, (s: string, p: string, o: number) => {
let ret = '';
try {
ret = katex.renderToString(U.Common.stripTags(p), {
displayMode: false,
throwOnError: false,
output: 'html',
trust: ctx => [ '\\url', '\\href', '\\includegraphics' ].includes(ctx.command),
});

ret = ret ? `<${tag}>${ret}</${tag}>` : s;
} catch (e) {
ret = s;
};

return ret;
});

if (this.refEditable && (html !== value)) {
this.refEditable.setValue(html);
};
};

emojiParam (style: I.TextStyle) {
let size = 24;
switch (style) {
Expand Down Expand Up @@ -699,17 +741,6 @@ const BlockText = observer(class BlockText extends React.Component<Props> {
{ key: `ctrl+shift+l` },
{ key: `ctrl+shift+/` },
];

if (isInsideTable) {
if (!range.to) {
saveKeys.push({ key: `arrowleft, arrowup` });
};

if (range.to == value.length) {
saveKeys.push({ key: `arrowright, arrowdown` });
};
};

const twinePairs = {
'[': ']',
'{': '}',
Expand All @@ -722,8 +753,19 @@ const BlockText = observer(class BlockText extends React.Component<Props> {
'(': 'οΌ‰',
'β€œ': '”',
'β€˜': '’',
'$': '$',
};

if (isInsideTable) {
if (!range.to) {
saveKeys.push({ key: `arrowleft, arrowup` });
};

if (range.to == value.length) {
saveKeys.push({ key: `arrowright, arrowdown` });
};
};

for (let i = 0; i < 9; ++i) {
saveKeys.push({ key: `${cmd}+${i}` });
};
Expand Down Expand Up @@ -1241,11 +1283,13 @@ const BlockText = observer(class BlockText extends React.Component<Props> {
};

onFocus (e: any) {
const { onFocus } = this.props;
const { onFocus, block } = this.props;

e.persist();

this.placeholderCheck();
this.setValue(block.getText());

keyboard.setFocus(true);

if (onFocus) {
Expand Down Expand Up @@ -1283,6 +1327,8 @@ const BlockText = observer(class BlockText extends React.Component<Props> {
if (key) {
analytics.event(key);
};

this.renderLatex();
};

onPaste (e: any) {
Expand Down
1 change: 1 addition & 0 deletions src/ts/component/util/iconObject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ const IconObject = observer(class IconObject extends React.Component<Props> {
let name = String(object.name || translate('defaultNamePage'));
name = U.Smile.strip(name);
name = name.trim().substring(0, 1).toUpperCase();
name = U.Common.htmlSpecialChars(name);

return name;
};
Expand Down
1 change: 1 addition & 0 deletions src/ts/interface/block/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export enum MarkType {
Mention = 8,
Emoji = 9,
Object = 10,
Latex = 11,

Change = 100,
Highlight = 101,
Expand Down
31 changes: 22 additions & 9 deletions src/ts/lib/mark.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import $ from 'jquery';
import { I, U, J, analytics } from 'Lib';

const Tags = {};
for (const i in I.MarkType) {
if (!isNaN(Number(i))) {
Tags[i] = `markup${I.MarkType[i].toLowerCase()}`;
};
};

const Patterns = {
'-β†’': '⟢',
'β€”>': '⟢',
Expand All @@ -23,8 +30,7 @@ const Patterns = {
'...': '…',
};

const Order: any = {};
[
const Order = [
I.MarkType.Change,
I.MarkType.Object,
I.MarkType.Emoji,
Expand All @@ -37,7 +43,7 @@ const Order: any = {};
I.MarkType.Color,
I.MarkType.BgColor,
I.MarkType.Code,
].forEach((type, i) => Order[type] = i);
];

class Mark {

Expand Down Expand Up @@ -139,8 +145,8 @@ class Mark {
};

sort (c1: I.Mark, c2: I.Mark) {
const o1 = Order[c1.type];
const o2 = Order[c2.type];
const o1 = Order.indexOf(c1.type);
const o2 = Order.indexOf(c2.type);
if (o1 > o2) return 1;
if (o1 < o2) return -1;
if (c1.range.from > c2.range.from) return 1;
Expand Down Expand Up @@ -543,6 +549,8 @@ class Mark {
return s;
});

marks = this.checkRanges(text, marks);

// Links
html.replace(/\[([^\[\]]+)\]\(([^\(\)]+)\)(\s|$)/g, (s: string, p1: string, p2: string, p3: string) => {
p1 = String(p1 || '');
Expand Down Expand Up @@ -629,22 +637,27 @@ class Mark {
};

switch (type) {
case I.MarkType.Link:
case I.MarkType.Link: {
attr = `href="${param}"`;
break;
};

case I.MarkType.Mention:
case I.MarkType.Emoji:
case I.MarkType.Emoji: {
attr = 'contenteditable="false"';
break;
};

case I.MarkType.Color:
case I.MarkType.Color: {
attr = `class="textColor textColor-${param}"`;
break;
};

case I.MarkType.BgColor:
case I.MarkType.BgColor: {
attr = `class="bgColor bgColor-${param}"`;
break;
};

};
return attr;
};
Expand Down
Loading