-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.lua
More file actions
211 lines (190 loc) · 6.32 KB
/
Copy pathCore.lua
File metadata and controls
211 lines (190 loc) · 6.32 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
local ADDON_NAME, ns = ...
local Notesmith = CreateFrame("Frame", "NotesmithCoreFrame")
ns.Notesmith = Notesmith
_G.Notesmith = Notesmith
local DEFAULTS = {
notes = {},
nextId = 1,
settings = {
soundOnReminder = true,
autoOpenOnReminder = true,
autoOpenOnLogin = true,
windowPoint = { "CENTER", "UIParent", "CENTER", 0, 0 },
},
schema = 1,
}
local PREFIX = "|cFF7FFFD4Notesmith:|r "
local function deepCopy(tbl)
if CopyTable then return CopyTable(tbl) end
local r = {}
for k, v in pairs(tbl) do
r[k] = type(v) == "table" and deepCopy(v) or v
end
return r
end
local function applyDefaults(target, defaults)
for key, value in pairs(defaults) do
if target[key] == nil then
if type(value) == "table" then
target[key] = deepCopy(value)
else
target[key] = value
end
elseif type(value) == "table" and type(target[key]) == "table" then
applyDefaults(target[key], value)
end
end
end
function Notesmith:Print(msg)
DEFAULT_CHAT_FRAME:AddMessage(PREFIX .. tostring(msg))
end
function Notesmith:GetDB()
return NotesmithDB
end
function Notesmith:CreateNote(title, body, reminder)
local db = NotesmithDB
local id = db.nextId or 1
db.nextId = id + 1
local now = time()
local note = {
id = id,
title = (title and title ~= "") and title or "Untitled",
body = body or "",
created = now,
modified = now,
reminder = reminder and true or false,
origin = (UnitName("player") or "?") .. "-" .. (GetRealmName() or "?"),
}
db.notes[id] = note
return note
end
function Notesmith:UpdateNote(id, fields)
local note = NotesmithDB.notes[id]
if not note then return end
for k, v in pairs(fields) do
note[k] = v
end
note.modified = time()
return note
end
function Notesmith:DeleteNote(id)
NotesmithDB.notes[id] = nil
end
function Notesmith:GetNote(id)
return NotesmithDB.notes[id]
end
function Notesmith:GetSortedNotes()
local list = {}
for _, note in pairs(NotesmithDB.notes) do
list[#list + 1] = note
end
table.sort(list, function(a, b)
if a.reminder ~= b.reminder then
return a.reminder and not b.reminder
end
return (a.modified or 0) > (b.modified or 0)
end)
return list
end
function Notesmith:GetPendingReminders()
local list = {}
for _, note in pairs(NotesmithDB.notes) do
if note.reminder then
list[#list + 1] = note
end
end
table.sort(list, function(a, b) return (a.created or 0) < (b.created or 0) end)
return list
end
function Notesmith:ClearReminder(id)
local note = NotesmithDB.notes[id]
if note then
note.reminder = false
note.modified = time()
end
end
function Notesmith:CountNotes()
local n = 0
for _ in pairs(NotesmithDB.notes) do n = n + 1 end
return n
end
Notesmith:RegisterEvent("ADDON_LOADED")
Notesmith:RegisterEvent("PLAYER_LOGIN")
Notesmith:SetScript("OnEvent", function(self, event, arg1)
if event == "ADDON_LOADED" and arg1 == ADDON_NAME then
NotesmithDB = NotesmithDB or {}
applyDefaults(NotesmithDB, DEFAULTS)
if ns.InitializeUI then
ns.InitializeUI()
end
self:UnregisterEvent("ADDON_LOADED")
elseif event == "PLAYER_LOGIN" then
C_Timer.After(3, function()
if NotesmithDB.settings.autoOpenOnLogin and ns.ShowMainFrame then
ns.ShowMainFrame()
end
local pending = Notesmith:GetPendingReminders()
if #pending > 0 and ns.ShowReminders then
ns.ShowReminders(pending)
end
end)
end
end)
local function trim(s)
return (s and s:gsub("^%s+", ""):gsub("%s+$", "")) or ""
end
local function printHelp()
Notesmith:Print("commands:")
DEFAULT_CHAT_FRAME:AddMessage(" |cFFFFD700/notesmith|r - toggle the window")
DEFAULT_CHAT_FRAME:AddMessage(" |cFFFFD700/notesmith new|r [title] - create and open a note")
DEFAULT_CHAT_FRAME:AddMessage(" |cFFFFD700/notesmith remind|r <text> - set a reminder for next login")
DEFAULT_CHAT_FRAME:AddMessage(" |cFFFFD700/notesmith list|r - list all notes in chat")
DEFAULT_CHAT_FRAME:AddMessage(" |cFFFFD700/notesmith clear|r - clear all pending reminders")
DEFAULT_CHAT_FRAME:AddMessage(" |cFFFFD700/notesmith auto|r - toggle auto-open on login")
DEFAULT_CHAT_FRAME:AddMessage(" |cFFFFD700/notesmith help|r - this message")
end
SLASH_NOTESMITH1 = "/notesmith"
SlashCmdList["NOTESMITH"] = function(msg)
msg = trim(msg)
if msg == "" then
if ns.ToggleMainFrame then ns.ToggleMainFrame() end
return
end
local cmd, rest = msg:match("^(%S+)%s*(.*)$")
cmd = cmd and cmd:lower() or ""
rest = trim(rest or "")
if cmd == "new" or cmd == "add" then
local note = Notesmith:CreateNote(rest ~= "" and rest or "New note", "", false)
if ns.OpenEditor then ns.OpenEditor(note.id) end
elseif cmd == "remind" or cmd == "reminder" then
if rest == "" then
Notesmith:Print("usage: /notesmith remind <text>")
else
local note = Notesmith:CreateNote(rest, "", true)
Notesmith:Print("reminder set for next login: " .. note.title)
end
elseif cmd == "list" then
local list = Notesmith:GetSortedNotes()
Notesmith:Print(#list .. " note(s)")
for _, n in ipairs(list) do
local mark = n.reminder and " |cFFFFD700[reminder]|r" or ""
DEFAULT_CHAT_FRAME:AddMessage((" [%d] %s%s"):format(n.id, n.title, mark))
end
elseif cmd == "clear" then
local cleared = 0
for _, n in pairs(NotesmithDB.notes) do
if n.reminder then
n.reminder = false
cleared = cleared + 1
end
end
Notesmith:Print("cleared " .. cleared .. " pending reminder(s)")
elseif cmd == "auto" then
NotesmithDB.settings.autoOpenOnLogin = not NotesmithDB.settings.autoOpenOnLogin
Notesmith:Print("auto-open on login: " .. (NotesmithDB.settings.autoOpenOnLogin and "ON" or "OFF"))
elseif cmd == "help" or cmd == "?" then
printHelp()
else
if ns.ToggleMainFrame then ns.ToggleMainFrame() end
end
end