@shaxpir/duiduidui-models 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. package/README.md +1 -0
  2. package/decs.d.ts +87 -0
  3. package/dist/index.d.ts +3 -0
  4. package/dist/index.js +20 -0
  5. package/dist/models/OutboundMessage.d.ts +18 -0
  6. package/dist/models/OutboundMessage.js +25 -0
  7. package/dist/models/content/Activity.d.ts +10 -0
  8. package/dist/models/content/Activity.js +2 -0
  9. package/dist/models/content/ArrayView.d.ts +26 -0
  10. package/dist/models/content/ArrayView.js +174 -0
  11. package/dist/models/content/Billing.d.ts +144 -0
  12. package/dist/models/content/Billing.js +418 -0
  13. package/dist/models/content/Book.d.ts +77 -0
  14. package/dist/models/content/Book.js +407 -0
  15. package/dist/models/content/Category.d.ts +16 -0
  16. package/dist/models/content/Category.js +20 -0
  17. package/dist/models/content/Checkpointable.d.ts +21 -0
  18. package/dist/models/content/Checkpointable.js +156 -0
  19. package/dist/models/content/Comment.d.ts +19 -0
  20. package/dist/models/content/Comment.js +53 -0
  21. package/dist/models/content/ConceptArt.d.ts +31 -0
  22. package/dist/models/content/ConceptArt.js +84 -0
  23. package/dist/models/content/Content.d.ts +52 -0
  24. package/dist/models/content/Content.js +61 -0
  25. package/dist/models/content/ContentKind.d.ts +10 -0
  26. package/dist/models/content/ContentKind.js +16 -0
  27. package/dist/models/content/Context.d.ts +28 -0
  28. package/dist/models/content/Context.js +162 -0
  29. package/dist/models/content/DevEnv.d.ts +5 -0
  30. package/dist/models/content/DevEnv.js +9 -0
  31. package/dist/models/content/Device.d.ts +24 -0
  32. package/dist/models/content/Device.js +62 -0
  33. package/dist/models/content/Dictionary.d.ts +31 -0
  34. package/dist/models/content/Dictionary.js +5 -0
  35. package/dist/models/content/DictionaryEntry.d.ts +20 -0
  36. package/dist/models/content/DictionaryEntry.js +2 -0
  37. package/dist/models/content/ElasticModel.d.ts +149 -0
  38. package/dist/models/content/ElasticModel.js +179 -0
  39. package/dist/models/content/Environment.d.ts +61 -0
  40. package/dist/models/content/Environment.js +124 -0
  41. package/dist/models/content/ExportOptions.d.ts +64 -0
  42. package/dist/models/content/ExportOptions.js +213 -0
  43. package/dist/models/content/Folder.d.ts +16 -0
  44. package/dist/models/content/Folder.js +33 -0
  45. package/dist/models/content/Fragment.d.ts +54 -0
  46. package/dist/models/content/Fragment.js +181 -0
  47. package/dist/models/content/GeoLocation.d.ts +4 -0
  48. package/dist/models/content/GeoLocation.js +2 -0
  49. package/dist/models/content/Hanzi.d.ts +21 -0
  50. package/dist/models/content/Hanzi.js +2 -0
  51. package/dist/models/content/HighlightRule.d.ts +9 -0
  52. package/dist/models/content/HighlightRule.js +2 -0
  53. package/dist/models/content/Manifest.d.ts +42 -0
  54. package/dist/models/content/Manifest.js +114 -0
  55. package/dist/models/content/Media.d.ts +32 -0
  56. package/dist/models/content/Media.js +98 -0
  57. package/dist/models/content/Metric.d.ts +46 -0
  58. package/dist/models/content/Metric.js +183 -0
  59. package/dist/models/content/Migration.d.ts +68 -0
  60. package/dist/models/content/Migration.js +155 -0
  61. package/dist/models/content/Model.d.ts +45 -0
  62. package/dist/models/content/Model.js +280 -0
  63. package/dist/models/content/Permissions.d.ts +7 -0
  64. package/dist/models/content/Permissions.js +20 -0
  65. package/dist/models/content/Phrase.d.ts +8 -0
  66. package/dist/models/content/Phrase.js +2 -0
  67. package/dist/models/content/Placeholder.d.ts +8 -0
  68. package/dist/models/content/Placeholder.js +36 -0
  69. package/dist/models/content/Profile.d.ts +30 -0
  70. package/dist/models/content/Profile.js +95 -0
  71. package/dist/models/content/RichText.d.ts +58 -0
  72. package/dist/models/content/RichText.js +79 -0
  73. package/dist/models/content/Session.d.ts +39 -0
  74. package/dist/models/content/Session.js +173 -0
  75. package/dist/models/content/Speech.d.ts +67 -0
  76. package/dist/models/content/Speech.js +97 -0
  77. package/dist/models/content/Stub.d.ts +24 -0
  78. package/dist/models/content/Stub.js +179 -0
  79. package/dist/models/content/Time.d.ts +56 -0
  80. package/dist/models/content/Time.js +295 -0
  81. package/dist/models/content/User.d.ts +36 -0
  82. package/dist/models/content/User.js +95 -0
  83. package/dist/models/content/Workspace.d.ts +71 -0
  84. package/dist/models/content/Workspace.js +237 -0
  85. package/dist/models/content/index.d.ts +36 -0
  86. package/dist/models/content/index.js +53 -0
  87. package/dist/models/index.d.ts +4 -0
  88. package/dist/models/index.js +21 -0
  89. package/dist/models/legacy/LegacyBodyFormat.d.ts +9 -0
  90. package/dist/models/legacy/LegacyBodyFormat.js +2 -0
  91. package/dist/models/legacy/LegacyComment.d.ts +12 -0
  92. package/dist/models/legacy/LegacyComment.js +2 -0
  93. package/dist/models/legacy/LegacyContent.d.ts +53 -0
  94. package/dist/models/legacy/LegacyContent.js +55 -0
  95. package/dist/models/legacy/LegacyConversion.d.ts +55 -0
  96. package/dist/models/legacy/LegacyConversion.js +401 -0
  97. package/dist/models/legacy/LegacyFragment.d.ts +21 -0
  98. package/dist/models/legacy/LegacyFragment.js +2 -0
  99. package/dist/models/legacy/LegacyLocator.d.ts +8 -0
  100. package/dist/models/legacy/LegacyLocator.js +31 -0
  101. package/dist/models/legacy/LegacyOutboundMessage.d.ts +16 -0
  102. package/dist/models/legacy/LegacyOutboundMessage.js +13 -0
  103. package/dist/models/legacy/LegacyPicture.d.ts +14 -0
  104. package/dist/models/legacy/LegacyPicture.js +2 -0
  105. package/dist/models/legacy/LegacyProfile.d.ts +9 -0
  106. package/dist/models/legacy/LegacyProfile.js +2 -0
  107. package/dist/models/legacy/LegacySession.d.ts +41 -0
  108. package/dist/models/legacy/LegacySession.js +35 -0
  109. package/dist/models/legacy/LegacyStory.d.ts +23 -0
  110. package/dist/models/legacy/LegacyStory.js +2 -0
  111. package/dist/models/legacy/LegacyStub.d.ts +15 -0
  112. package/dist/models/legacy/LegacyStub.js +2 -0
  113. package/dist/models/legacy/LegacyTransaction.d.ts +14 -0
  114. package/dist/models/legacy/LegacyTransaction.js +49 -0
  115. package/dist/models/legacy/LegacyUser.d.ts +28 -0
  116. package/dist/models/legacy/LegacyUser.js +32 -0
  117. package/dist/models/legacy/LegacyWorkspace.d.ts +23 -0
  118. package/dist/models/legacy/LegacyWorkspace.js +6 -0
  119. package/dist/models/legacy/index.d.ts +15 -0
  120. package/dist/models/legacy/index.js +32 -0
  121. package/dist/models/markup/BodyFormat.d.ts +14 -0
  122. package/dist/models/markup/BodyFormat.js +190 -0
  123. package/dist/models/markup/ChangeModel.d.ts +22 -0
  124. package/dist/models/markup/ChangeModel.js +107 -0
  125. package/dist/models/markup/DeltaOps.d.ts +5 -0
  126. package/dist/models/markup/DeltaOps.js +74 -0
  127. package/dist/models/markup/HtmlMarkup.d.ts +4 -0
  128. package/dist/models/markup/HtmlMarkup.js +21 -0
  129. package/dist/models/markup/Operation.d.ts +32 -0
  130. package/dist/models/markup/Operation.js +194 -0
  131. package/dist/models/markup/TextEditOps.d.ts +9 -0
  132. package/dist/models/markup/TextEditOps.js +50 -0
  133. package/dist/models/markup/index.d.ts +6 -0
  134. package/dist/models/markup/index.js +23 -0
  135. package/dist/repo/ConnectionListener.d.ts +9 -0
  136. package/dist/repo/ConnectionListener.js +21 -0
  137. package/dist/repo/PermissiveJson1.d.ts +58 -0
  138. package/dist/repo/PermissiveJson1.js +39 -0
  139. package/dist/repo/ShareSync.d.ts +60 -0
  140. package/dist/repo/ShareSync.js +348 -0
  141. package/dist/repo/index.d.ts +3 -0
  142. package/dist/repo/index.js +20 -0
  143. package/dist/util/Async.d.ts +8 -0
  144. package/dist/util/Async.js +18 -0
  145. package/dist/util/Base62.d.ts +6 -0
  146. package/dist/util/Base62.js +47 -0
  147. package/dist/util/BinarySearch.d.ts +7 -0
  148. package/dist/util/BinarySearch.js +46 -0
  149. package/dist/util/CachingHasher.d.ts +8 -0
  150. package/dist/util/CachingHasher.js +41 -0
  151. package/dist/util/Color.d.ts +32 -0
  152. package/dist/util/Color.js +204 -0
  153. package/dist/util/Dispatch.d.ts +15 -0
  154. package/dist/util/Dispatch.js +79 -0
  155. package/dist/util/EditDistance.d.ts +13 -0
  156. package/dist/util/EditDistance.js +184 -0
  157. package/dist/util/Encryption.d.ts +5 -0
  158. package/dist/util/Encryption.js +2 -0
  159. package/dist/util/Logging.d.ts +108 -0
  160. package/dist/util/Logging.js +412 -0
  161. package/dist/util/NumberFormat.d.ts +14 -0
  162. package/dist/util/NumberFormat.js +224 -0
  163. package/dist/util/Struct.d.ts +4 -0
  164. package/dist/util/Struct.js +15 -0
  165. package/dist/util/Template.d.ts +16 -0
  166. package/dist/util/Template.js +128 -0
  167. package/dist/util/Text.d.ts +45 -0
  168. package/dist/util/Text.js +243 -0
  169. package/dist/util/Tuples.d.ts +9 -0
  170. package/dist/util/Tuples.js +135 -0
  171. package/dist/util/Validate.d.ts +4 -0
  172. package/dist/util/Validate.js +11 -0
  173. package/dist/util/Vocabulary.d.ts +3 -0
  174. package/dist/util/Vocabulary.js +35 -0
  175. package/dist/util/index.d.ts +16 -0
  176. package/dist/util/index.js +33 -0
  177. package/lib/models/content/ArrayView.ts +203 -0
  178. package/lib/models/content/Billing.ts +558 -0
  179. package/lib/models/content/Content.ts +110 -0
  180. package/lib/models/content/ContentKind.ts +14 -0
  181. package/lib/models/content/DevEnv.ts +5 -0
  182. package/lib/models/content/Device.ts +86 -0
  183. package/lib/models/content/DictionaryEntry.ts +22 -0
  184. package/lib/models/content/GeoLocation.ts +4 -0
  185. package/lib/models/content/Hanzi.ts +25 -0
  186. package/lib/models/content/Manifest.ts +162 -0
  187. package/lib/models/content/Media.ts +126 -0
  188. package/lib/models/content/Model.ts +327 -0
  189. package/lib/models/content/Permissions.ts +21 -0
  190. package/lib/models/content/Phrase.ts +10 -0
  191. package/lib/models/content/Profile.ts +119 -0
  192. package/lib/models/content/Time.ts +328 -0
  193. package/lib/models/content/User.ts +130 -0
  194. package/lib/models/markup/ChangeModel.ts +95 -0
  195. package/lib/models/markup/DeltaOps.ts +71 -0
  196. package/lib/models/markup/Operation.ts +215 -0
  197. package/lib/models/markup/TextEditOps.ts +50 -0
  198. package/lib/repo/ConnectionListener.ts +25 -0
  199. package/lib/repo/PermissiveJson1.ts +14 -0
  200. package/lib/repo/ShareSync.ts +390 -0
  201. package/lib/util/Base62.ts +47 -0
  202. package/lib/util/CachingHasher.ts +38 -0
  203. package/lib/util/Dispatch.ts +92 -0
  204. package/lib/util/Encryption.ts +5 -0
  205. package/lib/util/Logging.ts +568 -0
  206. package/lib/util/NumberFormat.ts +194 -0
  207. package/lib/util/Struct.ts +14 -0
  208. package/lib/util/Tuples.ts +131 -0
  209. package/package.json +41 -0
  210. package/tsconfig.json +25 -0
  211. package/tslint.json +46 -0
