Skip to content

Commit e8cb2ef

Browse files
committed
we came a long way today
1 parent 11bfd8e commit e8cb2ef

File tree

5 files changed

+53
-24
lines changed

5 files changed

+53
-24
lines changed

lib/editor/tiny/plugins/collaborative/amd/build/collaborater.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/editor/tiny/plugins/collaborative/amd/src/collaborater.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ let newHash = '';
3737
//let newContent = '';
3838
//let lastHash = '';
3939
const INTERVALTIMEOUT = 1000;
40+
const HEADER = "changes Index: a\n===================================================================";
4041

4142
const fetchOne = (methodname, args) => call([{
4243
methodname,
@@ -74,15 +75,15 @@ export const register = (editor) => {
7475
currentHash = newHash;
7576
}
7677
if (newHash !== currentHash) {
77-
const patch = jsDiff.createPatch('a', currentContent, newContent);
78-
79-
c.log('contextid', Options.getContextId(editor));
78+
let patch = jsDiff.createPatch('a', currentContent, newContent);
79+
patch = patch.substring(HEADER.length);
80+
/* c.log('contextid', Options.getContextId(editor));
8081
c.log('pagehash', Options.getPageHash(editor));
8182
c.log('pageinstance', Options.getPageInstance(editor));
8283
c.log('elementid', editor.targetElm.id);
8384
c.log('oldcontenthash', currentHash);
8485
c.log('newcontenthash', newHash);
85-
c.log('changes', patch);
86+
c.log('changes', patch);*/
8687
return fetchOne('tiny_collaborate_save_changes', {
8788
contextid: Options.getContextId(editor),
8889
pagehash: Options.getPageHash(editor),
@@ -104,7 +105,30 @@ export const register = (editor) => {
104105
});
105106
}
106107
});
108+
109+
fetchOne('tiny_collaborate_get_changes', {
110+
contextid: Options.getContextId(editor),
111+
pagehash: Options.getPageHash(editor),
112+
pageinstance: Options.getPageInstance(editor),
113+
elementid: editor.targetElm.id,
114+
currenthash: currentHash,
115+
}).then((result) => {
116+
if (result) {
117+
for (let i in result) {
118+
let change = result[i];
119+
c.log('changes', change);
120+
let patch = HEADER + change;
121+
currentContent = jsDiff.applyPatch(currentContent, patch);
122+
editor.setContent(currentContent);
123+
sha1(currentContent).then(hash => {
124+
currentHash = hash;
125+
});
126+
}
127+
}
128+
});
129+
107130
}, INTERVALTIMEOUT);
131+
108132
/*editor.on('Change', (event) => {
109133
c.log('Change collaborative', event);
110134
});*/

