1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
import { makeObservable, action, observable, autorun } from 'mobx';
import * as React from 'react';
import { Docs } from '../../../documents/Documents';
import { DocumentType } from '../../../documents/DocumentTypes';
import { ViewBoxAnnotatableComponent } from '../../DocComponent';
import { FieldView, FieldViewProps } from '../FieldView';
import { FormattedTextBox, FormattedTextBoxProps } from './FormattedTextBox';
import { gptAPICall, GPTCallType, gptImageLabel } from '../../../apis/gpt/GPT';
import { RichTextField } from '../../../../fields/RichTextField';
import { TextSelection } from 'prosemirror-state';
export class DailyJournal extends ViewBoxAnnotatableComponent<FieldViewProps>() {
@observable journalDate: string;
public static LayoutString(fieldStr: string) {
return FieldView.LayoutString(DailyJournal, fieldStr);
}
constructor(props: FormattedTextBoxProps) {
super(props);
makeObservable(this);
this.journalDate = this.getFormattedDate();
}
getFormattedDate(): string {
const date = new Date().toLocaleDateString(undefined, {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
});
console.log('getFormattedDate():', date);
return date;
}
@action
setDailyTitle() {
console.log('setDailyTitle() called...');
console.log('Current title before update:', this.dataDoc.title);
if (!this.dataDoc.title || this.dataDoc.title !== this.journalDate) {
console.log('Updating title to:', this.journalDate);
this.dataDoc.title = this.journalDate;
}
console.log('New title after update:', this.dataDoc.title);
}
@action
setDailyText() {
const placeholderText = 'Start writing here...';
const dateText = `${this.journalDate}\n`;
console.log('Checking if dataDoc has text field...');
this.dataDoc[this.fieldKey] = RichTextField.textToRtfFormat(
[
{ text: 'Journal Entry:', styles: { bold: true, color: 'black', fontSize: 20 } },
{ text: dateText, styles: { italic: true, color: 'gray', fontSize: 15 } },
{ text: placeholderText, styles: { fontSize: 14, color: 'gray' } },
],
undefined,
placeholderText.length
);
console.log('Current text field:', this.dataDoc[this.fieldKey]);
}
componentDidMount(): void {
console.log('componentDidMount() triggered...');
console.log("Text: " + (this.Document.text as any)?.Text);
const rawText = (this.Document.text as any)?.Text ?? '';
const isTextEmpty = !rawText || rawText === "";
const currentTitle = this.dataDoc.title || "";
const isTitleString = typeof currentTitle === 'string';
const isDefaultTitle = isTitleString && currentTitle.includes("Untitled DailyJournal");
if (isTextEmpty && isDefaultTitle) {
console.log('Journal title and text are default. Initializing...');
this.setDailyTitle();
this.setDailyText();
} else {
console.log('Journal already has content. Skipping initialization.');
}
}
@action handleGeneratePrompts = async () => {
const rawText = (this.Document.text as any)?.Text ?? '';
console.log('Extracted Journal Text:', rawText);
console.log('Before Update:', this.Document.text, 'Type:', typeof this.Document.text);
if (!rawText.trim()) {
alert('Journal is empty! Write something first.');
return;
}
try {
// Call GPT API to generate prompts
const res = await gptAPICall('Generate 1-2 short journal prompts for the following journal entry: ' + rawText, GPTCallType.COMPLETION);
if (!res) {
console.error('GPT call failed.');
return;
}
const editorView = this._ref.current?.EditorView;
if (!editorView) {
console.error('EditorView is not available.');
return;
}
else {
const { state, dispatch } = editorView;
const { schema } = state;
// Use available marks
const boldMark = schema.marks.strong.create();
const italicMark = schema.marks.em.create();
const fontSizeMark = schema.marks.pFontSize.create({ fontSize: "14px" });
const fontColorMark = schema.marks.pFontColor.create({ fontColor: "gray" });
// Create text nodes with formatting
const headerText = schema.text('\n\n# Suggested Prompts:\n', [boldMark, italicMark, fontSizeMark, fontColorMark]);
const responseText = schema.text(res, [fontSizeMark, fontColorMark]);
// Insert formatted text
const transaction = state.tr.insert(
state.selection.from, headerText).insert(
state.selection.from + headerText.nodeSize, responseText);
dispatch(transaction);
}
} catch (err) {
console.error('Error calling GPT:', err);
}
};
_ref = React.createRef<FormattedTextBox>();
render() {
return (
<div
style={{
// background: 'beige',
width: '100%',
height: '100%',
backgroundColor: 'beige',
backgroundImage: `
repeating-linear-gradient(
to bottom,
rgba(255, 26, 26, 0.2) 0px, rgba(255, 26, 26, 0.2) 1px, /* Thin red stripes */
transparent 1px, transparent 20px
)
`,
backgroundSize: '100% 20px',
backgroundRepeat: 'repeat',
}}>
{/* GPT Button */}
<button
style={{
position: 'absolute',
bottom: '5px',
right: '5px',
padding: '5px 10px',
backgroundColor: '#9EAD7C',
color: 'white',
border: 'none',
borderRadius: '5px',
cursor: 'pointer',
zIndex: 10,
}}
onClick={this.handleGeneratePrompts}>
Prompts
</button>
<FormattedTextBox ref={this._ref} {...this._props} fieldKey={'text'} Document={this.Document} TemplateDataDocument={undefined} />
</div>
);
}
}
Docs.Prototypes.TemplateMap.set(DocumentType.JOURNAL, {
layout: { view: DailyJournal, dataField: 'text' },
options: {
acl: '',
_height: 35,
_xMargin: 10,
_yMargin: 10,
_layout_autoHeight: true,
_layout_nativeDimEditable: true,
_layout_reflowVertical: true,
_layout_reflowHorizontal: true,
defaultDoubleClick: 'ignore',
systemIcon: 'BsFileEarmarkTextFill',
},
});
|