@@ -0,0 +1,86 @@
1
+ import { Doc } from '@shaxpir/sharedb/lib/client';
2
+ import { ShareSync, ShareSyncFactory } from '../../repo/ShareSync';
3
+ import { CachingHasher } from '../../util/CachingHasher';
4
+ import { BatchOperation } from '../markup/Operation';
5
+ import { Content, ContentBody, ContentId, ContentMeta } from "./Content";
6
+ import { ContentKind } from './ContentKind';
7
+ import { CompactDateTime, MultiClock } from './Time';
8
+
9
+ export interface LastSync {
10
+ at_utc_time:CompactDateTime;
11
+ }
12
+
13
+ export interface DevicePayload {
14
+ last_sync?:LastSync;
15
+ }
16
+
17
+ export interface DeviceBody extends ContentBody {
18
+ meta:ContentMeta;
19
+ payload:DevicePayload;
20
+ }
21
+
22
+ export class Device extends Content {
23
+
24
+ public static makeProfleId(userId:ContentId):ContentId {
25
+ return CachingHasher.makeMd5ContentId(userId + "-" + ContentKind.PROFILE);
26
+ }
27
+
28
+ public static create(
29
+ userId:ContentId,
30
+ deviceId:ContentId,
31
+ theme:string,
32
+ typeface:string,
33
+ typesize:string
34
+ ):Device {
35
+ const now = MultiClock.now();
36
+ return ShareSyncFactory.get().createContent(
37
+ {
38
+ meta : {
39
+ ref : deviceId,
40
+ is_head : true,
41
+ kind : ContentKind.DEVICE,
42
+ id : deviceId,
43
+ owner : userId,
44
+ created_at : now,
45
+ updated_at : now
46
+ },
47
+ payload : {
48
+ last_sync : { at_utc_time : null as CompactDateTime }
49
+ }
50
+ }
51
+ ) as Device;
52
+ }
53
+
54
+ constructor(doc:Doc, shouldAcquire:boolean, shareSync:ShareSync) {
55
+ super(doc, shouldAcquire, shareSync);
56
+ }
57
+
58
+ public get payload():DevicePayload {
59
+ this.checkDisposed("Device.payload");
60
+ return this.doc.data.payload as DevicePayload;
61
+ }
62
+
63
+ public get lastSyncAtUtcTime():CompactDateTime {
64
+ this.checkDisposed("Device.lastSyncAtUtcTime");
65
+ this.ensureLastSyncAtUtcTimeExists();
66
+ return this.payload.last_sync.at_utc_time;
67
+ }
68
+ public setLastSyncAtUtcTime(value:CompactDateTime):void {
69
+ this.checkDisposed("Device.setLastSyncAtUtcTime");
70
+ this.ensureLastSyncAtUtcTimeExists();
71
+ if (this.lastSyncAtUtcTime !== value) {
72
+ const batch = new BatchOperation(this);
73
+ batch.setPathValue([ 'payload', 'last_sync', 'at_utc_time' ] , value);
74
+ batch.commit();
75
+ }
76
+ }
77
+ private ensureLastSyncAtUtcTimeExists():void {
78
+ this.checkDisposed("Device.ensureLastSyncAtUtcTimeExists");
79
+ // If the 'last_sync' field doesn't exist, add it first...
80
+ if (!this.payload.hasOwnProperty('last_sync')) {
81
+ const batch = new BatchOperation(this);
82
+ batch.setPathValue([ 'payload', 'last_sync' ] , { at_utc_time : null as CompactDateTime });
83
+ batch.commit();
84
+ }
85
+ }
86
+ }
@@ -0,0 +1,22 @@
1
+ export interface DictionaryEntry {
2
+ text:string;
3
+ rank:number;
4
+ learn_rank:number;
5
+ is_starred?:boolean;
6
+ tags:string[];
7
+ }
8
+
9
+ export interface Pronouncable {
10
+ pinyin:string; // hanzi, phrase
11
+ pinyin_tokenized?:string; // phrase
12
+ pinyin_normalized:string; // hanzi, phrase
13
+ }
14
+
15
+ export interface Definable {
16
+ definition:string; // hanzi, phrase
17
+ definition_analysis:string; // hanzi, phrase
18
+ translation?:string; // hanzi
19
+ transliteration?:string; // phrase
20
+ function?:string; // hanzi
21
+ usage?:string; // phrase
22
+ }
@@ -0,0 +1,4 @@
1
+ export interface GeoLocation {
2
+ lat:number;
3
+ lon:number;
4
+ }
@@ -0,0 +1,25 @@
1
+ import { ContentBody, ContentMeta } from "./Content";
2
+ import { Definable, DictionaryEntry, Pronouncable } from "./DictionaryEntry";
3
+
4
+ export interface HanziComponent {
5
+ component:string;
6
+ meaning:string;
7
+ pinyin:string;
8
+ }
9
+
10
+ export interface HanziSense extends Definable, Pronouncable {
11
+ phrases:string[];
12
+ }
13
+
14
+ export interface HanziPayload extends DictionaryEntry {
15
+ components:HanziComponent[];
16
+ component_analysis:string;
17
+ radical:string;
18
+ radical_analysis:string;
19
+ senses:HanziSense[];
20
+ }
21
+
22
+ export interface HanziBody extends ContentBody {
23
+ meta:ContentMeta;
24
+ payload:HanziPayload;
25
+ }
@@ -0,0 +1,162 @@
1
+ import { Doc } from '@shaxpir/sharedb/lib/client';
2
+ import { ShareSync, ShareSyncFactory } from '../../repo/ShareSync';
3
+ import { CachingHasher } from '../../util/CachingHasher';
4
+ import { BatchOperation } from "../markup/Operation";
5
+ import { Content, ContentId, ContentRef } from "./Content";
6
+ import { ContentKind } from './ContentKind';
7
+ import { Model } from './Model';
8
+ import { CompactDateTime, SingleTime, Time } from './Time';
9
+
10
+ export interface ManifestMeta {
11
+ kind:ContentKind;
12
+ ref:ContentRef;
13
+ updated_at:SingleTime;
14
+ owner:ContentId;
15
+ }
16
+
17
+ export interface ManifestBody {
18
+ meta:ManifestMeta;
19
+ payload:ManifestPayload;
20
+ }
21
+
22
+ export interface ManifestEntry {
23
+ kind:ContentKind;
24
+ ref:ContentRef;
25
+ at_utc_time:CompactDateTime;
26
+ }
27
+
28
+ export interface ManifestPayload {
29
+ device:any;
30
+ media:any;
31
+ profile:any;
32
+ user:any;
33
+ }
34
+
35
+ export class Manifest extends Model {
36
+
37
+ constructor(doc:Doc, shouldAcquire:boolean, shareSync:ShareSync) {
38
+ super(doc, shouldAcquire, shareSync);
39
+ }
40
+
41
+ public static makeManifestId(userId:ContentId):ContentId {
42
+ return CachingHasher.makeMd5ContentId(userId + "-" + ContentKind.MANIFEST);
43
+ }
44
+
45
+ public static create(userId:ContentId):Manifest {
46
+ const now:CompactDateTime = Time.utc();
47
+ const manifestId = Manifest.makeManifestId(userId);
48
+ return ShareSyncFactory.get().createManifest({
49
+ meta : {
50
+ kind : ContentKind.MANIFEST,
51
+ ref : manifestId,
52
+ updated_at : {
53
+ utc_time : now
54
+ },
55
+ owner : userId
56
+ },
57
+ payload: {
58
+ device : {},
59
+ media : {},
60
+ profile : {},
61
+ user: {}
62
+ }
63
+ });
64
+ }
65
+
66
+ public get meta():ManifestMeta {
67
+ this.checkDisposed("Manifest.meta");
68
+ return this.doc.data.meta as ManifestMeta;
69
+ }
70
+
71
+ public get owner():ContentId {
72
+ this.checkDisposed("Manifest.userId");
73
+ return this.doc.data.meta.owner as ContentId;
74
+ }
75
+
76
+ public canUserView(userId:ContentId):boolean {
77
+ this.checkDisposed("Manifest.canUserView");
78
+ return this.doc.data.meta.owner === userId;
79
+ }
80
+
81
+ public doesContentExist(kind:ContentKind, ref:ContentRef):boolean {
82
+ this.checkDisposed("Manifest.doesContentExist");
83
+ return this.doc.data &&
84
+ this.doc.data.payload &&
85
+ this.doc.data.payload[kind] &&
86
+ this.doc.data.payload[kind][ref];
87
+ }
88
+
89
+ public getUtcTimeOf(kind:ContentKind, ref:ContentRef):CompactDateTime {
90
+ this.checkDisposed("Manifest.getTimestampOf");
91
+ if (this.doesContentExist(kind, ref)) {
92
+ return this.doc.data.payload[kind][ref] as CompactDateTime;
93
+ }
94
+ return null as CompactDateTime;
95
+ }
96
+
97
+ public getAllEntries():ManifestEntry[] {
98
+ return this.getAllEntriesWhere(() => true);
99
+ }
100
+
101
+ public getAllEntriesWhere(
102
+ predicate?:(entry:ManifestEntry) => boolean
103
+ ):ManifestEntry[] {
104
+ const entries = [] as ManifestEntry[];
105
+ if (this.exists() && this.doc.data.payload) {
106
+ Manifest.forEachEntry(
107
+ this.doc.data as ManifestBody,
108
+ (entry:ManifestEntry) => {
109
+ if (!predicate || predicate(entry)) {
110
+ entries.push(entry);
111
+ }
112
+ }
113
+ );
114
+ }
115
+ return entries;
116
+ }
117
+
118
+ public update(content:Content):void {
119
+ this.checkDisposed("Manifest.update");
120
+ content.acquire().then(() => {
121
+
122
+ const kind:ContentKind = content.kind;
123
+ const contentRef:ContentId = content.ref;
124
+
125
+ const timestamp:CompactDateTime = content.updatedAt.utc_time;
126
+ const batch = new BatchOperation(this, content.meta.updated_at);
127
+
128
+ // Update the embedded timestamp of this content item
129
+ batch.setPathValue([ 'payload', kind, contentRef ], timestamp);
130
+
131
+ batch.commit();
132
+ content.release();
133
+ });
134
+ }
135
+
136
+ public static forEachEntry(
137
+ body:ManifestBody,
138
+ callback:(entry:ManifestEntry) => void
139
+ ):void {
140
+ const kinds = Object.keys(body.payload);
141
+ for (let i = 0; i < kinds.length; i++) {
142
+ const kind = kinds[i] as ContentKind;
143
+ const manifestPayload = (body.payload as any)[kind];
144
+ const refs = Object.keys(manifestPayload);
145
+ for (let j = 0; j < refs.length; j++) {
146
+ const ref = refs[j] as ContentRef;
147
+ const timestamp = manifestPayload[ref] as CompactDateTime;
148
+ if (ref && timestamp) {
149
+ const entry = {
150
+ kind : kind,
151
+ ref : ref,
152
+ at_utc_time : timestamp
153
+ };
154
+ callback(entry);
155
+ } else {
156
+ console.error(`manifest with missing ref or timestamp: ${JSON.stringify(body)}`);
157
+ }
158
+ }
159
+ }
160
+ }
161
+
162
+ }
@@ -0,0 +1,126 @@
1
+ import { Doc } from '@shaxpir/sharedb/lib/client';
2
+ import { ShareSync, ShareSyncFactory } from '../../repo/ShareSync';
3
+ import { Content, ContentBody, ContentId, ContentMeta } from './Content';
4
+ import { ContentKind } from './ContentKind';
5
+ import { MultiClock } from './Time';
6
+
7
+ export interface MediaDimensions {
8
+ width:number;
9
+ height:number;
10
+ }
11
+
12
+ export interface MediaCropping extends MediaDimensions {
13
+ offset_top:number;
14
+ offset_left:number;
15
+ }
16
+
17
+ export interface MediaPayload extends MediaDimensions {
18
+ extension:string;
19
+ bytes:number;
20
+ }
21
+
22
+ export interface MediaBody extends ContentBody {
23
+ meta:ContentMeta;
24
+ payload:MediaPayload;
25
+ }
26
+
27
+ export class Media extends Content {
28
+
29
+ constructor(doc:Doc, shouldAcquire:boolean, shareSync:ShareSync) {
30
+ super(doc, shouldAcquire, shareSync);
31
+ }
32
+
33
+ public get payload():MediaPayload {
34
+ this.checkDisposed("Media.payload");
35
+ return this.doc.data.payload as MediaPayload;
36
+ }
37
+
38
+ public static create(
39
+ userId:ContentId,
40
+ mediaId:ContentId,
41
+ payload:MediaPayload
42
+ ):Media {
43
+ const now = MultiClock.now();
44
+ return ShareSyncFactory.get().createContent({
45
+ meta : {
46
+ ref : mediaId,
47
+ is_head : true,
48
+ kind : ContentKind.MEDIA,
49
+ id : mediaId,
50
+ owner : userId,
51
+ created_at : now,
52
+ updated_at : now
53
+ },
54
+ payload : payload
55
+ }) as Media;
56
+ }
57
+
58
+ get bytes():number {
59
+ this.checkDisposed("Media.bytes");
60
+ return this.payload.bytes;
61
+ }
62
+
63
+ get width():number {
64
+ this.checkDisposed("Media.width");
65
+ return this.payload.width;
66
+ }
67
+
68
+ get height():number {
69
+ this.checkDisposed("Media.height");
70
+ return this.payload.height;
71
+ }
72
+
73
+ get extension():string {
74
+ this.checkDisposed("Media.extension");
75
+ return this.payload.extension;
76
+ }
77
+
78
+ public get mediaType():string {
79
+ this.checkDisposed("Media.mediaType");
80
+ const extension:string = this.extension;
81
+ if (extension === "jpg" || extension === "jpeg") {
82
+ return "image/jpeg";
83
+ } else if (extension === "png") {
84
+ return "image/png";
85
+ } else if (extension === "gif") {
86
+ return "image/gif";
87
+ } else if (extension === "svg") {
88
+ return "image/svg+xml";
89
+ } else if (extension === "bmp") {
90
+ return "image/bmp";
91
+ }
92
+ return "image/" + extension;
93
+ }
94
+
95
+ public scaleToFit(boxSize:number, cropping:MediaCropping):MediaCropping {
96
+ this.checkDisposed("Media.scaleToFit");
97
+ const zoom = boxSize / cropping.width;
98
+ const scaledWidth = Math.floor(this.width * zoom);
99
+ const scaledHeight = Math.floor(this.height * zoom);
100
+ const offsetLeft = -Math.floor(cropping.offset_left * zoom);
101
+ const offsetTop = -Math.floor(cropping.offset_top * zoom);
102
+ return {
103
+ "offset_top" : offsetTop,
104
+ "offset_left" : offsetLeft,
105
+ "width" : scaledWidth,
106
+ "height" : scaledHeight
107
+ };
108
+ }
109
+
110
+ public static makeDefaultCropping(media:Media):MediaCropping {
111
+ const width = media.width;
112
+ const height = media.height;
113
+ const isLandscapeOrSquare:boolean = width >= height;
114
+ const isPortraitOrSquare:boolean = height >= width;
115
+ const croppingOffsetTop:number = isLandscapeOrSquare ? 0 : Math.floor((height - width) / 2);
116
+ const croppingOffsetLeft:number = isPortraitOrSquare ? 0 : Math.floor((width - height) / 2);
117
+ const croppingWidth:number = isLandscapeOrSquare ? height : width;
118
+ const croppingHeight:number = isPortraitOrSquare ? width : height;
119
+ return {
120
+ offset_top : croppingOffsetTop,
121
+ offset_left : croppingOffsetLeft,
122
+ width : croppingWidth,
123
+ height : croppingHeight
124
+ };
125
+ }
126
+ }