Skip to content

Commit

Permalink
feat: default note color change with bg contrast
Browse files Browse the repository at this point in the history
  • Loading branch information
juanfran committed Jan 8, 2025
1 parent bd04763 commit bf11ab9
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface Toolbar {
options: {
layoutOptions: boolean;
fontSize: boolean;
defaultTextColor: string;
};
node: Signal<TuNode<NodeToolbar, string>>;
x: number;
Expand All @@ -45,6 +46,7 @@ interface Toolbar {
[node]="toolbar.node()"
[layoutOptions]="toolbar.options.layoutOptions"
[fontSize]="toolbar.options.fontSize"
[defaultTextColor]="toolbar.options.defaultTextColor"
[style.left.px]="toolbar.x"
[style.top.px]="toolbar.y" />
}
Expand Down
5 changes: 5 additions & 0 deletions apps/web/src/app/styles/override-color-picker.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.pickr {
border: 1px solid var(--grey-30);
border-radius: 3px
}

.pcr-app {
border-radius: var(--radius-3);
border: 1px solid var(--grey-30);
Expand Down
1 change: 1 addition & 0 deletions libs/nodes/src/lib/note/note.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<tapiz-editor-portal [node]="node()">
<tapiz-editor-view
[class.readonly]="!edit()"
[defaultTextColor]="defaultTextColor()"
#editorView="editorView"
[node]="node()"
[toolbar]="edit()"
Expand Down
15 changes: 14 additions & 1 deletion libs/nodes/src/lib/note/note.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@angular/core';
import { Store } from '@ngrx/store';
import { Drawing, Note, Panel, TuNode, isPanel } from '@tapiz/board-commons';
import { lighter } from '@tapiz/cdk/utils/colors';
import { contrast, lighter } from '@tapiz/cdk/utils/colors';
import { insideNode } from '@tapiz/cdk/utils/inside-node';
import {
DrawingDirective,
Expand Down Expand Up @@ -95,6 +95,19 @@ export class NoteComponent {
rotation = signal(0);
rotateAngle = signal('0deg');

defaultTextColor = computed(() => {
// if the note has text, the color is by the current text color
if (this.node().content.text.length) {
return null;
}

const color = this.node().content.color ?? defaultNoteColor;

const contrastColor = contrast(color, '#ffffff');

return contrastColor > 2 ? '#ffffff' : '#000000';
});

generateRandomRotation() {
let rotation: number;
const aspectRatio = this.height() / this.width();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class EditorViewSharedStateService {
options: {
layoutOptions: boolean;
fontSize: boolean;
defaultTextColor: string;
},
) {
this.#nodes.next(
Expand Down
239 changes: 124 additions & 115 deletions libs/ui/src/lib/editor-view/editor-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from '@angular/core';

import { Editor } from '@tiptap/core';
import { Color } from '@tiptap/extension-color';
import { TextStyle } from '@tiptap/extension-text-style';
import { TextAlign } from '@tiptap/extension-text-align';
import { Link } from '@tiptap/extension-link';
Expand Down Expand Up @@ -41,6 +40,7 @@ import { FontSize } from './font-size-plugin';
import { PopupComponent } from '../popup/popup.component';
import { normalize } from '@tapiz/utils/normalize';
import { explicitEffect } from 'ngxtension/explicit-effect';
import Color from '@tiptap/extension-color';

@Component({
selector: 'tapiz-editor-view',
Expand Down Expand Up @@ -95,6 +95,7 @@ export class EditorViewComponent implements OnDestroy, AfterViewInit {
contentChange = output<string>();
focus = input<boolean>(false);
customClass = input('');
defaultTextColor = input<string | null>(null);
popupComponent = viewChild(PopupComponent);
mentions = input<{ id: string; name: string }[]>([]);
suggestedMentions = signal<{ id: string; name: string }[]>([]);
Expand Down Expand Up @@ -178,129 +179,136 @@ export class EditorViewComponent implements OnDestroy, AfterViewInit {
}
});

this.#editor.set(
new Editor({
element: this.editor.nativeElement,
editorProps: {
attributes: {
class: this.customClass(),
},
const editor = new Editor({
element: this.editor.nativeElement,
editorProps: {
attributes: {
class: this.customClass(),
},
extensions: [
ListItem,
Text,
Document,
OrderedList,
Italic,
Bold,
Paragraph,
BulletList,
Strike,
Heading.configure({
levels: [1, 2, 3],
}),
TextStyle,
Color,
TextAlign.configure({
types: ['heading', 'paragraph'],
}),
Link.configure({
openOnClick: true,
}),
FontFamily.configure({
types: ['textStyle'],
}),
FontSize,
History.configure(),
BubbleMenu.configure({
element: this.linkMenu.nativeElement,
shouldShow: ({ editor, nodeDom }) => {
if (
(nodeDom?.tagName !== 'A' && !nodeDom?.closest('a')) ||
!this.toolbar()
) {
},
extensions: [
ListItem,
Text,
Document,
OrderedList,
Italic,
Bold,
Paragraph,
BulletList,
Strike,
Heading.configure({
levels: [1, 2, 3],
}),
TextStyle,
Color,
// CustomDefaultColorPlugin(this.defaultTextColor()),
TextAlign.configure({
types: ['heading', 'paragraph'],
}),
Link.configure({
openOnClick: true,
}),
FontFamily.configure({
types: ['textStyle'],
}),
FontSize,
History.configure(),
BubbleMenu.configure({
element: this.linkMenu.nativeElement,
shouldShow: ({ editor, nodeDom }) => {
if (
(nodeDom?.tagName !== 'A' && !nodeDom?.closest('a')) ||
!this.toolbar()
) {
return false;
}

const isLink = editor.isActive('link');

if (isLink) {
const currentUrl = editor.getAttributes('link')['href'] ?? '';
this.linkUrl.set(currentUrl);

if (!currentUrl) {
return false;
}
}

const isLink = editor.isActive('link');
return isLink;
},
}),
Mention.configure({
suggestion: {
render: () => {
return {
onStart: (props) => {
this.mentionCommand = props.command;
this.suggestionElement.set(props.decorationNode);
this.suggestedMentions.set(props.items);
},
onUpdate: (props) => {
this.mentionCommand = props.command;
this.suggestionElement.set(props.decorationNode);
this.suggestedMentions.set(props.items);
},
onKeyDown: (props) => {
if (props.event.key === 'Enter') {
this.selectMention(this.mentionIndex());
return true;
} else if (props.event.key === 'Escape') {
this.suggestionElement.set(null);

if (isLink) {
const currentUrl = editor.getAttributes('link')['href'] ?? '';
this.linkUrl.set(currentUrl);
return true;
} else if (props.event.key === 'ArrowDown') {
this.mentionIndex.update((index) => {
return Math.min(
index + 1,
this.suggestedMentions().length - 1,
);
});

if (!currentUrl) {
return false;
}
}
return true;
} else if (props.event.key === 'ArrowUp') {
this.mentionIndex.update((index) => {
return Math.max(index - 1, 0);
});

return isLink;
return true;
}

return false;
},
onExit: () => {
this.suggestionElement.set(null);
},
};
},
}),
Mention.configure({
suggestion: {
render: () => {
return {
onStart: (props) => {
this.mentionCommand = props.command;
this.suggestionElement.set(props.decorationNode);
this.suggestedMentions.set(props.items);
},
onUpdate: (props) => {
this.mentionCommand = props.command;
this.suggestionElement.set(props.decorationNode);
this.suggestedMentions.set(props.items);
},
onKeyDown: (props) => {
if (props.event.key === 'Enter') {
this.selectMention(this.mentionIndex());
return true;
} else if (props.event.key === 'Escape') {
this.suggestionElement.set(null);

return true;
} else if (props.event.key === 'ArrowDown') {
this.mentionIndex.update((index) => {
return Math.min(
index + 1,
this.suggestedMentions().length - 1,
);
});

return true;
} else if (props.event.key === 'ArrowUp') {
this.mentionIndex.update((index) => {
return Math.max(index - 1, 0);
});

return true;
}

return false;
},
onExit: () => {
this.suggestionElement.set(null);
},
};
},
items: ({ query }) => {
return this.mentions().filter((item) => {
return normalize(item.name).includes(normalize(query));
});
},
items: ({ query }) => {
return this.mentions().filter((item) => {
return normalize(item.name).includes(normalize(query));
});
},
}),
],
content: node.innerHTML,
onUpdate: ({ editor }) => {
this.#contentChange$.next(editor.getHTML());
},
onCreate: ({ editor }) => {
if (this.focus()) {
editor.view.focus();
}
},
}),
);
},
}),
],
content: node.innerHTML,
onUpdate: ({ editor }) => {
this.#contentChange$.next(editor.getHTML());
},
onCreate: ({ editor }) => {
if (this.focus()) {
editor.view.focus();
}

const defaultTextColor = this.defaultTextColor();

if (defaultTextColor) {
editor.chain().setColor(defaultTextColor).run();
}
},
});

this.#editor.set(editor);

if (this.toolbar()) {
this.#showToolbar();
Expand Down Expand Up @@ -335,6 +343,7 @@ export class EditorViewComponent implements OnDestroy, AfterViewInit {
this.#editorViewSharedStateService.addNode(this.node, instance, {
layoutOptions: this.layoutToolbarOptions(),
fontSize: this.fontSize(),
defaultTextColor: this.defaultTextColor() ?? '',
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@
--mat-form-field-container-vertical-padding: 5px;
--mat-form-field-container-height: 26px;
}

Loading

0 comments on commit bf11ab9

Please sign in to comment.