lib/editor/tiny/plugins/collaborative/classes/change_manager.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
*/
2828
class change_manager {
2929

30+
protected $autosaveid;
31+
3032
/** @var int The contextid */
3133
protected $contextid;
3234

@@ -57,26 +59,27 @@ public function __construct(
5759
string $elementid,
5860
string $oldcontenthash
5961
) {
62+
global $DB;
6063
$this->contextid = $contextid;
6164
$this->pagehash = $pagehash;
6265
$this->pageinstance = $pageinstance;
6366
$this->elementid = $elementid;
6467
$this->oldcontenthash = $oldcontenthash;
68+
$this->autosaveid = $DB->get_field('tiny_autosave', 'id', ['elementid' => $elementid, 'contextid' => $contextid, 'pagehash' => $pagehash]);
6569
}
6670

6771
public function add_collaborative_record($newcontenthash, $changes) {
6872
global $DB;
6973

70-
$autosave = $DB->get_record('tiny_autosave', ['elementid' => $this->elementid, 'contextid' => $this->contextid, 'pagehash' =>$this->pagehash]);
7174
$record = new \stdClass();
7275
$record->oldcontenthash = $this->oldcontenthash;
7376
$record->newcontenthash = $newcontenthash;
7477
$record->timemodified = time();
7578
$record->changes = $changes;
76-
$record->autosaveid = $autosave->id;
79+
$record->autosaveid = $this->autosaveid;
7780

7881
// try {
79-
$record = $DB->insert_record('tiny_collaborative_changes', $record);
82+
$record->id = $DB->insert_record('tiny_collaborative_changes', $record);
8083
// } catch(\Exception $e) {
8184
// return "-1";
8285
// }
@@ -86,8 +89,10 @@ public function add_collaborative_record($newcontenthash, $changes) {
8689
public function get_changes() {
8790
global $DB;
8891
$changesarray = [];
89-
while ($change = $DB->get_record('tiny_collaborative_changes', ['oldcontenthash' => $this->oldcontenthash])) {
92+
$currenthash = $this->oldcontenthash;
93+
while ($change = $DB->get_record('tiny_collaborative_changes', ['oldcontenthash' => $currenthash, 'autosaveid' => $this->autosaveid])) {
9094
$changesarray[] = $change->changes;
95+
$currenthash = $change->newcontenthash;
9196
}
9297
return $changesarray;
9398
}

lib/editor/tiny/plugins/collaborative/classes/external/get_changes.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use core_external\external_function_parameters;
2121
use core_external\external_single_structure;
2222
use core_external\external_value;
23+
use core_external\external_multiple_structure;
2324

2425
/**
2526
* Web Service to resume an autosave session.
@@ -41,7 +42,7 @@ public static function execute_parameters(): external_function_parameters {
4142
'pagehash' => new external_value(PARAM_ALPHANUMEXT, 'The page hash', VALUE_REQUIRED),
4243
'pageinstance' => new external_value(PARAM_ALPHANUMEXT, 'The page instance', VALUE_REQUIRED),
4344
'elementid' => new external_value(PARAM_RAW, 'The ID of the element', VALUE_REQUIRED),
44-
'hash' => new external_value(PARAM_ALPHANUMEXT, 'The ID of the element', VALUE_REQUIRED),
45+
'currenthash' => new external_value(PARAM_ALPHANUMEXT, 'The ID of the element', VALUE_REQUIRED),
4546
]);
4647
}
4748

@@ -63,42 +64,41 @@ public static function execute(
6364
string $pagehash,
6465
string $pageinstance,
6566
string $elementid,
66-
string $hash
67+
string $currenthash
6768
): array {
6869

6970
[
7071
'contextid' => $contextid,
7172
'pagehash' => $pagehash,
7273
'pageinstance' => $pageinstance,
7374
'elementid' => $elementid,
74-
'hash' => $hash,
75+
'currenthash' => $currenthash,
7576
] = self::validate_parameters(self::execute_parameters(), [
7677
'contextid' => $contextid,
7778
'pagehash' => $pagehash,
7879
'pageinstance' => $pageinstance,
7980
'elementid' => $elementid,
80-
'hash' => $hash,
81+
'currenthash' => $currenthash,
8182
]);
8283

8384

8485
// May have been called by a non-logged in user.
8586
if (isloggedin() && !isguestuser()) {
86-
$manager = new \tiny_collaborative\change_manager($contextid, $pagehash, $pageinstance, $elementid,$hash);
87+
$manager = new \tiny_collaborative\change_manager($contextid, $pagehash, $pageinstance, $elementid, $currenthash);
8788
$changes = $manager->get_changes();
8889
}
89-
return [
90-
'changes' => $changes,
91-
];
90+
return $changes;
9291
}
9392

9493
/**
9594
* Describe the return structure of the external service.
9695
*
9796
* @return external_single_structure
9897
*/
99-
public static function execute_returns(): external_single_structure {
100-
return new external_single_structure([
101-
'drafttext' => new external_value(PARAM_RAW, 'The draft text'),
102-
]);
98+
public static function execute_returns(): external_multiple_structure {
99+
return new external_multiple_structure(new external_value(PARAM_RAW, 'Description of the change')
100+
101+
// Add other fields related to the change here
102+
);
103103
}
104104
}

lib/editor/tiny/plugins/collaborative/db/install.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
<TABLE NAME="tiny_collaborative_changes" COMMENT="Default comment for tiny_collaborative, please edit me">
88
<FIELDS>
99
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
10-
<FIELD NAME="contenthashbefore" TYPE="char" LENGTH="50" NOTNULL="true" SEQUENCE="false" COMMENT="The content hash before the change"/>
11-
<FIELD NAME="contenthashafter" TYPE="char" LENGTH="50" NOTNULL="true" SEQUENCE="false"/>
10+
<FIELD NAME="oldcontenthash" TYPE="char" LENGTH="50" NOTNULL="true" SEQUENCE="false" COMMENT="The content hash before the change"/>
11+
<FIELD NAME="newcontenthash" TYPE="char" LENGTH="50" NOTNULL="true" SEQUENCE="false"/>
1212
<FIELD NAME="timemodified" TYPE="int" LENGTH="20" NOTNULL="true" SEQUENCE="false"/>
1313
<FIELD NAME="changes" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
1414
<FIELD NAME="autosaveid" TYPE="int" LENGTH="20" NOTNULL="true" SEQUENCE="false"/>
1515
</FIELDS>
1616
<KEYS>
1717
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
18-
<KEY NAME="contenthashbefore_unique" TYPE="unique" FIELDS="contenthashbefore"/>
18+
<KEY NAME="oldcontenthash_unique" TYPE="unique" FIELDS="oldcontenthash"/>
1919
</KEYS>
2020
</TABLE>
2121
</TABLES>

0 commit comments

Comments
 (0)